df <- final_data_2023

#Variables
heatwave <- df$NRI_HWAV_EALPE
inflow <- df$Migration16to20_A_Inflow_Estimate
outflow <- df$Migration16to20_A_Outflow_Estimate
net_migration <- df$Migration16to20_A_NetMigration_Estimate

#H1: As climate risk increases, in-migration will decrease, and out-migration will increase.
#heatwave_Regression-1(inflow)
model_inflow <- lm(inflow ~ heatwave)
summary(model_inflow)
## 
## Call:
## lm(formula = inflow ~ heatwave)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -148552  -17021  -11283    3502  220682 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.406e+04  1.896e+03   12.69   <2e-16 ***
## heatwave    9.742e-04  8.965e-05   10.87   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 35710 on 375 degrees of freedom
##   (16 observations deleted due to missingness)
## Multiple R-squared:  0.2395, Adjusted R-squared:  0.2374 
## F-statistic: 118.1 on 1 and 375 DF,  p-value: < 2.2e-16
#heatwave_Regression-2(outflow)
model_outflow <- lm(outflow ~ heatwave)
summary(model_outflow)
## 
## Call:
## lm(formula = outflow ~ heatwave)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -198755  -16099  -11618     482  362739 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.254e+04  2.278e+03   9.894   <2e-16 ***
## heatwave    1.302e-03  1.077e-04  12.088   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 42900 on 375 degrees of freedom
##   (16 observations deleted due to missingness)
## Multiple R-squared:  0.2804, Adjusted R-squared:  0.2785 
## F-statistic: 146.1 on 1 and 375 DF,  p-value: < 2.2e-16

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#heatwave_Regression-3(net)
model_net <- lm(net_migration ~ heatwave)
summary(model_net)
## 
## Call:
## lm(formula = net_migration ~ heatwave)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -231762   -2018      74    2679   73322 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.522e+03  9.437e+02   1.613    0.108    
## heatwave    -3.279e-04  4.462e-05  -7.348 1.26e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17770 on 375 degrees of freedom
##   (16 observations deleted due to missingness)
## Multiple R-squared:  0.1259, Adjusted R-squared:  0.1235 
## F-statistic:    54 on 1 and 375 DF,  p-value: 1.265e-12
#H2:As climate risk increases, the price-to-income (P/I) ratio of housing will decline over time.
df$PI_Change <- df$HousingPI25_2023 - df$HousingPI25_2018 

model_PI_change <- lm(PI_Change ~ heatwave, data = df)
summary(model_PI_change)
## 
## Call:
## lm(formula = PI_Change ~ heatwave, data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.78395 -0.28593 -0.08448  0.21586  2.41402 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.860e-01  2.644e-02  33.505   <2e-16 ***
## heatwave    -1.167e-09  1.267e-09  -0.921    0.357    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5048 on 385 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.002201,   Adjusted R-squared:  -0.000391 
## F-statistic: 0.8491 on 1 and 385 DF,  p-value: 0.3574
#Variables
df$hrcn_bldg  <- df$NRI_HRCN_EALB 
df$inflow     <- df$Migration16to20_A_Inflow_Estimate
df$outflow    <- df$Migration16to20_A_Outflow_Estimate
df$netmig     <- df$Migration16to20_A_NetMigration_Estimate

#H1: As climate risk increases, in-migration will decrease, and out-migration will increase.
#HRCN_Regression-1(inflow)-building
model_inflow <- lm(inflow ~ hrcn_bldg, data = df)
summary(model_inflow)
## 
## Call:
## lm(formula = inflow ~ hrcn_bldg, data = df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -103256  -18465  -12614    1792  225203 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.549e+04  2.043e+03  12.477  < 2e-16 ***
## hrcn_bldg   7.498e-05  1.055e-05   7.104 6.13e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 38440 on 375 degrees of freedom
##   (16 observations deleted due to missingness)
## Multiple R-squared:  0.1186, Adjusted R-squared:  0.1163 
## F-statistic: 50.47 on 1 and 375 DF,  p-value: 6.135e-12
#HRCN_Regression-2(outflow)
model_outflow <- lm(outflow ~ hrcn_bldg, data = df)
summary(model_outflow)
## 
## Call:
## lm(formula = outflow ~ hrcn_bldg, data = df)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -90076 -18045 -13363   -807 415175 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.449e+04  2.498e+03   9.806  < 2e-16 ***
## hrcn_bldg   9.929e-05  1.290e-05   7.694 1.27e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 47000 on 375 degrees of freedom
##   (16 observations deleted due to missingness)
## Multiple R-squared:  0.1364, Adjusted R-squared:  0.134 
## F-statistic: 59.21 on 1 and 375 DF,  p-value: 1.269e-13
#HRCN_Regression-3(net)
model_netmig <- lm(netmig ~ hrcn_bldg, data = df)
summary(model_netmig)
## 
## Call:
## lm(formula = netmig ~ hrcn_bldg, data = df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -253810   -1648     169    2837   56928 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.964e+02  9.805e+02   1.016     0.31    
## hrcn_bldg   -2.432e-05  5.066e-06  -4.800  2.3e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 18450 on 375 degrees of freedom
##   (16 observations deleted due to missingness)
## Multiple R-squared:  0.05788,    Adjusted R-squared:  0.05536 
## F-statistic: 23.04 on 1 and 375 DF,  p-value: 2.299e-06
#H2:As climate risk increases, the price-to-income (P/I) ratio of housing will decline over time.
df$PI_change  <- df$HousingPI25_2023 - df$HousingPI25_2018

model_PI <- lm(PI_change ~ hrcn_bldg, data = df)
summary(model_PI)
## 
## Call:
## lm(formula = PI_change ~ hrcn_bldg, data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.76840 -0.27331 -0.06708  0.23186  2.43306 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 8.669e-01  2.628e-02  32.988   <2e-16 ***
## hrcn_bldg   3.137e-10  1.475e-10   2.126   0.0341 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5024 on 385 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.01161,    Adjusted R-squared:  0.009041 
## F-statistic: 4.521 on 1 and 385 DF,  p-value: 0.03411
library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(broom)

df <- final_data_2023

# 파생 변수 생성
df <- df %>%
  mutate(
    inflow = Migration16to20_A_Inflow_Estimate,
    outflow = Migration16to20_A_Outflow_Estimate,
    netmig = Migration16to20_A_NetMigration_Estimate,
    PI_change = HousingPI25_2023 - HousingPI25_2018
  )

# hazard 변수 목록 생성
hazard_vars_bldg <- names(df)[grepl("_EALB$", names(df))]
hazard_vars_pop <- names(df)[grepl("_EALP$", names(df))]

# 결과 저장용 리스트 초기화
results <- list()

# 회귀 함수 정의
run_reg <- function(df, yvar, xvar) {
  formula <- as.formula(paste(yvar, "~", xvar))
  model <- lm(formula, data = df)
  tidy(model) %>%
    filter(term != "(Intercept)") %>%
    mutate(
      outcome = yvar,
      predictor = xvar,
      r_squared = summary(model)$r.squared
    )
}

# 회귀 실행: EALB (building loss)
for (hazard in hazard_vars_bldg) {
  for (yvar in c("inflow", "outflow", "netmig", "PI_change")) {
    reg_result <- run_reg(df, yvar, hazard)
    reg_result$loss_type <- "building"
    results[[length(results) + 1]] <- reg_result
  }
}

# 회귀 실행: EALP (population loss)
for (hazard in hazard_vars_pop) {
  for (yvar in c("inflow", "outflow", "netmig", "PI_change")) {
    reg_result <- run_reg(df, yvar, hazard)
    reg_result$loss_type <- "population"
    results[[length(results) + 1]] <- reg_result
  }
}

# 결과 데이터프레임으로 변환
final_results <- bind_rows(results) %>%
  select(loss_type, predictor, outcome, estimate, std.error, statistic, p.value, r_squared)

# 결과 확인
print(final_results, n = Inf)
## # A tibble: 68 × 8
##    loss_type predictor  outcome  estimate std.error statistic  p.value r_squared
##    <chr>     <chr>      <chr>       <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
##  1 building  NRI_AVLN_… inflow   1.50e+ 1  4.79e+ 0    3.13   1.86e- 3   2.55e-2
##  2 building  NRI_AVLN_… outflow  1.42e+ 1  5.95e+ 0    2.38   1.77e- 2   1.49e-2
##  3 building  NRI_AVLN_… netmig   8.37e- 1  2.25e+ 0    0.371  7.11e- 1   3.68e-4
##  4 building  NRI_AVLN_… PI_cha…  5.28e- 5  5.98e- 5    0.884  3.77e- 1   2.02e-3
##  5 building  NRI_CFLD_… inflow   4.45e- 4  7.72e- 5    5.77   1.69e- 8   8.15e-2
##  6 building  NRI_CFLD_… outflow  9.67e- 4  8.61e- 5   11.2    2.04e-25   2.52e-1
##  7 building  NRI_CFLD_… netmig  -5.22e- 4  2.60e- 5  -20.1    2.08e-61   5.18e-1
##  8 building  NRI_CFLD_… PI_cha…  1.25e- 9  9.95e-10    1.25   2.11e- 1   4.07e-3
##  9 building  NRI_CWAV_… inflow   1.01e+ 0  1.48e- 1    6.81   3.90e-11   1.10e-1
## 10 building  NRI_CWAV_… outflow  1.07e+ 0  1.86e- 1    5.75   1.87e- 8   8.10e-2
## 11 building  NRI_CWAV_… netmig  -6.01e- 2  7.29e- 2   -0.825  4.10e- 1   1.81e-3
## 12 building  NRI_CWAV_… PI_cha… -2.18e- 6  1.88e- 6   -1.16   2.47e- 1   3.47e-3
## 13 building  NRI_ERQK_… inflow   8.26e- 5  8.73e- 6    9.46   3.58e-19   1.93e-1
## 14 building  NRI_ERQK_… outflow  1.14e- 4  1.05e- 5   10.9    4.57e-24   2.39e-1
## 15 building  NRI_ERQK_… netmig  -3.11e- 5  4.22e- 6   -7.38   1.03e-12   1.27e-1
## 16 building  NRI_ERQK_… PI_cha…  7.86e-11  1.20e-10    0.655  5.13e- 1   1.11e-3
## 17 building  NRI_HAIL_… inflow   6.94e- 4  8.90e- 5    7.80   6.14e-14   1.40e-1
## 18 building  NRI_HAIL_… outflow  6.49e- 4  1.14e- 4    5.71   2.34e- 8   7.99e-2
## 19 building  NRI_HAIL_… netmig   4.56e- 5  4.45e- 5    1.02   3.06e- 1   2.79e-3
## 20 building  NRI_HAIL_… PI_cha… -1.23e- 9  1.18e- 9   -1.04   2.99e- 1   2.81e-3
## 21 building  NRI_HWAV_… inflow   3.14e- 2  1.75e- 2    1.79   7.38e- 2   8.50e-3
## 22 building  NRI_HWAV_… outflow  3.37e- 2  2.17e- 2    1.55   1.21e- 1   6.40e-3
## 23 building  NRI_HWAV_… netmig  -2.25e- 3  8.17e- 3   -0.275  7.83e- 1   2.02e-4
## 24 building  NRI_HWAV_… PI_cha… -7.89e- 8  2.17e- 7   -0.363  7.17e- 1   3.43e-4
## 25 building  NRI_HRCN_… inflow   7.50e- 5  1.06e- 5    7.10   6.13e-12   1.19e-1
## 26 building  NRI_HRCN_… outflow  9.93e- 5  1.29e- 5    7.69   1.27e-13   1.36e-1
## 27 building  NRI_HRCN_… netmig  -2.43e- 5  5.07e- 6   -4.80   2.30e- 6   5.79e-2
## 28 building  NRI_HRCN_… PI_cha…  3.14e-10  1.48e-10    2.13   3.41e- 2   1.16e-2
## 29 building  NRI_ISTM_… inflow   3.65e- 3  9.04e- 4    4.04   6.49e- 5   4.17e-2
## 30 building  NRI_ISTM_… outflow  5.02e- 3  1.11e- 3    4.52   8.22e- 6   5.17e-2
## 31 building  NRI_ISTM_… netmig  -1.37e- 3  4.23e- 4   -3.24   1.29e- 3   2.73e-2
## 32 building  NRI_ISTM_… PI_cha… -2.27e-10  1.14e- 8   -0.0200 9.84e- 1   1.04e-6
## 33 building  NRI_LNDS_… inflow   4.40e- 2  4.29e- 3   10.2    6.93e-22   2.19e-1
## 34 building  NRI_LNDS_… outflow  5.79e- 2  5.20e- 3   11.1    5.14e-25   2.48e-1
## 35 building  NRI_LNDS_… netmig  -1.39e- 2  2.14e- 3   -6.48   2.87e-10   1.01e-1
## 36 building  NRI_LNDS_… PI_cha…  5.40e- 8  5.97e- 8    0.903  3.67e- 1   2.11e-3
## 37 building  NRI_LTNG_… inflow   6.18e- 2  4.55e- 3   13.6    2.05e-34   3.29e-1
## 38 building  NRI_LTNG_… outflow  6.79e- 2  5.91e- 3   11.5    2.22e-26   2.60e-1
## 39 building  NRI_LTNG_… netmig  -6.06e- 3  2.56e- 3   -2.36   1.86e- 2   1.47e-2
## 40 building  NRI_LTNG_… PI_cha…  1.82e- 8  6.86e- 8    0.266  7.91e- 1   1.83e-4
## 41 building  NRI_RFLD_… inflow   1.65e- 4  3.13e- 5    5.27   2.30e- 7   6.90e-2
## 42 building  NRI_RFLD_… outflow  1.87e- 4  3.88e- 5    4.81   2.21e- 6   5.81e-2
## 43 building  NRI_RFLD_… netmig  -2.20e- 5  1.50e- 5   -1.46   1.44e- 1   5.68e-3
## 44 building  NRI_RFLD_… PI_cha… -3.19e-10  4.00e-10   -0.797  4.26e- 1   1.65e-3
## 45 building  NRI_SWND_… inflow   2.34e- 3  3.30e- 4    7.08   7.00e-12   1.18e-1
## 46 building  NRI_SWND_… outflow  3.63e- 3  3.92e- 4    9.27   1.51e-18   1.86e-1
## 47 building  NRI_SWND_… netmig  -1.29e- 3  1.49e- 4   -8.67   1.32e-16   1.67e-1
## 48 building  NRI_SWND_… PI_cha… -5.58e- 9  4.33e- 9   -1.29   1.98e- 1   4.30e-3
## 49 building  NRI_TRND_… inflow   1.15e- 3  8.19e- 5   14.0    2.63e-36   3.45e-1
## 50 building  NRI_TRND_… outflow  1.35e- 3  1.04e- 4   13.1    1.96e-32   3.13e-1
## 51 building  NRI_TRND_… netmig  -2.03e- 4  4.58e- 5   -4.45   1.15e- 5   5.01e-2
## 52 building  NRI_TRND_… PI_cha… -2.23e- 9  1.24e- 9   -1.79   7.35e- 2   8.30e-3
## 53 building  NRI_TSUN_… inflow   3.28e- 2  3.31e- 2    0.989  3.23e- 1   2.60e-3
## 54 building  NRI_TSUN_… outflow  4.83e- 2  4.09e- 2    1.18   2.39e- 1   3.70e-3
## 55 building  NRI_TSUN_… netmig  -1.55e- 2  1.54e- 2   -1.01   3.14e- 1   2.70e-3
## 56 building  NRI_TSUN_… PI_cha…  4.24e- 7  4.09e- 7    1.04   3.00e- 1   2.79e-3
## 57 building  NRI_VLCN_… inflow   1.27e- 3  3.44e- 4    3.69   2.59e- 4   3.50e-2
## 58 building  NRI_VLCN_… outflow  1.19e- 3  4.28e- 4    2.77   5.80e- 3   2.01e-2
## 59 building  NRI_VLCN_… netmig   8.07e- 5  1.62e- 4    0.496  6.20e- 1   6.57e-4
## 60 building  NRI_VLCN_… PI_cha…  1.20e- 9  4.32e- 9    0.279  7.81e- 1   2.02e-4
## 61 building  NRI_WFIR_… inflow   4.89e- 4  5.98e- 5    8.18   4.49e-15   1.51e-1
## 62 building  NRI_WFIR_… outflow  5.10e- 4  7.58e- 5    6.73   6.24e-11   1.08e-1
## 63 building  NRI_WFIR_… netmig  -2.09e- 5  3.01e- 5   -0.692  4.89e- 1   1.28e-3
## 64 building  NRI_WFIR_… PI_cha…  1.42e- 9  7.98e-10    1.78   7.66e- 2   8.12e-3
## 65 building  NRI_WNTW_… inflow   5.99e- 3  2.21e- 3    2.71   7.02e- 3   1.92e-2
## 66 building  NRI_WNTW_… outflow  7.98e- 3  2.72e- 3    2.93   3.58e- 3   2.24e-2
## 67 building  NRI_WNTW_… netmig  -2.00e- 3  1.03e- 3   -1.94   5.32e- 2   9.93e-3
## 68 building  NRI_WNTW_… PI_cha… -5.62e- 8  2.65e- 8   -2.12   3.48e- 2   1.15e-2