Hi all.
A couple of things
Wonderful, wonderful attempts at the first lab! This is precisely what I was going for!
Working in a group was hopefully a pleasant experience. If at all possible, maybe shuffle group membership before the next lab so that we can have people of different R experience sorted between groups.
Our class Slack has been a good resource for folks looking for the source of problems. But it doesn’t work best when you ask a question like
Instead, it’s best when you provide a very specific example of what you’ve tried and then failed to do, the actual code you’ve used to generate an error.
This is key – provide enough code so that your interlocutor can make it to the same point where you’re current stuck. That way, whatever solution your interlocutor finds, will also work for you.
What’s another way to think of this? You’re trying to provide a minimal reproducible example to your interlocutor so that they can help you in a way
So this question asks that we report the distribution of perceptions that global climates have increased in recent years. The ANES 2020 variable list will be your friend here. First, we load the necessary libraries.
library(tidyverse)
library(magrittr)
(If you hit any errors at this point, typically of the kind Error in library(magrittr) : there is no package called 'magrittr'
, this means you need to install the package first, with install.packages("magrittr")
Then, we download the data.frame
which contains the 2020 ANES
d1 <- "https://github.com/thomasjwood/ps4160/raw/master/anes_timeseries_2020.rds" %>%
url %>%
gzcon %>%
readRDS
Then we generate a nice outcome variable which throws away the
d1$world_temp <- d1$V202555
d1$world_temp[
d1$V202555 %>%
is_in(
c("1. World temperatures have risen on average over the last 100 years",
"2. World temperatures have not risen on average over the last 100 years")
) %>%
not
] <- NA
d1$world_temp <- d1$world_temp %>%
factor
Did this work?
d1$world_temp %>%
table %>%
prop.table %>%
round(2)
## .
## 1. World temperatures have risen on average over the last 100 years
## 0.88
## 2. World temperatures have not risen on average over the last 100 years
## 0.12
Yay! what a nice informative table.
88% of the American public have the descriptive data right–that global temperatures have risen over the last 100 years.
Now we have to tabulate education and partisanship against climate change belief. Let’s make some nice clean party and education variables:
d1$educ <- NA
d1$educ[d1$V201510 %>%
is_in(
d1$V201510 %>%
levels %>%
extract(c(3:4))
)
] <- "HSD or less"
d1$educ[d1$V201510 %>%
is_in(
d1$V201510 %>%
levels %>%
extract(c(5:7))
)
] <- "Some college"
d1$educ[d1$V201510 %>%
is_in(
d1$V201510 %>%
levels %>%
extract(c(8:10))
)
] <- "BA or more"
d1$educ %<>%
factor(
c("HSD or less",
"Some college",
"BA or more")
)
d1$party <- NA
d1$party[
d1$V201231x %>%
is_in(
d1$V201231x %>%
levels %>%
extract(3:5)
)
] <- "Democrat"
d1$party[
d1$V201231x %>%
is_in(
d1$V201231x %>%
levels %>%
extract(6)
)
] <- "Independent"
d1$party[
d1$V201231x %>%
is_in(
d1$V201231x %>%
levels %>%
extract(7:9)
)
] <- "Republican"
Now we’ll tab these variables against climate change
xtabs( ~ world_temp + educ + party, data = d1) %>%
ftable(
col.vars = "world_temp"
) %>%
prop.table(1) %>%
round(2)
## world_temp 1. World temperatures have risen on average over the last 100 years 2. World temperatures have not risen on average over the last 100 years
## educ party
## HSD or less Democrat 0.89 0.11
## Independent 0.81 0.19
## Republican 0.74 0.26
## Some college Democrat 0.95 0.05
## Independent 0.89 0.11
## Republican 0.78 0.22
## BA or more Democrat 0.99 0.01
## Independent 0.93 0.07
## Republican 0.83 0.17
nicely formatted as follows
It seems that–education improves scientific understanding, all along the partisan spectrum, and partisanship effects are pretty stable, (ie, they only weakly interact with education.)
Let’s check the immigration attitudes relationship with vote choice, and see if it interacts with education.
First, we create some nice summary cleaned up variables for immigration.
d1$immig_econ <- NA
d1$immig_econ[
d1$V202418 %>%
is_in(
d1$V202418 %>%
levels %>%
extract(6:7)
)
] <- "1. Agree"
d1$immig_econ[
d1$V202418 %>%
is_in(
d1$V202418 %>%
levels %>%
extract(8)
)
] <- "2. Neither agree nor disagree"
d1$immig_econ[
d1$V202418 %>%
is_in(
d1$V202418 %>%
levels %>%
extract(9:10)
)
] <- "3. Disagree"
d1$immig_culture <- NA
d1$immig_culture[
d1$V202419 %>%
is_in(
d1$V202419 %>%
levels %>%
extract(6:7)
)
] <- "1. Agree"
d1$immig_culture[
d1$V202419 %>%
is_in(
d1$V202419 %>%
levels %>%
extract(8)
)
] <- "2. Neither agree nor disagree"
d1$immig_culture[
d1$V202419 %>%
is_in(
d1$V202419 %>%
levels %>%
extract(9:10)
)
] <- "3. Disagree"
d1$immig_crime <- NA
d1$immig_crime[
d1$V202420 %>%
is_in(
d1$V202420 %>%
levels %>%
extract(6:7)
)
] <- "1. Agree"
d1$immig_crime[
d1$V202420 %>%
is_in(
d1$V202420 %>%
levels %>%
extract(8)
)
] <- "2. Neither agree nor disagree"
d1$immig_crime[
d1$V202420 %>%
is_in(
d1$V202420 %>%
levels %>%
extract(9:10)
)
] <- "3. Disagree"
Now, presidential vote
d1$pres_vote <- NA
d1$pres_vote[
d1$V202105x %>%
equals(
d1$V202105x %>%
levels %>%
extract(2)
)
] <- "Biden voter"
d1$pres_vote[
d1$V202105x %>%
equals(
d1$V202105x %>%
levels %>%
extract(3)
)
] <- "Trump voter"
Two part education variable
d1$educ_2 <- NA
d1$educ_2[
d1$educ %>%
equals("BA or more")
] <- "BA or more"
d1$educ_2[
d1$educ %>%
equals("BA or more") %>%
not
] <- "Less than BA"
Now, the three tables on immigration, education, and presidential vote
xtabs(
~ immig_crime + educ_2 + pres_vote,
data = d1,
drop.unused.levels = T
) %>%
ftable(
row.vars = c(
"educ_2",
"immig_crime"
)
) %>%
prop.table(1) %>%
round(2) %>%
multiply_by(100)
## pres_vote Biden voter Trump voter
## educ_2 immig_crime
## BA or more 1. Agree 18 82
## 2. Neither agree nor disagree 44 56
## 3. Disagree 86 14
## Less than BA 1. Agree 20 80
## 2. Neither agree nor disagree 41 59
## 3. Disagree 78 22
xtabs(
~ immig_culture + educ_2 + pres_vote,
data = d1,
drop.unused.levels = T
) %>%
ftable(
row.vars = c(
"educ_2",
"immig_culture"
)
) %>%
prop.table(1) %>%
round(2) %>%
multiply_by(100)
## pres_vote Biden voter Trump voter
## educ_2 immig_culture
## BA or more 1. Agree 23 77
## 2. Neither agree nor disagree 31 69
## 3. Disagree 75 25
## Less than BA 1. Agree 18 82
## 2. Neither agree nor disagree 33 67
## 3. Disagree 64 36
xtabs(
~ immig_econ + educ_2 + pres_vote,
data = d1,
drop.unused.levels = T
) %>%
ftable(
row.vars = c(
"educ_2",
"immig_econ"
)
) %>%
prop.table(1) %>%
round(2) %>%
multiply_by(100)
## pres_vote Biden voter Trump voter
## educ_2 immig_econ
## BA or more 1. Agree 74 26
## 2. Neither agree nor disagree 44 56
## 3. Disagree 21 79
## Less than BA 1. Agree 60 40
## 2. Neither agree nor disagree 41 59
## 3. Disagree 21 79
As we might expect–stronger relationships between issue positions and vote choice among the highly educated.