## ----echo=F, results='hide', fig.show='hide', message=FALSE, warning=FALSE---- library(knitr) opts_chunk$set(tidy=TRUE, tidy.opts = list(only.comment=TRUE), warning=FALSE, message=FALSE) ## ----nes_mod, echo=T----------------------------------------------------- library(checkpoint) checkpoint("2017-06-01") ## Load packages library(foreign) library(mice) ## read in data nes2004 <- read.dta("http://www.quantoid.net/files/reg3/anes2004_sub.dta") ## estimate and summarize listwise deleted model nes.mod <- lm(leftright ~ age + educ + male + hhincome2, data=nes2004) ## use default mice algorithm to impute missing data ## defaults are pmm for quantitative, logit for binary, ## multinomial logit for multi category and ## ordered logit for ordered categorical variables ## Here I use m=5, but in real life you probably want this to be ## a lot bigger (like 100 or 500). nes.mice <- mice(nes2004, printFlag=F, m=5) ## ----echo=T, eval=F------------------------------------------------------ ## ## install the SensMiceDA package from github. This is based on the SensMice package ## ## but has a bunch of wrappers to make it more friendly to interact with the functions. ## library(devtools) ## install_github("davidaarmstrong/SensMiceDA") ## ----echo=T, eval=F------------------------------------------------------ ## install.packages("C:/Users/User/Download/SensMiceDA_1.1.tar.gz", ## type="source", repos=NULL) ## ----nisetup, echo=T----------------------------------------------------- ## find some values of left-right to check sensitivity. Here the idea is that ## we are testing to see how results change if non-responders are -2sd, -1sd, 0, ## 1sd and 2sd different from responders on left-right variable on average. lr.vals <- sd(nes2004$leftright, na.rm=T)*c(-2,-1,0,1,2) ## ----sens_imp, echo=T, results="hide"------------------------------------ ## load the package and estimate the sensitivity imputations ## this doesn't work for cart and rf now, but does work for the ## parametric models. library(SensMiceDA) newimps <- sens.est(nes.mice, list(leftright = lr.vals)) ## ----sens_manage, echo=T------------------------------------------------- ## update the original (listwise-deleted) model using the sensitivity ## estimation imputations. pool.est <- sens.pool(nes.mod, impData=nes.mice, sensData=newimps) ## ----lrsens, echo=T, fig.show='hide', fig.height=6, fig.width=8---------- ## plot the sensitivity test results plot(pool.est, layout=c(4,2)) ## ----echo=T-------------------------------------------------------------- ## do a test for a single variable of the sensitivity test sens.test(nes.mod, var="educ", impData=nes.mice, sensData=newimps) ## ----wald, echo=T-------------------------------------------------------- ## do a wald test for all coefficients from the sensitivity analysis. sens.wald(nes.mod, c("educ2=0", "educ3=0", "educ4=0", "educ5=0"), newimps, nes.mice, test="F")