rm(list=ls())
library(stargazer)
library(data.table)
library(ggplot2)
library(lfe)
library(rgdal)
library(rgeos)
library(gridExtra)
library(fst)
coastal_states <- c("TX","LA","MS","AL","FL","GA","SC","NC","VA","MD","DE","NJ","NY","CT","MA","RI","NH","ME","PA")
coastal_states_fips <- c("48","22","28","01","12","13","45","37","51","24","10","34","36","09","25","44","33","23","42")
knitr::opts_chunk$set(echo = TRUE)
policies <- read_fst(path="C:/Users/dratnadiwakara2/Documents/OneDrive - Louisiana State University/Raw Data/NFIP/policies/Policies_Nov2019.fst",columns = c("effectiveyear","censustract11","primaryresidenceindicator","postfirmconstructionindicator","policyCost1dollarcov","house_age","floodzone_cat","basefloodelevation","elevationdifference","SFHA","elevatedbuildingindicator","construction_year","totalbuildinginsurancecoverage","policycost"), as.data.table = T )
policies[,year:=as.factor(policies$effectiveyear)]
policies[,censustract:=policies$censustract11]
policies[,house_age:=effectiveyear-construction_year]
policies[,countycode:=substr(censustract,1,5)]
policies[,prefirm:=1-postfirmconstructionindicator]
policies[,state_year:=paste(substr(countycode,1,2),year)]
\[ log(premium)=pre-FIRM \times year + county FE + state*yearFE\]
formula <- as.formula("log(policyCost1dollarcov)~prefirm*year+log(house_age)+factor(floodzone_cat)+factor(elevatedbuildingindicator)+log(basefloodelevation)+log(1+elevationdifference)|countycode+state_year|0|countycode")#+log(house_age)+factor(floodzone_cat)+factor(elevatedbuildingindicator)+log(basefloodelevation)
regs <- list()
regs[[1]] <- felm(formula,data=policies[policies$SFHA==1 & policies$policyCost1dollarcov>0 & policies$basefloodelevation>0 & policies$basefloodelevation<3000 & policies$primaryresidenceindicator==1 & policies$house_age>0 & is.finite(policies$policyCost1dollarcov) & policies$effectiveyear<2020 & policies$elevationdifference>=0 & policies$elevationdifference<100 & totalbuildinginsurancecoverage>=200000])
.coef_plot_1reg(regs[[1]],"prefirm:year",2009)+ggtitle("Pre-FIRM SFHA Flood Insurance Premium")
formula <- as.formula("log(policyCost1dollarcov)~year+log(house_age)+factor(floodzone_cat)+factor(elevatedbuildingindicator)+log(basefloodelevation)+log(1+elevationdifference)|countycode|0|countycode")#+log(house_age)+factor(floodzone_cat)+factor(elevatedbuildingindicator)+log(basefloodelevation)
regs <- list()
regs[[1]] <- felm(formula,data=policies[policies$SFHA==1 & policies$policyCost1dollarcov>0 & policies$basefloodelevation>0 & policies$basefloodelevation<3000 & policies$primaryresidenceindicator==1 & policies$house_age>0 & is.finite(policies$policyCost1dollarcov) & policies$effectiveyear<2020 & policies$postfirmconstructionindicator==1 & policies$elevationdifference>=0 & policies$elevationdifference<100 & totalbuildinginsurancecoverage>=200000])
.coef_plot_1reg(regs[[1]],"year",2009)+ggtitle("SFHA Flood Insurance Premium")
temp <- policies[policies$SFHA==1 & policies$floodzone_cat=="A" & policies$primaryresidenceindicator==1 & policies$postfirmconstructionindicator==1 & policies$effectiveyear %in% c(2012,2016) & totalbuildinginsurancecoverage>=200000,.(policycost=median(policyCost1dollarcov,na.rm=TRUE),obs=.N),by=.(countycode,effectiveyear)]
t1 <- temp[temp$effectiveyear==2012]
names(t1) <- c("countycode","effectiveyear","no_sfha_post_policycost_12","no_sfha_post_obs_12")
t2 <- temp[temp$effectiveyear==2016]
names(t2) <- c("countycode","effectiveyear","no_sfha_post_policycost_16","no_sfha_post_obs_16")
no_policycost_sfha_post <- merge(t2,t1,by="countycode")
no_policycost_sfha_post[,no_sfha_post_change:=log(no_policycost_sfha_post$no_sfha_post_policycost_16/no_policycost_sfha_post$no_sfha_post_policycost_12)]
no_policycost_sfha_post[,c("effectiveyear.x","effectiveyear.y"):=list(NULL)]
files <- NULL
files <- c(files,list.files(pattern="*.fst",path = "C:/Users/dratnadiwakara2/Documents/OneDrive - Louisiana State University/Raw Data/HMDA/OO_NP/",full.names = TRUE))
hmda = lapply(files, read_fst, as.data.table = TRUE, columns=c("asofdate","respondentid","agencycode","actiontaken","censustract","purposeofloan"))
hmda <- do.call(rbind , hmda)
hmda[,countycode:=substr(censustract,1,5)]
hmdacount <- hmda[actiontaken=="1",.N,by=.(asofdate,countycode)]
hmdacount <- merge(hmdacount,no_policycost_sfha_post,by="countycode",all.x=T)
hmdacount[,state_year:=paste(substr(countycode,1,2),asofdate)]
\[ log(no)=premium increase \times year + county FE + State*Year FE \]
r <- felm(log(N)~no_sfha_post_change*factor(asofdate)|countycode+state_year|0|countycode,data=hmdacount[asofdate>=2010 & no_sfha_post_obs_12>=100])
.coef_plot_1reg(r,"no_sfha_post_change:factor(asofdate)",2010)
zhvi <- readRDS(file="C:/Users/dratnadiwakara2/Documents/OneDrive - Louisiana State University/Raw Data/Zillow Research Data/County_Zhvi_SingleFamilyResidence.rds")
zhvi <- data.table(zhvi)
zhvi[,year:=year(month)]
zhvi <- zhvi[,.(zhvi=mean(zhvi,na.rm=T)),by=.(year,fips)]
zhvi <- merge(zhvi,no_policycost_sfha_post,by.x="fips",by.y="countycode",all.x=T)
zhvi[,state_year:=paste(substr(fips,1,2),year)]
\[ log(house price)=premium increase \times year + county FE + State*Year FE \]
r <- felm(log(zhvi)~no_sfha_post_change*factor(year)|fips+state_year|0|fips,data=zhvi[year>=2008 & no_sfha_post_obs_12>=100 ])
.coef_plot_1reg(r,"no_sfha_post_change:factor(year)",2008)