# Load necessary libraries
library(wooldridge)  # Contains datasets like VOTE1
library(tidyverse)   # For data manipulation
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lmtest)      # For statistical tests
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# Load the VOTE1 dataset from the wooldridge package
data("vote1")

# (i) Define the model with interaction between expendA and expendB
model <- lm(voteA ~ prtystrA + expendA + expendB + I(expendA * expendB), data = vote1)

# Display summary of the model
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
# Calculate partial effects as described in part (i)
# Partial effect of expendB on voteA, holding prtystrA and expendA fixed
partial_effect_expendB <- coef(model)["expendB"] + coef(model)["I(expendA * expendB)"] * mean(vote1$expendA)

# Partial effect of expendA on voteA, holding prtystrA and expendB fixed
partial_effect_expendA <- coef(model)["expendA"] + coef(model)["I(expendA * expendB)"] * mean(vote1$expendB)

cat("Partial effect of expendB on voteA:", partial_effect_expendB, "\n")
## Partial effect of expendB on voteA: -0.03378304
cat("Partial effect of expendA on voteA:", partial_effect_expendA, "\n")
## Partial effect of expendA on voteA: 0.03625836
# (ii) Check if the interaction term is statistically significant
# The interaction term expendA:expendB is statistically significant if p-value < 0.05
summary(model)$coefficients
##                           Estimate   Std. Error    t value     Pr(>|t|)
## (Intercept)           3.211738e+01 4.591152e+00  6.9954948 5.992114e-11
## prtystrA              3.419398e-01 8.799291e-02  3.8859926 1.464195e-04
## expendA               3.828094e-02 4.959966e-03  7.7179845 1.003489e-12
## expendB              -3.172384e-02 4.587537e-03 -6.9152235 9.322789e-11
## I(expendA * expendB) -6.629499e-06 7.186348e-06 -0.9225129 3.575841e-01
# (iii) Find the average of expendA and calculate effect of $100,000 increase in expendB
average_expendA <- mean(vote1$expendA)
effect_expendB_increase <- coef(model)["expendB"] + coef(model)["I(expendA * expendB)"] * 300  # expendA fixed at 300

cat("Estimated effect of $100,000 increase in expendB when expendA is 300:", effect_expendB_increase, "\n")
## Estimated effect of $100,000 increase in expendB when expendA is 300: -0.03371269
# (iv) Set expendB to 100 and calculate the estimated effect of a $100 increase in expendA on voteA
effect_expendA_increase <- coef(model)["expendA"] + coef(model)["I(expendA * expendB)"] * 100  # expendB fixed at 100

cat("Estimated effect of $100 increase in expendA when expendB is 100:", effect_expendA_increase, "\n")
## Estimated effect of $100 increase in expendA when expendB is 100: 0.03761799
# (v) Estimate model with shareA (percentage share of total expenditures)
# Assuming 'shareA' is the percentage of Candidate A's campaign expenditure
vote1 <- vote1 %>% mutate(shareA = expendA / (expendA + expendB))  # Calculate shareA
model_shareA <- lm(voteA ~ prtystrA + expendA + expendB + shareA, data = vote1)

# Display summary of the new model
summary(model_shareA)
## 
## Call:
## lm(formula = voteA ~ prtystrA + expendA + expendB + shareA, data = vote1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.5859  -3.3764  -0.3036   3.2095  31.5656 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 18.195374   2.567856   7.086 3.63e-11 ***
## prtystrA     0.157272   0.049685   3.165  0.00184 ** 
## expendA     -0.006670   0.002833  -2.354  0.01971 *  
## expendB      0.004267   0.002607   1.637  0.10351    
## shareA      49.439436   2.530859  19.535  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.167 on 168 degrees of freedom
## Multiple R-squared:  0.8681, Adjusted R-squared:  0.865 
## F-statistic: 276.5 on 4 and 168 DF,  p-value: < 2.2e-16
# (vi) Calculate partial effect of expendB on voteA, with expendA fixed at 300 and expendB at 0
expendA_fixed <- 300
expendB_fixed <- 0
partial_effect_expendB_calculus <- coef(model)["expendB"] + coef(model)["I(expendA * expendB)"] * expendA_fixed

cat("Partial effect of expendB on voteA with expendA = 300 and expendB = 0:", partial_effect_expendB_calculus, "\n")
## Partial effect of expendB on voteA with expendA = 300 and expendB = 0: -0.03371269