Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created November 16, 2023 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/0f7050b5cdf84dd288cb399886eac45f to your computer and use it in GitHub Desktop.
Save ryanburge/0f7050b5cdf84dd288cb399886eac45f to your computer and use it in GitHub Desktop.
Code for 11/16
error_bar <- function(wd){
if(missing(wd)){
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2, position=position_dodge(.9))
} else{
geom_errorbar(aes(ymin=lower, ymax=upper), width=wd, position=position_dodge(.9))
}
}
cces20 <- read_csv("https://www.dropbox.com/s/wuixmc67ae786wp/small_ces2020.csv?dl=1")
### CALCULATE THE SHARE OF EACH RACIAL GROUP THAT HAS A FOUR YEAR COLLEGE DEGREE #####
ed <- cces20 %>%
mutate(race = frcode(race == 1 ~ "White",
race == 2 ~ "Black",
race == 3 ~ "Hispanic",
race == 4 ~ "Asian",
race == 5 ~ "Native American",
race == 6 ~ "Two Or More",
race == 7 ~ "Other",
race == 8 ~ "Middle Eastern")) %>%
mutate(ba = case_when(educ == 5 | educ == 6 ~ 1,
educ <= 4 ~ 0)) %>%
group_by(race) %>%
mean_ci(ba)
ed %>%
ggplot(., aes(x = race, y = mean)) +
geom_col() +
coord_flip() +
error_bar()
ed1 <- cces20 %>%
mutate(ba = case_when(educ == 5 | educ == 6 ~ 1,
educ <= 4 ~ 0)) %>%
group_by(birthyr) %>%
mean_ci(ba)
ed1 %>%
filter(birthyr >= 1940) %>%
ggplot(., aes(x = birthyr, y = mean )) +
geom_point() +
error_bar()
ed2 <- cces20 %>%
mutate(race2 = frcode(race == 1 ~ "White",
TRUE ~ "Non-White")) %>%
mutate(ba = case_when(educ == 5 | educ == 6 ~ 1,
educ <= 4 ~ 0)) %>%
group_by(birthyr, race2) %>%
mean_ci(ba)
ed2 %>%
filter(birthyr >= 1940) %>%
ggplot(., aes(x = birthyr, y = mean, color = race2)) +
geom_point() +
error_bar()
## Republicans
gg1 <- cces20 %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(ba = case_when(educ == 5 | educ == 6 ~ 1,
educ <= 4 ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
group_by(gender) %>%
mean_ci(ba) %>%
mutate(pid = "Republican")
gg2 <- cces20 %>%
filter(pid7 == 1 | pid7 == 2 | pid7 == 3) %>%
mutate(ba = case_when(educ == 5 | educ == 6 ~ 1,
educ <= 4 ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
group_by(gender) %>%
mean_ci(ba) %>%
mutate(pid = "Democrat")
gg3 <- bind_rows(gg1, gg2)
gg3 %>%
ggplot(., aes(x = gender, y = mean, fill = pid)) +
geom_col(position = "dodge") +
error_bar() +
scale_fill_manual(values = c("dodgerblue3", "firebrick3"))
bball <- read_csv("https://raw.githubusercontent.com/ryanburge/pls2003_sp17/master/bball.csv", guess_max = 25000) %>%
rename(games = g, wins =w, losses = l, runs =r, atbats = ab, hits =h, homeruns =hr, walks = bb, strikeout = so, stolenbase = sb, earnedruns = era)
bball %>%
ggplot(., aes(x = runs, y = wins)) +
geom_point() +
geom_smooth(method = lm)
lm(wins ~ runs, data = bball)
bball %>%
ggplot(., aes(x = wins, y = attendance, color = league_id)) +
geom_point() +
geom_smooth(method = lm)
lm(attendance ~ wins, data = bball)
bball %>%
arrange(-runs)
## LEAST NUMBER OF RUNS SCORED IN 2003 ####
bball %>%
filter(year == 2003) %>%
arrange(runs)
bball %>%
select(team_id, wins, runs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment