# Load necessary library
library(readr)
# Load the dataset
data <- read_csv("/Users/chenkailu/Desktop/us_fred_coastal_us_states_avg_hpi_before_after_2005.csv")
## Rows: 48 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): STATE
## dbl (5): HPI_CHG, Time_Period, Disaster_Affected, NUM_DISASTERS, NUM_IND_ASSIST
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Create the interaction term
data$Interaction <- with(data, Time_Period * Disaster_Affected)
# Run the linear regression model
model <- lm(HPI_CHG ~ Time_Period + Disaster_Affected + Interaction, data = data)
# Print the summary of the model
summary(model)
##
## Call:
## lm(formula = HPI_CHG ~ Time_Period + Disaster_Affected + Interaction,
## data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.023081 -0.007610 -0.000171 0.004656 0.035981
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.037090 0.002819 13.157 < 2e-16 ***
## Time_Period -0.027847 0.003987 -6.985 1.2e-08 ***
## Disaster_Affected -0.013944 0.006176 -2.258 0.0290 *
## Interaction 0.019739 0.008734 2.260 0.0288 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01229 on 44 degrees of freedom
## Multiple R-squared: 0.5356, Adjusted R-squared: 0.504
## F-statistic: 16.92 on 3 and 44 DF, p-value: 1.882e-07
Control Group: States not affected by a disaster before the event or intervention.
Treatment Group: States affected by a disaster.
The “control” is the scenario of not being affected by a disaster or being in the before period, while the “treatment” is being affected by a disaster and/or moving into the after period.
The coefficient for Time_Period is
-0.0278, indicating that, on average, states experience a decrease in
HPI_CHG in the period after the event or intervention, holding other
variables constant.
The coefficient for
Disaster_Affected is -0.0139, suggesting that
being affected by a disaster is associated with a decrease in HPI_CHG,
holding other variables constant.
The coefficient for the interaction term between
Time_Period and
Disaster_Affected is
0.0197, indicating that the effect of being affected by
a disaster on HPI_CHG changes in the period after the event or
intervention. Specifically, the negative impact of disasters on HPI_CHG
is lessened in the after period.
This methodology works because it compares changes over time within the same group and across different groups, allowing us to isolate the specific impact of being affected by a disaster. By analyzing the differences in differences, we aim to eliminate biases that could arise from comparing disparate groups or from changes that would have happened over time regardless of the disaster.
# Calculate and print the means for the 2x2 matrix
means_table <- with(data, tapply(HPI_CHG, list(Disaster_Affected, Time_Period), mean))
print(means_table)
## 0 1
## 0 0.03709002 0.009242792
## 1 0.02314612 0.015038346
# Manually calculate the difference in differences
diff_in_diff_manual <- (means_table[2, 2] - means_table[2, 1]) - (means_table[1, 2] - means_table[1, 1])
print(diff_in_diff_manual)
## [1] 0.01973946
From this, the manually calculated difference-in-difference effect is approximately 0.0197. This match the result in the linear regression above.
The key to trusting the results of this kind of analysis lies in several assumptions, the most crucial being the parallel trends assumption. This assumption posits that, in the absence of the treatment (being affected by a disaster in this case), the treatment and control groups would have followed parallel paths over time in terms of their outcome variable (HPI_CHG).
If the parallel trends assumption holds, we can be more confident in our results. However, if this assumption does not hold—if the groups were on diverging or converging paths irrespective of the treatment—then our estimates of the treatment effect might be biased. Another threat could be from variables that change over time in a way that differentially affects the groups but is not controlled for in the model.
To trust the study’s results, we should believe that:
The groups would have followed similar trends in the absence of the disaster.
There are no unobserved factors changing over time that differentially affect the groups’ HPI_CHG.
The observed effects are truly attributable to the disaster and not to other concurrent events or changes.