
In R, dots are common, underscores are ok:
## a few bad variable names from my past work
incspendtran
V101
gender # if coded as one or zero, as a factor I'd accept this
## better names
increase.spending.transportation
respondent.id
female # coded 1 if femaleperc.perc.district, or state, perc.stateperc.district.demperc.district.dem.marriage means their district-level perception of support for same-sex marriage. perc.district.guns.banassault is district-level support for banning assault weapons. There’s also guns.background.check….grep() to grab variables that are similar# plot partisan estimates against one another
# voters only
g = ggplot(ncs, aes(voted.pid7.dem)) +
geom_density(color = 'blue') +
theme_classic() +
theme(legend.position='none') +
xlim(0,1) +
theme(axis.title=element_text(size=8), plot.title = element_text(hjust=0.5, size = 10)) +
geom_density(aes(voted.pid7.rep), color = 'red') +
ggtitle("Distribution of electorate partisanship estimates") +
xlab("Percent of voters in district belonging to each party")### POSTSTRATIFICATION ###
# set up poststratification file and poststratify 2014 MRSP models
# level is upper or lower districts, pid.level is 3 or 7 point item
get.cell.predictions <- function(individual.model,
pid.var, # e.g., 'pid3.dem'
pstrat){
# district random effects need zeros for missing dists
district.ranefs <- ranef(individual.model)$modgeoid
missing.districts <- setdiff(unique(pstrat$modgeoid), rownames(district.ranefs))
missing.districts.df <- data.frame(district = missing.districts)
rownames(missing.districts.df) <- missing.districts.df$district
missing.districts.df$`(Intercept)` <- 0
missing.districts.df <- dplyr::select(missing.districts.df, -district)
district.ranefs <- rbind.data.frame(district.ranefs, missing.districts.df)
# NOTE: make sure AK and HI is working# divide within proportions grouped by party
# and district to get dist-party estimates
pstrat.upper = pstrat %>%
dplyr::filter(grepl("U", modgeoid)) %>%
cbind(upper.cell.predictions) %>%
dplyr::select(-X)
pstrat.lower = pstrat %>%
dplyr::filter(grepl("L", modgeoid)) %>%
cbind(lower.cell.predictions) %>%
dplyr::select(-X)broomdotwhiskerstringr




?lm
?read.csv
?dplyr::rename




