Part 1
library(dplyr)
Warning: package ‘dplyr’ was built under R version 4.2.3
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
library(MatchIt)
Warning: package ‘MatchIt’ was built under R version 4.2.3
library(marginaleffects)
Warning: package ‘marginaleffects’ was built under R version 4.2.3Registered S3 method overwritten by 'data.table':
method from
print.data.table
library(car)
Loading required package: carData
Attaching package: ‘car’
The following object is masked from ‘package:dplyr’:
recode
set.seed(1337)
repData <- 1
repData[1:10000] <- sample(0:1, 10000, replace = TRUE, prob = c(.95, .05)) #Treatment-- Phone Call (1 or 2) and control
repData2 <- repData
numChange <- round(.35 * length(repData)) #These lines correlate the treatment with the voting column
indicesChange <- sample(1:length(repData), numChange)
repData2 <- ifelse(seq_len(length(repData2)) %in% indicesChange, 0, repData2) #Vote
repData3 <- 0
repData3[1:10000] <- sample(0:1, 10000, replace = TRUE, prob = c(.98, .02)) #Newly registered voter
repData4 <- 0
repData4[1:10000] <- sample(18:90, 10000, replace = TRUE) #Age
repData5 <- 0
repData5[1:10000] <- sample(0:1, 10000, replace = TRUE, prob = c(.45, .55)) #Female
fullData <- as.data.frame(cbind(repData, repData2, repData3, repData4, repData5))
cor(fullData$repData, fullData$repData2)
[1] 0.7855478
model <- matchit(data = fullData, repData ~ repData3 + repData4 + repData5, method = "exact", distance = "glm")
sumModel <- match.data(model)
model3 <- lm(repData2 ~ repData * (repData3 + repData4 + repData5), data = sumModel, weights = weights)
avg_comparisons(model3, variables = "repData", vcov = ~subclass, newdata = subset(sumModel, repData == 1), wts = "weights")
Warning: The `repData` variable is treated as a categorical (factor) variable, but the original data is of class numeric. It is safer and faster to convert such variables to factor
before fitting the model and calling `slopes` functions.
This warning appears once per session.
Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
Part 2
data <- read.csv("https://raw.githubusercontent.com/PrisonRodeo/PLSC504-2023-git/main/Exercises/PLSC504-2023-ExerciseThree.csv")
#murder ~ deathpen + urban + estbkpct + pop + aveeduc + aveideol
model1 <- lm(data = data, murder ~ deathpen + urban + estbkpct + pop + aveeduc + aveideol)
summary(model1)
Call:
lm(formula = murder ~ deathpen + urban + estbkpct + pop + aveeduc +
aveideol, data = data)
Residuals:
Min 1Q Median 3Q Max
-4.6026 -1.5837 -0.2067 1.2445 7.8281
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8.424e+00 3.180e+00 2.649 0.00833 **
deathpen 4.079e-01 2.720e-01 1.499 0.13448
urban 4.032e-02 9.157e-03 4.403 1.32e-05 ***
estbkpct 2.319e-01 1.318e-02 17.593 < 2e-16 ***
pop 1.779e-04 2.171e-05 8.193 2.30e-15 ***
aveeduc -5.430e-01 2.574e-01 -2.110 0.03539 *
aveideol -1.853e-02 7.568e-03 -2.448 0.01472 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.192 on 483 degrees of freedom
Multiple R-squared: 0.6452, Adjusted R-squared: 0.6408
F-statistic: 146.4 on 6 and 483 DF, p-value: < 2.2e-16
This linear regression model finds that the death penalty is not predictive of murder rate. How urban a state is, the state’s black population, and total population are the most predictive of murder rate… with each being a positive coefficient suggesting that when each is increased, murder increases. Average education and average ideology are less significant but still significant nonetheless.
model2 <- matchit(data = data, deathpen ~ urban + estbkpct + pop + aveeduc + aveideol, method = "full", distance = "glm")
sumModel <- match.data(model2)
model3 <- lm(murder ~ deathpen * (urban + estbkpct + pop + aveeduc + aveideol), data = sumModel, weights = weights)
avg_comparisons(model3, variables = "deathpen", vcov = ~subclass, newdata = subset(sumModel, deathpen == 1), wts = "weights")
Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
My results suggest that the deathpenalty has a -3.29 effect on the murder rate.
Unsure how to test the model other than by checking the balance. It could also be tested by varying the method of matching used (as a sensitivity analysis).
data$lowcont[which(data$lowcont < 1)] <- 0
model2 <- matchit(data = data, lowcont ~ pop + aveeduc + urban, method = "full", distance = "glm")
sumModel <- match.data(model2)
model3 <- lm(murder ~ lowcont * (urban + pop + aveeduc), data = sumModel, weights = weights)
avg_comparisons(model3, variables = "lowcont", vcov = ~subclass, newdata = subset(sumModel, lowcont == 1), wts = "weights")
Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
The first result shows that murder rate increases if the House is Democratic.
data$upcont[which(data$upcont < 1)] <- 0
model2 <- matchit(data = data, upcont ~ pop + aveeduc + urban, method = "full", distance = "glm")
summary(model2)
Call:
matchit(formula = upcont ~ pop + aveeduc + urban, data = data,
method = "full", distance = "glm")
Summary of Balance for All Data:
Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean eCDF Max
distance 0.6919 0.5800 0.7002 1.2710 0.1996 0.2877
pop 5152.5406 4812.0882 0.0611 1.2163 0.1009 0.2301
aveeduc 12.6881 13.0339 -0.6741 1.9839 0.1961 0.2849
urban 68.0397 70.6018 -0.1681 1.3822 0.0658 0.1559
Summary of Balance for Matched Data:
Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean eCDF Max Std. Pair Dist.
distance 0.6919 0.6839 0.0498 1.1099 0.0130 0.1719 0.0569
pop 5152.5406 6917.1568 -0.3165 1.0581 0.1070 0.3568 1.0550
aveeduc 12.6881 12.7435 -0.1079 1.4126 0.0175 0.1813 0.1714
urban 68.0397 70.8816 -0.1864 1.9845 0.1016 0.2557 0.9779
Sample Sizes:
Control Treated
All 170. 320
Matched (ESS) 22.99 320
Matched 170. 320
Unmatched 0. 0
Discarded 0. 0
sumModel <- match.data(model2)
head(sumModel)
model3 <- lm(murder ~ upcont * (urban + pop + aveeduc), data = sumModel, weights = weights)
summary(model3)
Call:
lm(formula = murder ~ upcont * (urban + pop + aveeduc), data = sumModel,
weights = weights)
Weighted Residuals:
Min 1Q Median 3Q Max
-9.5174 -1.6910 -0.3294 1.5292 12.1929
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.114e+01 8.010e+00 -1.391 0.164828
upcont 5.470e+01 9.007e+00 6.073 2.54e-09 ***
urban 1.475e-01 2.607e-02 5.655 2.67e-08 ***
pop 1.270e-04 5.766e-05 2.203 0.028084 *
aveeduc 4.671e-01 6.677e-01 0.700 0.484505
upcont:urban -1.041e-01 2.918e-02 -3.567 0.000397 ***
upcont:pop 1.428e-04 6.617e-05 2.158 0.031457 *
upcont:aveeduc -3.660e+00 7.530e-01 -4.860 1.59e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.855 on 482 degrees of freedom
Multiple R-squared: 0.3799, Adjusted R-squared: 0.3709
F-statistic: 42.18 on 7 and 482 DF, p-value: < 2.2e-16
avg_comparisons(model3, variables = "upcont", vcov = ~subclass, newdata = subset(sumModel, upcont == 1), wts = "weights")
Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
The second result shows that murder rate increases if the Senate is Democratic.