rm(list=ls())
library(stargazer)
library(plyr)
library(lfe)
library(zoo)
library(scales)
library(rdrobust)
library(data.table)
library(ggplot2)
library(dplyr)
library(fst)
library(fixest)
zdata[,Selling_premium:=zdata$sales_price/zdata$adj_purch_price]
zdata[,Renter_fraction:=zdata$renters/zdata$totalpopulation]
zdata[,Purchase_price:=SalesPriceAmount_prev]
zdata[,Listing_price:=listing_amount]
zdata[,Selling_price:=sales_price]
zdata[,Listing_premium:=list_premium]
zdata[,Property_tax_last_year:=prop_tax_prev_year]
zdata[,Property_tax_rate_last_year:=prop_tax_rate]

zdata_reg <- zdata[PurchaseYear<2008 & ListingYear>2013 & house_age>0 ]
zdata_reg[,Time_on_market:=as.numeric(sale_date)-as.numeric(listed_date)+1]

zdata_reg[,zip_list_month:=paste(zip,ListingMonth)]
zdata_reg[,tract_list_month:=paste(GEOID,ListingMonth)]

zdata_reg[,zip3:=floor(zip/100)]
setorder(zdata_reg,listed_date)
zdata_reg[,predprice:= 0]

pb <- txtProgressBar(min=1, max=nrow(zdata_reg), style=3)

for(i in 1:nrow(zdata_reg)) {
  
  setTxtProgressBar(pb, i)
  
  temp <- CA_all[zip==zdata_reg[i]$zip &  Sale_month < zdata_reg[i]$ListingMonth & Sale_month> as.Date(paste0((year(zdata_reg[i]$ListingMonth)-1),"-",month(zdata_reg[i]$ListingMonth),"-01")) & hpi_inflation>0 & sqft>0 & house_age>0 & !is.na(Purchase_price) & !is.na(hpi_inflation) & !is.na(beds) & !is.na(baths) & !is.na(sqft) & !is.na(house_age) & !is.na(yr) & !is.na(sales_price) & sales_price>0 & LotSizeSquareFeet>1000 & LotSizeSquareFeet<500000 &  sales_price<2000000 & sales_price>100000 &  adj_purch_price<2000000 & adj_purch_price>100000 & abs(sales_price-adj_purch_price) < 100000 & beds==zdata_reg[i]$beds ]

  
  if(nrow(temp)<5) {
    
    zdata_reg[i,predprice:=adj_purch_price]
    
  } else {
    
    zdata_reg[i,predprice:=mean(temp$sales_price,na.rm=T)]
    
  }
  
}
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
# zdata_reg[,predprice:=adj_purch_price]#exp(predict(hedonicmodel,newdata = zd))]
# zdata_reg[,predprice:=(predict(hedonicmodel,newdata = zdata_reg))]#
# zdata_reg[,predprice:=exp(predict(hedonicmodel,newdata = zdata_reg))]#
zdata_reg[,nominalloss:=Purchase_price-predprice]
zdata_reg[,nominalloss:=ifelse(nominalloss<=0,1,nominalloss)]
zdata_reg[,lppp:=listing_amount/predprice]
zdata_reg[,sppp:=sales_price/predprice]

zdata_reg <- zdata_reg[abs(predprice-sales_price)<400000]

zdata_reg[,lp:=floor(listing_amount/1000)*1000]
temp <- zdata_reg[,.(predprice=mean(predprice,na.rm=T),adj_purc=mean(adj_purch_price,na.rm=T),.N),by=lp]

Figure 2

# ggplot(temp[N>10 & lp<500000],aes(x=lp,y=adj_purc))+geom_point()+geom_smooth(method="lm")+theme_minimal()+labs(x="Listing price",y="Adjusted purchase price")+ scale_x_continuous(labels = scales::comma)+ scale_y_continuous(labels = scales::comma)+labs(title="Adjusted purchase price")

ggplot(temp[N>10 & lp<1000000 & lp>=300000],aes(x=lp,y=predprice))+geom_point()+theme_minimal()+labs(x="Listing price  ($)",y="Predicted price ($)")+ scale_x_continuous(labels = scales::comma)+ scale_y_continuous(labels = scales::comma)#+labs(title="Predicted price")

ggplot(temp[N>10 & lp<1000000 & lp>=300000],aes(x=lp,y=adj_purc))+geom_point()+theme_minimal()+labs(x="Listing price  ($)",y="Adjusted purchase price ($)")+ scale_x_continuous(labels = scales::comma)+ scale_y_continuous(labels = scales::comma)#+labs(title="Predicted price")

Table 1

stargazer(zdata_reg[,c("Purchase_price","predprice","Listing_price","lppp","sppp","Property_tax_last_year","Property_tax_rate_last_year","purchase_hpi","hpi_inflation","ownership_years","beds","baths","sqft","LotSizeSquareFeet","house_age","LTV_prev","avg_school_rating","avg_school_distance","totalpopulation","medianage","medianhouseholdincome","Renter_fraction")],summary.stat = c("mean","sd","p25","median","p75","n"),digits = 4,
          covariate.labels =c("Purchased price ($)","Predicted price ($)","Listing price ($)","Listing price/Predicted price","Selling price/Predicted price","Effective tax rate","Property taxes paid ($)","HPI_{Purchase}","HPI_{List}/HPI_{Purchase}","Years of ownership","Number of bedrooms","Number of bathrooms","House area (sq. ft)","Lot area (sq. ft)","Age of the house (years)","Loan-to-value_{Purchase}","GreatSchools rating","Distance to schools (miles)","Census tract population","Census tract median age","Census tract median income","Census traction fraction of renters") ,type="text" ) #
## 
## ===========================================================================================================
## Statistic                               Mean       St. Dev.     Pctl(25)      Median      Pctl(75)     N   
## -----------------------------------------------------------------------------------------------------------
## Purchased price ()                  421,772.7000 309,432.3000   219,000      347,000      540,000    26,531
## Predicted price ()                  577,871.5000 378,704.0000 343,685.0000 496,600.0000 703,589.6000 26,531
## Listing price ()                    628,768.1000 413,689.1000 377,044.8000 545,775.8000 763,232.5000 26,531
## Listing price/Predicted price          1.1112       0.2877       0.9438       1.0646       1.2178    26,531
## Selling price/Predicted price          1.0774       0.2573       0.9233       1.0403       1.1828    26,531
## Effective tax rate                   5,355.4670   3,618.6790     3,032        4,564        6,665     26,531
## Property taxes paid ()                 0.0105       0.0039       0.0082       0.0100       0.0121    26,531
## HPIPurchase                         419,034.1000 242,528.5000   245,800      381,100      541,550    26,531
## HPIList/HPIPurchase                    1.4240       0.6064       0.9852       1.2351       1.6899    26,531
## Years of ownership                    12.5556       2.7507         11           12           14      26,531
## Number of bedrooms                     3.4088       0.8573       3.0000       3.0000       4.0000    26,529
## Number of bathrooms                    2.4433       0.9470       2.0000       2.0000       3.0000    26,432
## House area (sq. ft)                  1,949.0630    775.0677    1,403.0000   1,794.0000   2,349.0000  26,501
## Lot area (sq. ft)                   16,239.7800  91,213.2800   5,616.0000   7,143.0000   9,800.0000  25,167
## Age of the house (years)              38.9664      22.6594         19           35           56      26,531
## Loan-to-valuePurchase                  0.6434       0.4259       0.3705       0.7845       0.8000    26,499
## GreatSchools rating                    6.8789       2.0293       5.3333       7.0000       8.6667    26,505
## Distance to schools (miles)            1.4300       1.2717       0.8000       1.1333       1.6667    26,511
## Census tract population              5,859.1530   2,908.2620   4,109.0000   5,370.0000   6,772.0000  26,521
## Census tract median age               40.0726       7.8134      34.6000      39.2000      44.4000    26,519
## Census tract median income          80,579.3200  30,776.1200  57,619.0000  75,947.0000  99,464.0000  26,518
## Census traction fraction of renters    0.3229       0.1703       0.1925       0.2938       0.4298    26,519
## -----------------------------------------------------------------------------------------------------------

Table 2

# stargazer(hedonicmodelfelm,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,covariate.labels = c("log(Purchase price)","log(HPI inflation)","Number of bedrooms","Number of bathrooms","House area","log(Age of the house)","log(Lot area)"), add.lines = list(c("Year", "Yes")),type="text")#

Table 3

controls1 = "log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_list_month|"

controls2 = "log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|tract_list_month|"


controls3 = "log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction+log(nominalloss)|tract_list_month|"

controls4 = "log(adj_purch_price)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev|zip_list_month|"


cluster = "|zip"
instruments = "ownership_years" #
endo_var = "Property_tax_rate_last_year"
regs <- list()
regs[[1]] <- felm(as.formula(paste("I(prop_tax_rate*100)~ownership_years+",controls1,"0",cluster,sep="")),data=zdata_reg)
regs[[2]] <- felm(as.formula(paste("I(prop_tax_rate*100)~ownership_years+",controls2,"0","|GEOID",sep="")),data=zdata_reg)

# .printtable(regs)

stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = F,covariate.labels = c("Ownership years","log(Predicted price)","Number of bedrooms","Number of bathrooms","House area","GreatSchools rating","Distance to schools","Distance to amenities","log(Age of the house)","log(Lot area)","Loan-to-value_{Purchase}","Census tract median age","log(Census tract median income)","Census tract fraction of renters"),type="text")#
## 
## ======================================================
##                                     (1)        (2)    
## ------------------------------------------------------
## Ownership years                  -0.068***  -0.066*** 
##                                   (0.001)    (0.002)  
## log(Predicted price)             -0.213***  -0.254*** 
##                                   (0.024)    (0.040)  
## Number of bedrooms                0.037***   0.040*** 
##                                   (0.005)    (0.010)  
## Number of bathrooms                -0.001     -0.005  
##                                   (0.002)    (0.010)  
## House area                       0.00004*** 0.00005***
##                                  (0.00001)  (0.00002) 
## GreatSchools rating                0.003      0.009   
##                                   (0.004)    (0.011)  
## Distance to schools                0.006      -0.006  
##                                   (0.005)    (0.013)  
## Distance to amenities              0.0002    -0.00004 
##                                   (0.0002)   (0.0004) 
## log(Age of the house)            -0.036***  -0.046*** 
##                                   (0.009)    (0.016)  
## log(Lot area)                      0.010*     0.011   
##                                   (0.006)    (0.010)  
## Loan-to-valuePurchase             0.115***   0.108*** 
##                                   (0.010)    (0.017)  
## Census tract median age            -0.001             
##                                   (0.001)    (0.000)  
## log(Census tract median income)    0.016              
##                                   (0.021)    (0.000)  
## Census tract fraction of renters   -0.045             
##                                   (0.039)    (0.000)  
## N                                  23,988     23,988  
## Adjusted R2                        0.369      0.396   
## ======================================================

Table 5

olsformula <- as.formula(paste("log(Listing_price)~Property_tax_rate_last_year+",controls1,"0",cluster,sep=""))
ivformula <- as.formula(paste("log(Listing_price)~",controls1,"(",endo_var,"~",instruments,")",cluster,sep=""))
ivformula_tract <-as.formula(paste("log(Listing_price)~",controls2,"(",endo_var,"~",instruments,")","|GEOID",sep="")) 
ivformula_tract_loss <-as.formula(paste("log(Listing_price)~",controls3,"(",endo_var,"~",instruments,")","|GEOID",sep=""))

regs <- list()

regs[[1]] <- felm(olsformula,data=zdata_reg)
regs[[2]] <- felm(ivformula,data=zdata_reg)
regs[[3]] <- felm(ivformula_tract,data=zdata_reg)
regs[[4]] <- felm(ivformula_tract_loss,data=zdata_reg)


stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Effective tax rate","log(Predicted price)","Number of bedrooms","Number of bathrooms","House area","GreatSchools rating","Distance to schools","Distance to amenities","log(Age of the house)","log(Lot area)","Loan-to-value_{Purchase}","Census tract median age","log(Census tract median income)","Census tract fraction of renters","log(Nominal loss)"),
          column.labels=c("OLS","IV"),column.separate=c(1,3),
          add.lines = list(c("zipmonth", "Yes", "Yes", "No","No"),
                           c("tractmonth", "Yes", "Yes", "No","No"),
                           c("Cond. F. Stat","",round(condfstat(regs[[2]])[[1]],2),round(condfstat(regs[[3]])[[1]],2),round(condfstat(regs[[4]])[[1]],2))),type="text")#
## 
## ============================================================================
##                                       OLS                   IV              
##                                       (1)        (2)        (3)       (4)   
## ----------------------------------------------------------------------------
## Effective tax rate                  1.122***                                
##                                     (0.375)                                 
## log(Predicted price)                0.270***   0.275***  0.249***  0.256*** 
##                                     (0.019)    (0.019)    (0.026)   (0.025) 
## Number of bedrooms                 -0.018***  -0.019***  -0.014**   -0.007  
##                                     (0.004)    (0.004)    (0.007)   (0.007) 
## Number of bathrooms                  0.007      0.007      0.011     0.009  
##                                     (0.005)    (0.005)    (0.008)   (0.007) 
## House area                         0.0002***  0.0002***  0.0002*** 0.0002***
##                                    (0.00001)  (0.00001)  (0.00002) (0.00002)
## GreatSchools rating                 0.018***   0.018***   0.010*    0.011*  
##                                     (0.003)    (0.003)    (0.006)   (0.006) 
## Distance to schools                 -0.006*    -0.007**   -0.006    -0.005  
##                                     (0.003)    (0.003)    (0.005)   (0.005) 
## Distance to amenities              -0.0005*** -0.0005*** -0.001*** -0.001***
##                                     (0.0001)   (0.0001)  (0.0002)  (0.0002) 
## log(Age of the house)              -0.035***  -0.033***  -0.046*** -0.042***
##                                     (0.006)    (0.006)    (0.009)   (0.009) 
## log(Lot area)                       0.072***   0.072***  0.064***  0.062*** 
##                                     (0.007)    (0.007)    (0.010)   (0.009) 
## Loan-to-valuePurchase               -0.006**  -0.008***  -0.011**   -0.0002 
##                                     (0.003)    (0.003)    (0.005)   (0.005) 
## Census tract median age             0.004***   0.004***                     
##                                     (0.001)    (0.001)    (0.000)   (0.000) 
## log(Census tract median income)     0.113***   0.113***                     
##                                     (0.013)    (0.013)    (0.000)   (0.000) 
## Census tract fraction of renters    0.089***   0.091***                     
##                                     (0.024)    (0.024)    (0.000)   (0.000) 
## log(Nominal loss)                                                  0.006*** 
##                                                                     (0.001) 
## `Property_tax_rate_last_year(fit)`             3.175***  3.983***  -2.565** 
##                                                (0.686)    (1.117)   (1.007) 
## zipmonth                              Yes        Yes        No        No    
## tractmonth                            Yes        Yes        No        No    
## Cond. F. Stat                                   199.27     92.15     109.7  
## N                                    23,988     23,988    23,988    23,988  
## Adjusted R2                          0.939      0.939      0.947     0.949  
## ============================================================================

Table 6

zhvi <- readRDS("C:/Users/dratnadiwakara2/Documents/OneDrive - Louisiana State University/Raw Data/Zillow Research Data/Zip_Zhvi_SingleFamilyResidence.rds")
zhvi <- data.table(zhvi)
zhvi <- zhvi[state=="CA"]

zhvi <- zhvi[month %in% as.Date(c("2006-01-01","2014-01-01"))]
zhvi <- zhvi[,c("zipcode","month","zhvi")]

zhvi <- dcast(zhvi,zipcode~month,value.var = "zhvi")
names(zhvi) <- c("zipcode","val2006","val2014")

zhvi <- zhvi[complete.cases(zhvi),]
zhvi <- data.table(zhvi)
zhvi[,pricechange:=log(val2014/val2006)]

hist <- ggplot()+geom_histogram(data=zhvi,aes(x=pricechange),fill="dodgerblue4",color="dodgerblue4",alpha=0.75)+labs(x="Price Change 2006 to 2014",y="Number of Zipcodes")+ theme_minimal()+scale_y_continuous(labels = comma)

zhvi[,pricechangeqt:=ntile(pricechange,16)]
zhvi <- zhvi[,c("zipcode","pricechange","pricechangeqt")]
zhvi[,zipcode:=as.numeric(zipcode)]

zdata_reg <- merge(zdata_reg, zhvi, by.x="zip",by.y="zipcode",all.x=T)

# print(hist)
regs <- list()
regs[[1]] <- felm(olsformula,data=zdata_reg[nominalloss>1])
regs[[2]] <- felm(olsformula,data=zdata_reg[nominalloss<=1])
regs[[3]] <- felm(olsformula,data=zdata_reg[pricechangeqt<=4])
regs[[4]] <- felm(olsformula,data=zdata_reg[pricechangeqt %in% 5:16])
regs[[5]] <- felm(ivformula,data=zdata_reg[pricechangeqt<=4])
regs[[6]] <- felm(ivformula,data=zdata_reg[pricechangeqt  %in% 5:16])


stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Effective tax rate","log(Predicted price)"),
          column.labels=c("OLS","IV"),column.separate=c(4,2),
          add.lines = list(c("Controls", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("zipmonth", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("Cond. F. Stat","","","","",round(condfstat(regs[[5]])[[1]],2),round(condfstat(regs[[6]])[[1]],2))),
          keep=c("Property_tax_rate_last_year","predprice"),type="text")#
## 
## ========================================================================================
##                                                    OLS                        IV        
##                                      (1)      (2)      (3)      (4)      (5)      (6)   
## ----------------------------------------------------------------------------------------
## Effective tax rate                  -0.366  1.331***  -1.494  1.394***                  
##                                    (1.630)  (0.385)  (1.099)  (0.364)                   
## log(Predicted price)               0.268*** 0.298*** 0.237*** 0.286*** 0.242*** 0.291***
##                                    (0.041)  (0.022)  (0.043)  (0.020)  (0.044)  (0.020) 
## `Property_tax_rate_last_year(fit)`                                      0.491   3.251***
##                                                                        (1.915)  (0.736) 
## Controls                             Yes      Yes      Yes      Yes      Yes      Yes   
## zipmonth                             Yes      Yes      Yes      Yes      Yes      Yes   
## Cond. F. Stat                                                            26.2    172.1  
## N                                   5,783    18,205   3,756    20,212   3,756    20,212 
## Adjusted R2                         0.925    0.948    0.828    0.924    0.827    0.924  
## ========================================================================================

Table 7

regs <- list()
regs[[1]] <- felm(ivformula,data=zdata_reg[Listing_price<listing_q2])
regs[[2]] <- felm(ivformula,data=zdata_reg[Listing_price>listing_q2])

    CA <- read_fst("C:/Users/dratnadiwakara2/Documents/OneDrive - Louisiana State University/Projects/Sunk Cost/Processed Data/all_homes_prev/CA_all_homes_prev.fst",columns = c("BuildingAreaSqFt","zip","Bedrooms"),as.data.table = T)
    CA_summary <- CA[,.(
      sqft_lb=quantile(BuildingAreaSqFt,0.25, na.rm=T),
      sqft_hb=quantile(BuildingAreaSqFt,0.75, na.rm=T)),
      by=zip]
    temp <- CA[,.(
      beds_lb=quantile(Bedrooms,0.25, na.rm=T),
      beds_hb=floor(quantile(Bedrooms,0.75, na.rm=T)+0.99)),
      by=zip]
    CA_summary <- merge(CA_summary,temp,by="zip")
    
    
zdata_reg <- merge(zdata_reg,CA_summary,by="zip",all.x=T)
zdata_reg[,outlider_sqft:=ifelse(sqft>sqft_hb ,1,0)]
zdata_reg[,outlider_beds:=ifelse(beds>beds_hb,1,0)]

regs[[3]] <- felm(ivformula,data=zdata_reg[outlider_sqft==0 & outlider_beds==0])
regs[[4]] <- felm(ivformula,data=zdata_reg[outlider_sqft==1 | outlider_beds==1])

    redfin <- read.csv("C:/Users/dratnadiwakara2/Documents/Research/UH computer27/sunkcost_2019/home_sales_redfin.csv")
    redfin$month <- as.Date(redfin$month,origin="1900-01-01")
    redfin <- data.table(redfin)
    redfin <- redfin[redfin$month<="2016-01-01"]
    redfin <- redfin[,.(median_home_sales=median(home_sales)),by=list(zip)]
    redfin[,low_activiy:=ifelse(redfin$median_home_sales<quantile(redfin$median_home_sales,0.75),1,0)]
    
    names(redfin) <- c("zip","median_home_sales_2","low_activity_2")
    
    zdata_reg <- merge(zdata_reg,redfin,by="zip",all.x=TRUE)


regs[[5]] <- felm(ivformula,data=zdata_reg[low_activiy==1])
regs[[6]] <- felm(ivformula,data=zdata_reg[low_activiy==0])




  temp <- zdata_reg[!duplicated(zdata_reg$GEOID)]
  median_age <- median(temp$medianage,na.rm = TRUE)

regs[[7]] <- felm(ivformula,data=zdata_reg[medianage<=median_age])
regs[[8]] <- felm(ivformula,data=zdata_reg[medianage>median_age])

  medincome <- median(temp$medianhouseholdincome,na.rm = TRUE)

regs[[9]] <- felm(ivformula,data=zdata_reg[medianhouseholdincome<=medincome])
regs[[10]] <- felm(ivformula,data=zdata_reg[medianhouseholdincome>medincome])

  median_renterfrac <- median(temp$Renter_fraction,na.rm = TRUE)

regs[[11]] <- felm(ivformula,data=zdata_reg[Renter_fraction<=median_renterfrac])
regs[[12]] <- felm(ivformula,data=zdata_reg[Renter_fraction>median_renterfrac])

stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Effective tax rate"),
          column.labels=c("L Pr.","H Pr","L Sz","H Sz","L Act","H Act","L Ag","H Ag","L Inc","H Inc","L Ren","H Ren"),
          add.lines = list(
            c("Controls", rep("Yes",12)),
            c("zipmonth", rep("Yes",12)),
            c("Cond. F. Stat",round(c(condfstat(regs[[1]])[[1]],condfstat(regs[[2]])[[1]],condfstat(regs[[3]])[[1]],condfstat(regs[[4]])[[1]],condfstat(regs[[5]])[[1]],condfstat(regs[[6]])[[1]],condfstat(regs[[7]])[[1]],condfstat(regs[[8]])[[1]],condfstat(regs[[9]])[[1]],condfstat(regs[[10]])[[1]],condfstat(regs[[11]])[[1]],condfstat(regs[[12]])[[1]]),2))),
          keep=c("Property_tax_rate_last_year"), flip=T,
          type="text")#
## 
## ===========================================================================================================================
##                     L Pr.    H Pr     L Sz    H Sz    L Act    H Act    L Ag     H Ag    L Inc    H Inc    L Ren    H Ren  
##                      (1)     (2)      (3)      (4)     (5)      (6)      (7)     (8)      (9)      (10)     (11)     (12)  
## ---------------------------------------------------------------------------------------------------------------------------
## Effective tax rate  0.838  3.638*** 3.546*** 3.647** 4.085*** 2.407***  1.518  4.158*** 6.828*** 2.108*** 2.527*** 6.181***
##                    (0.864) (0.960)  (0.860)  (1.442) (1.171)  (0.857)  (1.161) (0.960)  (1.547)  (0.753)  (0.850)  (1.482) 
## Controls             Yes     Yes      Yes      Yes     Yes      Yes      Yes     Yes      Yes      Yes      Yes      Yes   
## zipmonth             Yes     Yes      Yes      Yes     Yes      Yes      Yes     Yes      Yes      Yes      Yes      Yes   
## Cond. F. Stat      110.03   70.33    141.58   48.36   94.98    108.64   69.48   123.89   52.74    144.08   119.59    65.4  
## N                  12,497   11,370   15,819   8,169   10,338   12,395   9,284   14,704   8,288    15,700   15,528   8,460  
## Adjusted R2         0.957   0.955    0.941    0.939   0.929    0.937    0.938   0.931    0.904    0.934    0.941    0.933  
## ===========================================================================================================================

Table 8 (Panel A)

regs <- list()
regs[[1]] <- felm(as.formula(paste("log(sales_price)~Property_tax_rate_last_year+",controls1,"0",cluster,sep="")),data=zdata_reg)
regs[[2]] <- felm(as.formula(paste("log(sales_price)~",controls1,"(",endo_var,"~",instruments,")",cluster,sep="")),data=zdata_reg)
regs[[3]] <- felm(as.formula(paste("log(Time_on_market)~Property_tax_rate_last_year+",controls1,"0",cluster,sep="")),data=zdata_reg)
regs[[4]] <- felm(as.formula(paste("log(Time_on_market)~",controls1,"(",endo_var,"~",instruments,")",cluster,sep="")),data=zdata_reg)


stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Effective tax rate","log(Predicted price)"),
          column.labels=c("log(Sales price)","log(Days on market)"),column.separate=c(2,2),
          add.lines = list(c("Controls", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("zipmonth", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("Cond. F. Stat","",round(condfstat(regs[[2]])[[1]],2),"",round(condfstat(regs[[4]])[[1]],2))),
          keep=c("Property_tax_rate_last_year","predprice"),          type="text")# 
## 
## =========================================================================
##                                     log(Sales price)   log(Days on market)
##                                      (1)       (2)        (3)      (4)   
## -------------------------------------------------------------------------
## Effective tax rate                 1.441***              0.692           
##                                    (0.397)              (1.274)          
## log(Predicted price)               0.273***  0.278***   -0.008    0.007  
##                                    (0.019)   (0.019)    (0.043)  (0.043) 
## `Property_tax_rate_last_year(fit)`           3.296***            6.339***
##                                              (0.732)             (2.377) 
## Controls                             Yes       Yes        Yes      Yes   
## zipmonth                             Yes       Yes        Yes      Yes   
## Cond. F. Stat                                 199.27              199.27 
## N                                   23,988    23,988    23,988    23,988 
## Adjusted R2                         0.913     0.913      0.340    0.339  
## =========================================================================
temp <- zdata_reg

temp[,purch_month_no:=as.numeric(format(temp$PurchaseMonth,"%m"))]
temp[,sale_month_no:=as.numeric(format(temp$sale_date,"%m"))]


temp <- temp[!is.na(temp$predprice) & !is.na(temp$GEOID)]
temp[,zip_purch_list:=paste(temp$zip,temp$purchase_list_year)]

adj_proptax <- felm(Property_tax_rate_last_year~log(predprice)|zip_purch_list,data=temp)
adj_proptax_resid <- data.frame(adj_proptax$residuals)
names(adj_proptax_resid) <- c("adj_proptax_resid")

adj_list <- felm(log(Listing_price)~log(predprice)|zip_purch_list,data=temp)
adj_list_resid <- data.frame(adj_list$residuals)
names(adj_list_resid) <- c("adj_list_resid")

adj_sale <- felm(log(Selling_price)~log(predprice)|zip_purch_list,data=temp)
adj_sale_resid <- data.frame(adj_sale$residuals)
names(adj_sale_resid) <- c("adj_sale_resid")

adj_purch <- felm(log(Purchase_price)~0|zip_purch_list,data=temp)
adj_purch_resid <- data.frame(adj_purch$residuals)
names(adj_purch_resid) <- c("adj_purch_resid")


temp <- cbind(temp,adj_proptax_resid)
temp <- cbind(temp,adj_list_resid)
temp <- cbind(temp,adj_sale_resid)
temp <- cbind(temp,adj_purch_resid)

temp[,DocumentDate_prev:=as.Date(temp$DocumentDate_prev,origin = "1970-01-01")]

temp[,purch_week:=as.numeric(strftime(temp$DocumentDate_prev,format="%V"))-20]
temp[,postJune:=ifelse(purch_week>-2,1,0)]
temp[,zip_purch_year:=paste(zip,PurchaseYear)]

Table 9

dscregs <- list()
dscregs[[1]] <- felm(log(Purchase_price)~postJune+purch_week|zip_purch_year|0|zip,data=temp)
dscregs[[2]] <- felm(beds~postJune+purch_week|zip_purch_year|0|zip,data=temp)
dscregs[[3]] <- felm(baths~postJune+purch_week|zip_purch_year|0|zip,data=temp)
dscregs[[4]] <- felm(log(sqft)~postJune+purch_week|zip_purch_year|0|zip,data=temp)
dscregs[[5]] <- felm(log(house_age)~postJune+purch_week|zip_purch_year|0|zip,data=temp)
# dscregs[[6]] <- felm(log(0.0001+LTV_prev)~postJune|zip_purch_year|0|zip,data=temp)


stargazer(dscregs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = F,
          covariate.labels = c("Purchased after June 01"),
          add.lines = list(c("zippurchmonth", "Yes", "Yes", "Yes","Yes","Yes","Yes")),
          column.labels=c("log(Purchase price)","No beds","No baths","log(House area)","log(Age of house)","log(Loan-to-value)"),type = "text")
## 
## ==============================================================================================
##                         log(Purchase price) No beds No baths log(House area) log(Age of house)
##                                 (1)           (2)     (3)          (4)              (5)       
## ----------------------------------------------------------------------------------------------
## Purchased after June 01      0.034***       -0.030   -0.021     -0.022**          0.021*      
##                               (0.010)       (0.019) (0.019)      (0.010)          (0.012)     
## purch_week                    0.001**        0.001  0.00001      0.0003           -0.001      
##                              (0.0003)       (0.001) (0.001)     (0.0003)         (0.0004)     
## zippurchmonth                   Yes           Yes     Yes          Yes              Yes       
## N                             26,521        26,519   26,422      26,491           26,521      
## Adjusted R2                    0.607         0.173   0.129        0.169            0.488      
## ==============================================================================================
          # keep=c("Property_tax_rate_last_year","predprice"),
          # type="text")#

Table 10 (Panel A)

regs = list()
regs[[1]] = felm(Property_tax_rate_last_year~postJune+purch_week+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp)
regs[[2]] = felm(log(Listing_price)~postJune+purch_week+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp)
regs[[3]] = felm(log(Selling_price)~postJune+purch_week+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp)


stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Purchased after June 01","Week of purchase","log(Predicted price)"),
          column.labels=c("Effective tax rate","log(Listing price)","log(Selling price)"),
          add.lines = list(c("Controls", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("zippurchmonth", "Yes", "Yes", "Yes","Yes","Yes","Yes")),
          keep=c("postJune","predprice","purch_week"),type="text")#
## 
## ================================================================================
##                         Effective tax rate log(Listing price) log(Selling price)
##                                (1)                (2)                (3)        
## --------------------------------------------------------------------------------
## Purchased after June 01     -0.0005***           -0.001             -0.001      
##                              (0.0001)           (0.004)            (0.004)      
## Week of purchase            0.00002***          -0.0001            -0.00003     
##                             (0.00000)           (0.0001)           (0.0001)     
## log(Predicted price)        -0.002***           0.263***           0.268***     
##                              (0.0002)           (0.018)            (0.019)      
## Controls                       Yes                Yes                Yes        
## zippurchmonth                  Yes                Yes                Yes        
## N                             23,988             23,988             23,988      
## Adjusted R2                   0.410              0.939              0.935       
## ================================================================================

Table 10 (Panel B)

regs = list()
regs[[1]] = felm(Property_tax_rate_last_year~postJune+purch_week+I(purch_week^2)+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp)
regs[[2]] = felm(log(Listing_price)~postJune+purch_week+I(purch_week^2)+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp)
regs[[3]] = felm(log(Selling_price)~postJune+purch_week+I(purch_week^2)+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp)

stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Purchased after June 01","Week of purchase","Week of purchase^{2}","log(Predicted price)"),
          column.labels=c("Effective tax rate","log(Listing price)","log(Selling price)"),
          add.lines = list(c("Controls", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("zippurchmonth", "Yes", "Yes", "Yes","Yes","Yes","Yes")),
          keep=c("postJune","predprice","purch_week"),type="text")#,    type="text"
## 
## ================================================================================
##                         Effective tax rate log(Listing price) log(Selling price)
##                                (1)                (2)                (3)        
## --------------------------------------------------------------------------------
## Purchased after June 01     -0.0003***           -0.005             -0.006      
##                              (0.0001)           (0.005)            (0.005)      
## Week of purchase            0.00002***           0.0002             0.0002      
##                             (0.00000)           (0.0002)           (0.0002)     
## Week of purchase2            0.00000*           -0.00001           -0.00001     
##                             (0.00000)          (0.00001)          (0.00001)     
## log(Predicted price)        -0.002***           0.263***           0.268***     
##                              (0.0002)           (0.018)            (0.019)      
## Controls                       Yes                Yes                Yes        
## zippurchmonth                  Yes                Yes                Yes        
## N                             23,988             23,988             23,988      
## Adjusted R2                   0.411              0.939              0.935       
## ================================================================================

Table 10 (Panel C)

Local linear running variable (3-months)

regs = list()
regs[[1]] = felm(Property_tax_rate_last_year~postJune+purch_week+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp[abs(purch_week)<=12])
regs[[2]] = felm(log(Listing_price)~postJune+purch_week+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp[abs(purch_week)<=12])
regs[[3]] = felm(log(Selling_price)~postJune+purch_week+log(predprice)+beds+baths+sqft+avg_school_rating+avg_school_distance+walk_score+log(house_age)+log(LotSizeSquareFeet)+LTV_prev+medianage+log(medianhouseholdincome)+Renter_fraction|zip_purch_list|0|zip,data=temp[abs(purch_week)<=12])


stargazer(regs,no.space = T,align = T,omit.stat=c("ser","f", "rsq"),style = "qje",omit.table.layout = "n", dep.var.labels.include = FALSE,
          covariate.labels = c("Purchased after June 01","Week of purchase","log(Predicted price)"),
          column.labels=c("Effective tax rate","log(Listing price)","log(Selling price)"),
          add.lines = list(c("Controls", "Yes", "Yes", "Yes","Yes","Yes","Yes"),c("zippurchmonth", "Yes", "Yes", "Yes","Yes","Yes","Yes")),
          keep=c("postJune","predprice","purch_week"),type="text")#,type="text"
## 
## ================================================================================
##                         Effective tax rate log(Listing price) log(Selling price)
##                                (1)                (2)                (3)        
## --------------------------------------------------------------------------------
## Purchased after June 01     -0.0004***           -0.003             -0.004      
##                              (0.0001)           (0.007)            (0.007)      
## Week of purchase            0.00003***          -0.00000           0.00005      
##                             (0.00001)           (0.001)            (0.001)      
## log(Predicted price)        -0.002***           0.254***           0.262***     
##                              (0.0003)           (0.023)            (0.023)      
## Controls                       Yes                Yes                Yes        
## zippurchmonth                  Yes                Yes                Yes        
## N                             12,626             12,626             12,626      
## Adjusted R2                   0.409              0.939              0.938       
## ================================================================================