This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Load necessary libraries
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
# Simulate data for VOTEl
set.seed(123) # For reproducibility
n <- 100 # Assuming a sample size of 100 observations
VOTEl <- data.frame(
voteA = runif(n, 40, 60), # Simulated vote share for Candidate A (%)
prtystrA = runif(n, 0, 10), # Simulated party strength for Candidate A (scale 0-10)
expendA = runif(n, 100, 500), # Simulated expenditures by Candidate A (in $100,000s)
expendB = runif(n, 100, 500) # Simulated expenditures by Candidate B (in $100,000s)
)
# 1. Estimate the model with an interaction term between expenditures
model <- lm(voteA ~ prtystrA + expendA + expendB + I(expendA * expendB), data = VOTEl)
# Display model summary
summary(model)
##
## Call:
## lm(formula = voteA ~ prtystrA + expendA + expendB + I(expendA *
## expendB), data = VOTEl)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.0431 -4.3506 -0.6155 4.8840 10.1268
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.301e+01 4.526e+00 11.711 <2e-16 ***
## prtystrA -2.111e-01 2.208e-01 -0.956 0.341
## expendA -2.815e-03 1.343e-02 -0.210 0.834
## expendB -3.009e-03 1.415e-02 -0.213 0.832
## I(expendA * expendB) -2.637e-06 4.354e-05 -0.061 0.952
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.761 on 95 degrees of freedom
## Multiple R-squared: 0.01986, Adjusted R-squared: -0.02141
## F-statistic: 0.4813 on 4 and 95 DF, p-value: 0.7494
# 2. Calculate partial effect of expendB on voteA, holding prtystrA and expendA fixed
beta3 <- coef(model)["expendB"]
beta4 <- coef(model)["I(expendA * expendB)"]
# Example: Calculate partial effect when expendA is at its mean
expendA_mean <- mean(VOTEl$expendA)
partial_effect_expendB <- beta3 + beta4 * expendA_mean
cat("Partial effect of expendB on voteA, holding expendA at its mean:", partial_effect_expendB, "\n")
## Partial effect of expendB on voteA, holding expendA at its mean: -0.003785184
# 3. Partial effect of expendA on voteA, holding prtystrA and expendB fixed
beta2 <- coef(model)["expendA"]
# Example: Calculate partial effect when expendB = 100
expendB_fixed <- 100
partial_effect_expendA <- beta2 + beta4 * expendB_fixed
cat("Partial effect of expendA on voteA, holding expendB at 100:", partial_effect_expendA, "\n")
## Partial effect of expendA on voteA, holding expendB at 100: -0.003079116
# 4. Check the statistical significance of the interaction term
interaction_significance <- summary(model)$coefficients["I(expendA * expendB)", "Pr(>|t|)"]
cat("P-value for interaction term:", interaction_significance, "\n")
## P-value for interaction term: 0.9518243
# 5. Fix expendA at 300 and calculate effect of another $100,000 by Candidate B on voteA
expendA_fixed <- 300
increment_B <- 100
effect_B <- (beta3 + beta4 * expendA_fixed) * increment_B
cat("Effect on voteA of another $100,000 spent by Candidate B, holding expendA at 300:", effect_B, "\n")
## Effect on voteA of another $100,000 spent by Candidate B, holding expendA at 300: -0.3799899
# 6. Fix expendB at 100 and calculate effect of an additional $100,000 by Candidate A on voteA
increment_A <- 100
effect_A <- (beta2 + beta4 * expendB_fixed) * increment_A
cat("Effect on voteA of another $100,000 spent by Candidate A, holding expendB at 100:", effect_A, "\n")
## Effect on voteA of another $100,000 spent by Candidate A, holding expendB at 100: -0.3079116
# 7. Estimate a model with shareA (percentage share of total expenditures by Candidate A) instead of interaction
VOTEl <- VOTEl %>%
mutate(total_expend = expendA + expendB,
shareA = ifelse(total_expend > 0, expendA / total_expend, 0))
model_share <- lm(voteA ~ prtystrA + expendA + expendB + shareA, data = VOTEl)
# Display the model with shareA
summary(model_share)
##
## Call:
## lm(formula = voteA ~ prtystrA + expendA + expendB + shareA, data = VOTEl)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.0319 -4.4484 -0.5861 5.0698 10.2568
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.03025 9.86030 4.668 9.96e-06 ***
## prtystrA -0.22451 0.22083 -1.017 0.312
## expendA -0.01679 0.01819 -0.923 0.358
## expendB 0.00871 0.01730 0.504 0.616
## shareA 15.00516 19.86797 0.755 0.452
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.743 on 95 degrees of freedom
## Multiple R-squared: 0.02567, Adjusted R-squared: -0.01535
## F-statistic: 0.6258 on 4 and 95 DF, p-value: 0.6452
# 8. Calculate the partial effect of expendB on voteA in the shareA model, holding prtystrA and expendA fixed
beta_share3 <- coef(model_share)["expendB"]
beta_share4 <- coef(model_share)["shareA"]
# Example calculation at expendA = 300, expendB = 0
expendA_fixed <- 300
expendB_fixed <- 0
total_expend_fixed <- expendA_fixed + expendB_fixed
partial_effect_expendB_share <- beta_share3 - beta_share4 * expendA_fixed / (total_expend_fixed^2)
cat("Partial effect of expendB on voteA in the shareA model, holding expendA at 300 and expendB at 0:", partial_effect_expendB_share, "\n")
## Partial effect of expendB on voteA in the shareA model, holding expendA at 300 and expendB at 0: -0.0413075