# Install and load the wooldridge package if not already installed
if (!requireNamespace("wooldridge", quietly = TRUE)) {
    install.packages("wooldridge")
}
library(wooldridge)

# Load the dataset using lowercase 'vote1'
data("vote1", package = "wooldridge")  # Try loading it explicitly by name

# Check if the dataset is loaded by viewing the first few rows
head(vote1)
##   state district democA voteA expendA expendB prtystrA lexpendA lexpendB
## 1    AL        7      1    68 328.296   8.737       41 5.793916 2.167567
## 2    AK        1      0    62 626.377 402.477       60 6.439952 5.997638
## 3    AZ        2      1    73  99.607   3.065       55 4.601233 1.120048
## 4    AZ        3      0    69 319.690  26.281       64 5.767352 3.268846
## 5    AR        3      0    75 159.221  60.054       66 5.070293 4.095244
## 6    AR        4      1    69 570.155  21.393       46 6.345908 3.063064
##     shareA
## 1 97.40767
## 2 60.88104
## 3 97.01476
## 4 92.40370
## 5 72.61247
## 6 96.38355
# Part (i): Define the model with interaction term
model <- lm(voteA ~ prtystrA + expendA + expendB + I(expendA * expendB), data = vote1)
summary(model)  # Display model summary to interpret coefficients
## 
## Call:
## lm(formula = voteA ~ prtystrA + expendA + expendB + I(expendA * 
##     expendB), data = vote1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -28.9999  -8.7632  -0.1726   8.2310  29.7325 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           3.212e+01  4.591e+00   6.995 5.99e-11 ***
## prtystrA              3.419e-01  8.799e-02   3.886 0.000146 ***
## expendA               3.828e-02  4.960e-03   7.718 1.00e-12 ***
## expendB              -3.172e-02  4.588e-03  -6.915 9.32e-11 ***
## I(expendA * expendB) -6.629e-06  7.186e-06  -0.923 0.357584    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.13 on 168 degrees of freedom
## Multiple R-squared:  0.5708, Adjusted R-squared:  0.5606 
## F-statistic: 55.86 on 4 and 168 DF,  p-value: < 2.2e-16
# Part (ii): Check statistical significance of interaction term
# Output is provided in the model summary above. Look at the p-value for "I(expendA * expendB)"

# Part (iii): Estimated effect of an additional $100,000 in expendB with expendA fixed at 300
expendA_fixed <- 300
effect_expendB <- coef(model)["expendB"] + coef(model)["I(expendA * expendB)"] * expendA_fixed
cat("Estimated effect of additional $100,000 in expendB:", effect_expendB, "\n")
## Estimated effect of additional $100,000 in expendB: -0.03371269
# Part (iv): Effect of increasing expendA by 100 with expendB fixed at 100
expendB_fixed <- 100
effect_expendA <- coef(model)["expendA"] + coef(model)["I(expendA * expendB)"] * expendB_fixed
cat("Estimated effect of increasing expendA by 100:", effect_expendA * 100, "\n")
## Estimated effect of increasing expendA by 100: 3.761799
# Part (v): Model with shareA instead of the interaction term
model_share <- lm(voteA ~ prtystrA + shareA, data = vote1)
summary(model_share)  # Display model summary for shareA model
## 
## Call:
## lm(formula = voteA ~ prtystrA + shareA, data = vote1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -17.7258  -3.7460  -0.0886   3.0517  30.7756 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 19.85013    2.41558   8.218 5.08e-14 ***
## prtystrA     0.15320    0.04962   3.087  0.00236 ** 
## shareA       0.45089    0.01480  30.475  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.231 on 170 degrees of freedom
## Multiple R-squared:  0.8638, Adjusted R-squared:  0.8622 
## F-statistic:   539 on 2 and 170 DF,  p-value: < 2.2e-16
# Part (vi): Partial effect of expendB on voteA, with expendA = 300 and expendB = 0
expendA_value <- 300
expendB_value <- 0
partial_effect_expendB <- coef(model)["expendB"] + coef(model)["I(expendA * expendB)"] * expendA_value
cat("Partial effect of expendB on voteA with expendA = 300 and expendB = 0:", partial_effect_expendB, "\n")
## Partial effect of expendB on voteA with expendA = 300 and expendB = 0: -0.03371269