library(lfe)
## Warning: package 'lfe' was built under R version 4.3.2
## Loading required package: Matrix
#### filter out outliers
filesave <-paste0("E:/POI_gridmet_daily_weather_data/7cities_heat_spend_visit_climate",".RData")
load(filesave)

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

lower_bound <- Q1 - 1.5 * IQR
upper_bound <- Q3 + 1.5 * IQR

df <- df[df$visit >= lower_bound & df$visit <= upper_bound, ]
df <- df[!is.na(df$NAME), ]


# # Standardize the variables
# df_standardized <- df  # Create a copy of your dataframe
# df_standardized[, c("visit", 
#                     "extreme_heat_100F", 
#                     "extreme_heat_7days", 
#                     "extreme_heat_14days", 
#                     "extreme_heat_21days", 
#                     "extreme_heat_28days", 
#                     "pr", 
#                     "rmax", 
#                     "wind")] <- scale(df[, c("visit", 
#                                              "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_visit_tran_7days <- felm(visit ~ extreme_heat_100F + extreme_heat_7days + pr + rmax + wind | 
                                                   place_month + date | 0, 
                                                 data = df)

# Extract standardized coefficients
summary(FE_airtemp_visit_tran_7days)
## 
## Call:
##    felm(formula = visit ~ extreme_heat_100F + extreme_heat_7days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.779  -2.899  -0.318   2.299  41.316 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F  -1.1910253  0.0182320  -65.33   <2e-16 ***
## extreme_heat_7days -1.2421761  0.0273284  -45.45   <2e-16 ***
## pr                  0.0072076  0.0005902   12.21   <2e-16 ***
## rmax                0.0402977  0.0005158   78.13   <2e-16 ***
## wind               -0.1999847  0.0049994  -40.00   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.754 on 2020275 degrees of freedom
##   (29634 observations deleted due to missingness)
## Multiple R-squared(full model): 0.6723   Adjusted R-squared: 0.6671 
## Multiple R-squared(proj model): 0.015   Adjusted R-squared: -0.0005158 
## F-statistic(full model):130.2 on 31824 and 2020275 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  6153 on 5 and 2020275 DF, p-value: < 2.2e-16
# Fit the regression model with standardized variables
FE_airtemp_visit_tran_14days <- felm(visit ~ extreme_heat_100F + extreme_heat_14days + pr + rmax + wind | 
                                                    place_month + date | 0, 
                                                  data = df)

# Extract standardized coefficients
summary(FE_airtemp_visit_tran_14days)
## 
## Call:
##    felm(formula = visit ~ extreme_heat_100F + extreme_heat_14days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.672  -2.881  -0.320   2.281  40.923 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F   -1.4395738  0.0167341  -86.03   <2e-16 ***
## extreme_heat_14days -0.9767812  0.0318495  -30.67   <2e-16 ***
## pr                   0.0150073  0.0006478   23.17   <2e-16 ***
## rmax                 0.0406583  0.0005186   78.40   <2e-16 ***
## wind                -0.2045631  0.0050423  -40.57   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.747 on 1985735 degrees of freedom
##   (64186 observations deleted due to missingness)
## Multiple R-squared(full model): 0.6718   Adjusted R-squared: 0.6665 
## Multiple R-squared(proj model): 0.01489   Adjusted R-squared: -0.0008928 
## F-statistic(full model):127.7 on 31812 and 1985735 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  6002 on 5 and 1985735 DF, p-value: < 2.2e-16
# Fit the regression model with standardized variables
FE_airtemp_visit_tran_21days <- felm(visit ~ extreme_heat_100F + extreme_heat_21days + pr + rmax + wind | 
                                                    place_month + date | 0, 
                                                  data = df)

# Extract standardized coefficients
summary(FE_airtemp_visit_tran_21days)
## 
## Call:
##    felm(formula = visit ~ extreme_heat_100F + extreme_heat_21days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.636  -2.861  -0.314   2.261  40.914 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F   -1.4395500  0.0163227  -88.19   <2e-16 ***
## extreme_heat_21days -1.3252919  0.0358651  -36.95   <2e-16 ***
## pr                   0.0149141  0.0006623   22.52   <2e-16 ***
## rmax                 0.0405758  0.0005178   78.36   <2e-16 ***
## wind                -0.1980499  0.0051429  -38.51   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.736 on 1950995 degrees of freedom
##   (98936 observations deleted due to missingness)
## Multiple R-squared(full model): 0.6715   Adjusted R-squared: 0.6662 
## Multiple R-squared(proj model): 0.01519   Adjusted R-squared: -0.0008613 
## F-statistic(full model):125.4 on 31802 and 1950995 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  6019 on 5 and 1950995 DF, p-value: < 2.2e-16
# Fit the regression model with standardized variables
FE_airtemp_visit_tran_28days <- felm(visit ~ extreme_heat_100F + extreme_heat_28days + pr + rmax + wind | 
                                                    place_month + date | 0, 
                                                  data = df)

# Extract standardized coefficients
summary(FE_airtemp_visit_tran_28days)
## 
## Call:
##    felm(formula = visit ~ extreme_heat_100F + extreme_heat_28days +      pr + rmax + wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.605  -2.830  -0.313   2.235  40.909 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## extreme_heat_100F   -1.4924689  0.0160474  -93.00   <2e-16 ***
## extreme_heat_28days -1.3497360  0.0396778  -34.02   <2e-16 ***
## pr                   0.0152852  0.0006607   23.14   <2e-16 ***
## rmax                 0.0393876  0.0005194   75.84   <2e-16 ***
## wind                -0.1903740  0.0052164  -36.50   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.713 on 1916348 degrees of freedom
##   (133611 observations deleted due to missingness)
## Multiple R-squared(full model): 0.6727   Adjusted R-squared: 0.6673 
## Multiple R-squared(proj model): 0.01482   Adjusted R-squared: -0.001516 
## F-statistic(full model):  124 on 31774 and 1916348 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  5765 on 5 and 1916348 DF, p-value: < 2.2e-16