Skip to content

Instantly share code, notes, and snippets.

@jwbowers
Last active December 23, 2015 10:49
Show Gist options
  • Save jwbowers/6624526 to your computer and use it in GitHub Desktop.
Save jwbowers/6624526 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 and adding UNpk (UN
## peacekeeping) and year the civil war started
pk.good<-na.omit(subset(pk.df,select=c('ridp','wardur','warstart.yr','warstart.hist.yr','UNpk','id')))
## Load the mosaic library for plotting etc
library(mosaic)
lm1<-lm(ridp~warstart.hist.yr,data=pk.good) ## fit the model
plot(ridp~warstart.hist.yr,data=pk.good)
points(fitted(lm1)~warstart.hist.yr,pch=19,data=pk.good)
## Or all in one line as
##library(lattice)
##xyplot(ridp+fitted(lm1)~warstart.hist.yr,data=pk.df,pch=c(1,19))
##Function that takes a linear model (mod) and a value for an
##explanatory variable (x) as input and produces an output based on
##the coefficients in that model.
## Notice that you can give x more than one value if you concatenate
## them like c(value1,value2)
makemv<-function(mod,x){ as.vector(coef(mod)[1]+coef(mod)[2]*x) }
pk.good$lm1.fit<-fitted(lm1)
pk.good[order(pk.good$warstart.hist.yr),c("id","warstart.hist.yr","lm1.fit")]
with(pk.good,cbind(warstart.hist.yr,lm1fit=fitted(lm1))[warstart.hist.yr>=1967 & warstart.hist.yr<1971,])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment