library(lfe)
## Warning: package 'lfe' was built under R version 4.3.2
## Loading required package: Matrix
#filter out outliers (spend)
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, ]

#air temp bin
# Define the breaks with a step size of 2
breaks <- seq(14, 50, by = 2)

# Define custom labels for bins
custom_labels <- paste0("(", breaks[-length(breaks)], ", ", breaks[-1], "]")

# Categorize the column into bins with a step size of 2
df$temp_bin <- cut(df$tmax, breaks = breaks, labels = custom_labels, include.lowest = TRUE)

df$temp_bin <- relevel(df$temp_bin, ref = "(22, 24]")

FE_tempbin_spend_tran <- felm(spend_pertransaction ~ temp_bin + pr+rmax + wind| 
                                place_month + date|0, 
                              data = df)
summary(FE_tempbin_spend_tran)
## 
## Call:
##    felm(formula = spend_pertransaction ~ temp_bin + pr + rmax +      wind | place_month + date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -38.069  -5.158  -1.456   3.427  48.739 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## temp_bin(14, 16] -2.2703244  0.2833939  -8.011 1.14e-15 ***
## temp_bin(16, 18] -2.2334606  0.2461432  -9.074  < 2e-16 ***
## temp_bin(18, 20] -1.9876983  0.1607083 -12.368  < 2e-16 ***
## temp_bin(20, 22] -0.2852877  0.1489275  -1.916   0.0554 .  
## temp_bin(24, 26] -2.6428445  0.0930674 -28.397  < 2e-16 ***
## temp_bin(26, 28] -3.2761920  0.0898822 -36.450  < 2e-16 ***
## temp_bin(28, 30] -3.3142013  0.0857217 -38.662  < 2e-16 ***
## temp_bin(30, 32] -3.8333877  0.0853268 -44.926  < 2e-16 ***
## temp_bin(32, 34] -4.5261714  0.0863282 -52.430  < 2e-16 ***
## temp_bin(34, 36] -4.7270390  0.0875034 -54.021  < 2e-16 ***
## temp_bin(36, 38] -5.3766683  0.0890378 -60.386  < 2e-16 ***
## temp_bin(38, 40] -6.2970593  0.0918455 -68.561  < 2e-16 ***
## temp_bin(40, 42] -6.3879352  0.0955223 -66.874  < 2e-16 ***
## temp_bin(42, 44] -6.8298483  0.1009710 -67.642  < 2e-16 ***
## temp_bin(44, 46] -7.6859602  0.1114465 -68.965  < 2e-16 ***
## temp_bin(46, 48] -7.7446609  0.1533139 -50.515  < 2e-16 ***
## pr                0.0006977  0.0009015   0.774   0.4390    
## rmax              0.0284741  0.0008856  32.152  < 2e-16 ***
## wind              0.0928663  0.0077415  11.996  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.137 on 2143207 degrees of freedom
## Multiple R-squared(full model): 0.3338   Adjusted R-squared: 0.3234 
## Multiple R-squared(proj model): 0.006942   Adjusted R-squared: -0.008514 
## F-statistic(full model):32.19 on 33356 and 2143207 DF, p-value: < 2.2e-16 
## F-statistic(proj model): 788.5 on 19 and 2143207 DF, p-value: < 2.2e-16
#### filter out outliers (visit)
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), ]


#air temp bin
# Define the breaks with a step size of 2
breaks <- seq(14, 50, by = 2)

# Define custom labels for bins
custom_labels <- paste0("(", breaks[-length(breaks)], ", ", breaks[-1], "]")

# Categorize the column into bins with a step size of 2
df$temp_bin <- cut(df$tmax, breaks = breaks, labels = custom_labels, include.lowest = TRUE)

df$temp_bin <- relevel(df$temp_bin, ref = "(22, 24]")

FE_tempbin_visit_tran <- felm(visit ~ temp_bin + pr+rmax + wind| 
                                place_month + date|0, 
                              data = df)
summary(FE_tempbin_visit_tran)
## 
## Call:
##    felm(formula = visit ~ temp_bin + pr + rmax + wind | place_month +      date | 0, data = df) 
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -36.172  -2.732  -0.239   2.157  41.709 
## 
## Coefficients:
##                    Estimate Std. Error  t value Pr(>|t|)    
## temp_bin(14, 16]  0.6659052  0.1843508    3.612 0.000304 ***
## temp_bin(16, 18] -0.1354438  0.1539432   -0.880 0.378952    
## temp_bin(18, 20]  0.0418101  0.1019462    0.410 0.681719    
## temp_bin(20, 22] -0.2075839  0.0929614   -2.233 0.025548 *  
## temp_bin(24, 26]  3.1421802  0.0584339   53.773  < 2e-16 ***
## temp_bin(26, 28] -0.4579472  0.0563195   -8.131 4.25e-16 ***
## temp_bin(28, 30]  0.5142669  0.0537279    9.572  < 2e-16 ***
## temp_bin(30, 32]  0.4903087  0.0534963    9.165  < 2e-16 ***
## temp_bin(32, 34] -4.2951939  0.0540890  -79.410  < 2e-16 ***
## temp_bin(34, 36] -4.2917336  0.0548168  -78.292  < 2e-16 ***
## temp_bin(36, 38] -2.2119485  0.0557726  -39.660  < 2e-16 ***
## temp_bin(38, 40] -5.3669479  0.0574981  -93.341  < 2e-16 ***
## temp_bin(40, 42] -6.5089721  0.0597583 -108.922  < 2e-16 ***
## temp_bin(42, 44] -8.4354279  0.0631074 -133.668  < 2e-16 ***
## temp_bin(44, 46] -7.7358697  0.0696083 -111.134  < 2e-16 ***
## temp_bin(46, 48] -8.1173764  0.0963821  -84.221  < 2e-16 ***
## pr               -0.0041908  0.0005645   -7.424 1.13e-13 ***
## rmax              0.0008252  0.0005502    1.500 0.133653    
## wind             -0.0181423  0.0048294   -3.757 0.000172 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.564 on 2049887 degrees of freedom
## Multiple R-squared(full model): 0.6948   Adjusted R-squared:  0.69 
## Multiple R-squared(proj model): 0.08036   Adjusted R-squared: 0.06608 
## F-statistic(full model):146.5 on 31846 and 2049887 DF, p-value: < 2.2e-16 
## F-statistic(proj model):  9428 on 19 and 2049887 DF, p-value: < 2.2e-16