Skip to content

Instantly share code, notes, and snippets.

@jwbowers
Last active December 24, 2015 09:39
Show Gist options
  • Save jwbowers/6779199 to your computer and use it in GitHub Desktop.
Save jwbowers/6779199 to your computer and use it in GitHub Desktop.
##Load a small version of Fortna's data.
load(url("http://jakebowers.org/PS230/pk2.df.rda"))
## Make a new dataframe with no missing values at all
pk.good<-na.omit(pk.df)
## Load the mosaic library for plotting etc
library(mosaic)
with(pk.good,table(UNpk,pkopF))
## First just recode whether any peacekeeping happened
pk.good$anypk<-factor(with(pk.good,
pkopF!='no pk'),labels=c('no pk','some pk'))
##Check recode:
##with(pk.good,table(anypk,pkopF,useNA="ifany"))
with(pk.good,table(anypk,UNpk,useNA="ifany"))
## You can run the commented out code to learn about what
## interaction() does
## with(pk.good,table(interaction(UNpk,anypk,drop=TRUE)))
pk.good$pktype<-with(pk.good,
factor(interaction(UNpk,anypk,drop=TRUE),
labels=c('no pk','notUNpk','UNplus')
))
## Check to see if I got the order of the labels correct:
## with(pk.good,table(pktype,interaction(anypk,UNpk,drop=TRUE)))
mean(dead~pktype,data=pk.good)
## this next is the same as above but works without the mosaic library loaded.
with(pk.good,tapply(dead,pktype,mean))
## this next only works if you've loaded the mosaic library
themeans<-mean(dead~pktype,data=pk.good)
par(mfrow=c(1,2),oma=rep(0,4),mgp=c(1.5,.5,0))
plot(dead~pktype,data=pk.good)
points(1:3,themeans,pch=19)
## this version zooms in on the typical values.
plot(dead~pktype,data=pk.good,ylim=c(0,1000000))
points(1:3,themeans,pch=19)
## you will probably need to install the quantreg package
## So uncomment and run the following line
## install.packages('quantreg')
library(quantreg)
## don't worry about the warning: means make it easier to find unique
## solutions to this opimization problem than medians
lad1<-rq(dead~pktype,data=pk.good,tau=.5) ##smooth the medians,
coef(lad1) ##the coefficients from the model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment