# Load necessary package
library(wooldridge)

# Load the VOTE1 dataset
data("vote1")

# Part (i): Define and estimate the model with an interaction term
model <- lm(voteA ~ prtystrA + expendA + expendB + I(expendA * expendB), data = vote1)
summary(model)
## 
## 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
# Extract the coefficients for interpretation
coefficients <- coef(model)
beta_2 <- coefficients["expendA"]
beta_3 <- coefficients["expendB"]
beta_4 <- coefficients["I(expendA * expendB)"]

# (i) Partial effect of expendB on voteA, holding prtystrA and expendA fixed
# Partial effect of expendB = β3 + β4 * expendA
partial_effect_expendB <- function(expendA) {
  beta_3 + beta_4 * expendA
}

# Example: Calculate partial effect at expendA = 300
partial_effect_expendB(300)
##     expendB 
## -0.03371269
# (ii) Check if the interaction term (expendA * expendB) is statistically significant
summary(model)$coefficients["I(expendA * expendB)", "Pr(>|t|)"]
## [1] 0.3575841
# (iii) Find the average of expendA and estimate effect of another $100,000 spent by Candidate B
avg_expendA <- mean(vote1$expendA, na.rm = TRUE)
# Calculate effect of $100,000 increase in expendB at expendA = 300
effect_100k_expendB <- partial_effect_expendB(300) * 100  # For $100,000 increment
effect_100k_expendB
##   expendB 
## -3.371269
# (iv) Fix expendB at 100 and estimate effect of ΔexpendA = 100 on voteA
# Partial effect of expendA = β2 + β4 * expendB
partial_effect_expendA <- function(expendB) {
  beta_2 + beta_4 * expendB
}

# Calculate effect at expendB = 100 with ΔexpendA = 100
effect_delta_expendA <- partial_effect_expendA(100) * 100
effect_delta_expendA
##  expendA 
## 3.761799
# (v) Model with shareA (percentage of total expenditures by Candidate A)
vote1$shareA <- vote1$expendA / (vote1$expendA + vote1$expendB)
model_share <- lm(voteA ~ prtystrA + shareA, data = vote1)
summary(model_share)
## 
## 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      45.08931    1.47955  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
# (vi) Calculate partial effect of expendB on voteA with prtystrA and expendA fixed
# Partial effect of expendB = β3 + β4 * expendA
# Evaluate this at expendA = 300 and expendB = 0
partial_effect_expendB(300)
##     expendB 
## -0.03371269