library(lfe)
## Warning: package 'lfe' was built under R version 4.3.2
## Loading required package: Matrix
################Long-term effects of extreme heat##################
###################################################################

filesave <-paste0("E:/POI_gridmet_daily_weather_data/7cities_heat_spend_visit_climate",".RData")
load(filesave)

Q1 <- quantile(df$spend_pertransaction, 0.25, na.rm = TRUE) # 1st Quartile
Q3 <- quantile(df$spend_pertransaction, 0.75, na.rm = TRUE) # 3rd Quartile
IQR <- Q3 - Q1 # Interquartile Range

# Define outlier thresholds
lower_bound <- Q1 - 1.5 * IQR
upper_bound <- Q3 + 1.5 * IQR

df <- df[df$spend_pertransaction >= lower_bound & df$spend_pertransaction <= upper_bound, ]
# Standardize the variables
# df_standardized <- df  # Create a copy of your dataframe
# df_standardized[, c("spend_pertransaction", 
#                     "extreme_heat_100F", 
#                     "extreme_heat_7days", 
#                     "extreme_heat_14days", 
#                     "extreme_heat_21days", 
#                     "extreme_heat_28days", 
#                     "pr", 
#                     "rmax", 
#                     "wind")] <- scale(df[, c("spend_pertransaction", 
#                                              "extreme_heat_100F", 
#                                              "extreme_heat_7days", 
#                                              "extreme_heat_14days", 
#                                              "extreme_heat_21days", 
#                                              "extreme_heat_28days", 
#                                              "pr", 
#                                              "rmax", 
#                                              "wind")])


# Fit the regression model with standardized variables
FE_airtemp_spend_tran_7days <- felm(spend_pertransaction ~ extreme_heat_100F + extreme_heat_7days + pr + rmax + wind | 
                                                   place_month + date | 0, 
                                                 data = df)

# Extract standardized coefficients
summary(FE_airtemp_spend_tran_7days)
## 
## Call:
##    felm(formula = spend_pertransaction ~ extreme_heat_100F + extreme_heat_7days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.548  -5.190  -1.454   3.462  48.537 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F  -0.6674952  0.0284429 -23.468  < 2e-16 ***
## extreme_heat_7days -0.9881667  0.0426329 -23.178  < 2e-16 ***
## pr                  0.0044162  0.0009147   4.828 1.38e-06 ***
## rmax                0.0501979  0.0008056  62.312  < 2e-16 ***
## wind                0.0300137  0.0077705   3.863 0.000112 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.154 on 2110714 degrees of freedom
##   (32513 observations deleted due to missingness)
## Multiple R-squared(full model): 0.332   Adjusted R-squared: 0.3215 
## Multiple R-squared(proj model): 0.004433   Adjusted R-squared: -0.01129 
## F-statistic(full model):31.47 on 33336 and 2110714 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  1880 on 5 and 2110714 DF, p-value: < 2.2e-16
# Fit the regression model with standardized variables
FE_airtemp_spend_tran_14days <- felm(spend_pertransaction ~ extreme_heat_100F + extreme_heat_14days + pr + rmax + wind | 
                                                   place_month + date | 0, 
                                                 data = df)

# Extract standardized coefficients
summary(FE_airtemp_spend_tran_14days)
## 
## Call:
##    felm(formula = spend_pertransaction ~ extreme_heat_100F + extreme_heat_14days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.465  -5.191  -1.459   3.455  48.575 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F   -0.7009335  0.0261440 -26.811  < 2e-16 ***
## extreme_heat_14days -1.5652202  0.0496750 -31.509  < 2e-16 ***
## pr                   0.0062878  0.0010072   6.243 4.31e-10 ***
## rmax                 0.0493541  0.0008115  60.815  < 2e-16 ***
## wind                 0.0325882  0.0078544   4.149 3.34e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.16 on 2073163 degrees of freedom
##   (70071 observations deleted due to missingness)
## Multiple R-squared(full model): 0.3326   Adjusted R-squared: 0.3219 
## Multiple R-squared(proj model): 0.004742   Adjusted R-squared: -0.01126 
## F-statistic(full model):31.01 on 33329 and 2073163 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  1976 on 5 and 2073163 DF, p-value: < 2.2e-16
# Fit the regression model with standardized variables
FE_airtemp_spend_tran_21days <- felm(spend_pertransaction ~ extreme_heat_100F + extreme_heat_21days + pr + rmax + wind | 
                                                    place_month + date | 0, 
                                                  data = df)

# Extract standardized coefficients
summary(FE_airtemp_spend_tran_21days)
## 
## Call:
##    felm(formula = spend_pertransaction ~ extreme_heat_100F + extreme_heat_21days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.343  -5.190  -1.464   3.445  48.603 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F   -0.6858297  0.0255530 -26.839  < 2e-16 ***
## extreme_heat_21days -2.2644277  0.0560406 -40.407  < 2e-16 ***
## pr                   0.0073856  0.0010339   7.143 9.11e-13 ***
## rmax                 0.0486307  0.0008122  59.873  < 2e-16 ***
## wind                 0.0437729  0.0080362   5.447 5.12e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.162 on 2035402 degrees of freedom
##   (107839 observations deleted due to missingness)
## Multiple R-squared(full model): 0.3336   Adjusted R-squared: 0.3227 
## Multiple R-squared(proj model): 0.005062   Adjusted R-squared: -0.01123 
## F-statistic(full model):30.58 on 33322 and 2035402 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  2071 on 5 and 2035402 DF, p-value: < 2.2e-16
# Fit the regression model with standardized variables
FE_airtemp_spend_tran_28days <- felm(spend_pertransaction ~ extreme_heat_100F + extreme_heat_28days + pr + rmax + wind | 
                                                    place_month + date | 0, 
                                                  data = df)

# Extract standardized coefficients
summary(FE_airtemp_spend_tran_28days)
## 
## Call:
##    felm(formula = spend_pertransaction ~ extreme_heat_100F + extreme_heat_28days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.247  -5.183  -1.463   3.435  48.627 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F   -0.7508903  0.0252191 -29.775  < 2e-16 ***
## extreme_heat_28days -2.4235458  0.0622031 -38.962  < 2e-16 ***
## pr                   0.0086110  0.0010353   8.317  < 2e-16 ***
## rmax                 0.0491522  0.0008183  60.063  < 2e-16 ***
## wind                 0.0322120  0.0081916   3.932 8.41e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.159 on 1997488 degrees of freedom
##   (145760 observations deleted due to missingness)
## Multiple R-squared(full model): 0.3355   Adjusted R-squared: 0.3245 
## Multiple R-squared(proj model): 0.005011   Adjusted R-squared: -0.01158 
## F-statistic(full model):30.28 on 33315 and 1997488 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  2012 on 5 and 1997488 DF, p-value: < 2.2e-16