library(readr)
library(psych)
library(lme4)
## Loading required package: Matrix
library(lmerTest)
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
d <-  read.csv (file.choose()) 

# continuous party
d$partyCont <- NA
d$partyCont[d$demStrength == 1] <- -3
d$partyCont[d$demStrength == 2] <- -2
d$partyCont[d$partyClose== 1] <- -1
d$partyCont[d$partyClose == 3] <- 0
d$partyCont[d$repStrength == 1] <- 3
d$partyCont[d$repStrength == 2] <- 2
d$partyCont[d$partyClose == 2] <- 1


# party factor
d$party_factor <- NA
d$party_factor[d$partyCont < 0] <- 'Democrat'
d$party_factor[d$partyCont == 0] <- 'Independent'
d$party_factor[d$partyCont > 0] <- 'Republican'


## Order of timing var
d$election_timing <- factor(d$election_timing, levels = c('Pre-election', 'During-election','Post-election'))

d$party_factor <- factor(d$party_factor, levels = c('Democrat', 'Republican','Independent'))


### Party Factor

d$pDem_Rep <- NA
d$pDem_Rep[d$party_factor == 'Democrat'] <- -.5
d$pDem_Rep[d$party_factor == 'Independent'] <- 0
d$pDem_Rep[d$party_factor == 'Republican'] <- .5

d$pInd_Not <- NA
d$pInd_Not[d$party_factor == 'Democrat'] <- .33
d$pInd_Not[d$party_factor == 'Independent'] <- -.67
d$pInd_Not[d$party_factor == 'Republican'] <- .33

#dummy codes for party  ID

d$pDemR[d$party_factor == 'Democrat'] <- 0
d$pDemR[d$party_factor == 'Republican'] <- 1
d$pDemR[d$party_factor == 'Independent'] <- 0

d$pDemI[d$party_factor == 'Democrat'] <- 0
d$pDemI[d$party_factor == 'Republican'] <- 0
d$pDemI[d$party_factor == 'Independent'] <- 1


d$pRepD[d$party_factor == 'Democrat'] <- 1
d$pRepD[d$party_factor == 'Republican'] <- 0
d$pRepD[d$party_factor == 'Independent'] <- 0

d$pRepI[d$party_factor == 'Democrat'] <- 0
d$pRepI[d$party_factor == 'Republican'] <- 0
d$pRepI[d$party_factor == 'Independent'] <- 1

d$IndR[d$party_factor == 'Democrat'] <- 0
d$IndR[d$party_factor == 'Republican'] <- 1
d$IndR[d$party_factor == 'Independent'] <- 0

d$IndD[d$party_factor == 'Democrat'] <- 1
d$IndD[d$party_factor == 'Republican'] <- 0
d$IndD[d$party_factor == 'Independent'] <- 0


## negative emotions
d$negEmo <- (d$emotion_1 + d$emotion_3 + d$emotion_8 + d$emotion_9 + d$emotion_12)/5
d$negEmo.c <- d$negEmo - mean(d$negEmo, na.rm = T)

d2 <- d[d$election_timing != "Pre-election",]


### Timing codes
## Contrast
d2$tDur_Post <- NA
d2$tDur_Post[d2$election_timing == 'During-election'] <- -.5
d2$tDur_Post[d2$election_timing == 'Post-election'] <- .5


# Dummy
# Post!
d2$tDurD <- NA
d2$tDurD[d2$election_timing == 'During-election'] <- 0
d2$tDurD[d2$election_timing == 'Post-election'] <- 1

# During!
d2$tPostD <- NA
d2$tPostD[d2$election_timing == 'During-election'] <- 1
d2$tPostD[d2$election_timing == 'Post-election'] <- 0

negative emotions

main model

negEmotion.m <- lm(negEmo.c ~ (pDem_Rep + pInd_Not) * (tDur_Post), data = d2)
summary(negEmotion.m)
## 
## Call:
## lm(formula = negEmo.c ~ (pDem_Rep + pInd_Not) * (tDur_Post), 
##     data = d2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5920 -1.3920 -0.2589  1.1411  4.3456 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        -0.12636    0.05225  -2.418   0.0157 *  
## pDem_Rep            0.06104    0.10288   0.593   0.5531    
## pInd_Not            0.53800    0.12965   4.150 3.56e-05 ***
## tDur_Post          -0.16826    0.10450  -1.610   0.1076    
## pDem_Rep:tDur_Post  1.08933    0.20576   5.294 1.42e-07 ***
## pInd_Not:tDur_Post -0.06123    0.25930  -0.236   0.8134    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.635 on 1208 degrees of freedom
##   (28 observations deleted due to missingness)
## Multiple R-squared:  0.04018,    Adjusted R-squared:  0.03621 
## F-statistic: 10.11 on 5 and 1208 DF,  p-value: 1.671e-09

dummy coded for dem

negEmotion.D <- lm(negEmo.c ~ (pDemR + pDemI) * (tDur_Post), data = d2)
summary(negEmotion.D)
## 
## Call:
## lm(formula = negEmo.c ~ (pDemR + pDemI) * (tDur_Post), data = d2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5920 -1.3920 -0.2589  1.1411  4.3456 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.02066    0.06945   0.297 0.766177    
## pDemR            0.06104    0.10288   0.593 0.553113    
## pDemI           -0.50748    0.13779  -3.683 0.000241 ***
## tDur_Post       -0.73313    0.13889  -5.278 1.54e-07 ***
## pDemR:tDur_Post  1.08933    0.20576   5.294 1.42e-07 ***
## pDemI:tDur_Post  0.60589    0.27557   2.199 0.028092 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.635 on 1208 degrees of freedom
##   (28 observations deleted due to missingness)
## Multiple R-squared:  0.04018,    Adjusted R-squared:  0.03621 
## F-statistic: 10.11 on 5 and 1208 DF,  p-value: 1.671e-09

dummy coded for rep

negEmotion.R <- lm(negEmo.c ~ (pRepD + pRepI) * (tDur_Post), data = d2)
summary(negEmotion.R)
## 
## Call:
## lm(formula = negEmo.c ~ (pRepD + pRepI) * (tDur_Post), data = d2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5920 -1.3920 -0.2589  1.1411  4.3456 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.08169    0.07591   1.076   0.2820    
## pRepD           -0.06104    0.10288  -0.593   0.5531    
## pRepI           -0.56851    0.14115  -4.028 5.99e-05 ***
## tDur_Post        0.35620    0.15182   2.346   0.0191 *  
## pRepD:tDur_Post -1.08933    0.20576  -5.294 1.42e-07 ***
## pRepI:tDur_Post -0.48344    0.28231  -1.712   0.0871 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.635 on 1208 degrees of freedom
##   (28 observations deleted due to missingness)
## Multiple R-squared:  0.04018,    Adjusted R-squared:  0.03621 
## F-statistic: 10.11 on 5 and 1208 DF,  p-value: 1.671e-09

dummy coded for independent

negEmotion.I <- lm(negEmo.c ~ (IndR + IndD) * (tDur_Post), data = d2)
summary(negEmotion.I)
## 
## Call:
## lm(formula = negEmo.c ~ (IndR + IndD) * (tDur_Post), data = d2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5920 -1.3920 -0.2589  1.1411  4.3456 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -0.4868     0.1190  -4.091 4.59e-05 ***
## IndR             0.5685     0.1412   4.028 5.99e-05 ***
## IndD             0.5075     0.1378   3.683 0.000241 ***
## tDur_Post       -0.1272     0.2380  -0.535 0.593028    
## IndR:tDur_Post   0.4834     0.2823   1.712 0.087070 .  
## IndD:tDur_Post  -0.6059     0.2756  -2.199 0.028092 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.635 on 1208 degrees of freedom
##   (28 observations deleted due to missingness)
## Multiple R-squared:  0.04018,    Adjusted R-squared:  0.03621 
## F-statistic: 10.11 on 5 and 1208 DF,  p-value: 1.671e-09

means

print("means democrat")
## [1] "means democrat"
dDem <- d2[d2$party_factor == "Democrat",]
mean(dDem$negEmo[dDem$election_timing == "During-election"], na.rm = T)
## [1] 3.592032
mean(dDem$negEmo[dDem$election_timing == "Post-election"], na.rm = T)
## [1] 2.8589
print("means republican")
## [1] "means republican"
dRep <- d2[d2$party_factor == "Republican",]
mean(dRep$negEmo[dRep$election_timing == "During-election"], na.rm = T)
## [1] 3.108403
mean(dRep$negEmo[dRep$election_timing == "Post-election"], na.rm = T)
## [1] 3.464602
print("means independent")
## [1] "means independent"
dInd <- d2[d2$party_factor == "Independent",]
mean(dInd$negEmo[dInd$election_timing == "During-election"], na.rm = T)
## [1] 2.781609
mean(dInd$negEmo[dInd$election_timing == "Post-election"], na.rm = T)
## [1] 2.654369

decomposing anger interaction


Model: Dem vs Rep interegnum There is no sig difference between Dems and Reps during the election, t(1209) = -1.30, p = 0.194.


anger.m <- lm(emotion_1 ~ (pDem_Rep + pInd_Not) * (tDurD), data = d2)
summary(anger.m)
## 
## Call:
## lm(formula = emotion_1 ~ (pDem_Rep + pInd_Not) * (tDurD), data = d2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7345 -1.7217 -0.6295  1.3705  4.4138 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     3.20523    0.09448  33.923  < 2e-16 ***
## pDem_Rep       -0.23873    0.18349  -1.301    0.194    
## pInd_Not        0.92391    0.23600   3.915 9.55e-05 ***
## tDurD          -0.14125    0.12950  -1.091    0.276    
## pDem_Rep:tDurD  1.25156    0.25531   4.902 1.08e-06 ***
## pInd_Not:tDurD -0.42658    0.32113  -1.328    0.184    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.028 on 1209 degrees of freedom
##   (27 observations deleted due to missingness)
## Multiple R-squared:  0.04703,    Adjusted R-squared:  0.04309 
## F-statistic: 11.93 on 5 and 1209 DF,  p-value: 2.711e-11

Model: Dem vs Rep declared There is a significant difference between Dems and Reps such that Reps are 1.01 units angrier then Dems, t(1209) = 5.71, p<.001.


anger.m <- lm(emotion_1 ~ (pDem_Rep + pInd_Not) * (tPostD), data = d2)
summary(anger.m)
## 
## Call:
## lm(formula = emotion_1 ~ (pDem_Rep + pInd_Not) * (tPostD), data = d2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7345 -1.7217 -0.6295  1.3705  4.4138 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      3.06398    0.08856  34.597  < 2e-16 ***
## pDem_Rep         1.01283    0.17752   5.706 1.46e-08 ***
## pInd_Not         0.49733    0.21778   2.284   0.0226 *  
## tPostD           0.14125    0.12950   1.091   0.2756    
## pDem_Rep:tPostD -1.25156    0.25531  -4.902 1.08e-06 ***
## pInd_Not:tPostD  0.42658    0.32113   1.328   0.1843    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.028 on 1209 degrees of freedom
##   (27 observations deleted due to missingness)
## Multiple R-squared:  0.04703,    Adjusted R-squared:  0.04309 
## F-statistic: 11.93 on 5 and 1209 DF,  p-value: 2.711e-11