public
Created

Letting plyr do the work for us

  • Download Gist
plyr_NOMINATE.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# Letting plyr do the work for us.
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("foreign", "plyr", "Hmisc", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
 
# See: http://voteview.org/dwnominate.asp
dwNominate <- read.dta("ftp://voteview.com/junkord/HL01111E21_PRES.DTA")
 
# Make a re-coded party variable
dwNominate$majorParty <- "Other"
dwNominate$majorParty[dwNominate$party == 100] <- "Democrat"
dwNominate$majorParty[dwNominate$party == 200] <- "Republican"
head(dwNominate)
 
# Letting plyr do the work for us (most of the functions are from Hmisc)
aggregatedIdeology <- ddply(.data = dwNominate,
.variables = .(cong, majorParty),
.fun = summarise, # Allows the following:
Median = wtd.quantile(dwnom1, 1/bootse1, 1/2),
q25 = wtd.quantile(dwnom1, 1/bootse1, 1/4),
q75 = wtd.quantile(dwnom1, 1/bootse1, 3/4),
q05 = wtd.quantile(dwnom1, 1/bootse1, 1/20),
q95 = wtd.quantile(dwnom1, 1/bootse1, 19/20),
N = length(dwnom1),
.progress = "text") # Because we can.
aggregatedIdeology$majorParty <- factor(aggregatedIdeology$majorParty,
levels = c("Republican", "Democrat", "Other"))
head(aggregatedIdeology) # All of our stats, calculated "by" our .variables
 
# Neat, simple, clean plot of ideological distributions
# Neat, simple, clean plot of ideological distributions
zp1 <- ggplot(aggregatedIdeology,
aes(x = cong, y = Median,
ymin = q05, ymax = q95,
colour = majorParty, alpha = N))
zp1 <- zp1 + geom_linerange(aes(ymin = q25, ymax = q75), # Plot the 90% CI
size = 1) # it inherits x, y, colour and alpha
zp1 <- zp1 + geom_pointrange(size = 1/2) # Plot the IQR
zp1 <- zp1 + scale_colour_brewer(palette = "Set1")
zp1 <- zp1 + theme_bw()
print(zp1)

An complimentary approach to visualization would be to fit a generalized additive model to evaluate the trustworthiness of the visual trends. The code below achieves this (restricting the years to those with data for both democrats and republicans, and eliminating the other category for low data counts).

library(ez)
library(foreign)

#get the data
dwNominate <- read.dta("ftp://voteview.com/junkord/HL01111E21_PRES.DTA")

# Make a re-coded party variable
dwNominate$majorParty <- "Other"
dwNominate$majorParty[dwNominate$party == 100] <- "Democrat"
dwNominate$majorParty[dwNominate$party == 200] <- "Republican"

#toss the "Other" data and factorize majorParty with a sum contrast
dwNominate = dwNominate[dwNominate$majorParty!='Other',]
dwNominate$majorParty = factor(dwNominate$majorParty,levels=c('Democrat','Republican'))
contrasts(dwNominate$majorParty) = 'contr.sum'

#narrow data to cong>35
dwNominate = dwNominate[dwNominate$cong>35,]

#fit the gam
fit = gam(
    data = dwNominate
    , formula = dwnom1 ~ majorParty + s(cong,by=majorParty,k=76,bs='ts')
    , weights = I(1/bootse1)
)

#obtain predictions
preds = ezPredict(fit)

#data for each party separate, with a posteriori bootstrapped 95% CIs
ezPlot2(
    preds = preds
    , x = cong
    , split = majorParty
    , ribbon = T
)

#data for the party difference, with a posteriori bootstrapped 95% CI
ezPlot2(
    preds = preds
    , x = cong
    , diff = majorParty
    , reverse_diff = T
    , ribbon = T
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.