Skip to content

Instantly share code, notes, and snippets.

@tslumley
Created April 21, 2019 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tslumley/9ebe5e16bc001ba22978c2d4edb057ac to your computer and use it in GitHub Desktop.
Save tslumley/9ebe5e16bc001ba22978c2d4edb057ac to your computer and use it in GitHub Desktop.
Plausible values in surveys
withPV<-function(mapping, design, action, ...) UseMethod("withPV",design)
withPV.survey.design<-function(mapping, design, action,...){
if(inherits(mapping,"formula")) mapping<-list(mapping)
if (!is.list(mapping))
stop("'mapping' must be a list of formulas")
if (!all(sapply(mapping, length)==3))
stop("'mapping' must be a list of two-sided formulas")
df<-model.frame(design,na.action=na.pass)
PVframes<-lapply(mapping,
function(f) model.frame(f[-2], model.frame(design,na.action=na.pass)))
nvars<-length(PVframes)
PVnames<-sapply(mapping, function(f) deparse(f[[2]]))
if (any(PVnames %in% colnames(design)))
stop("working PV names must not already occur in the data")
nreps<-sapply(PVframes, NCOL)
if (length(unique(nreps))>1)
stop("number of plausible values must be the same for all variables")
nreps<-nreps[1]
.DESIGN<-design
results<-vector("list",nreps)
for(i in 1:nreps){
dfi<-lapply(PVframes, function(d) d[[i]])
names(dfi)<-PVnames
.DESIGN$variables<-cbind(df, as.data.frame(dfi))
if (is.function(action))
results[[i]] <- action(.DESIGN)
else
results[[i]] <- eval(action)
}
attr(results,"call")<-sys.call()
results
}
\name{withPV}
\alias{withPV}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
Analyse plausible values in surveys
}
\description{
Repeats an analysis for each of a set of 'plausible values' in a survey data set, returning a list suitable for \code{mitools::MIcombine}. The default method works for both standard and replicate-weight designs but not for two-phase designs.
}
\usage{
withPV(mapping, design, action, ...)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{mapping}{
A formula or list of formulas describing each variable in the analysis that has plausible values. The left-hand side of the formula is the name to use in the analysis; the right-hand side gives the names in the dataset.
}
\item{design}{
A survey design object, as created by \code{svydesign} or \code{svrepdesign}
}
\item{action}{
A function taking a survey design object as its only argument, or a quoted expression with \code{.DESIGN} referring to the survey design object to be used.
}
\item{\dots}{
For methods
}
}
\value{
A list of the results returned by each evaluation of \code{action}, with the call as an attribute.
}
\seealso{
\code{\link{with.svyImputationList}}
}
\examples{
library(mitools)
data(nzmaths)
des<-svydesign(id=~SCHOOLID+STIDSTD, strata=~STRATUM, nest=TRUE,
weights=~W_FSCHWT+condwt, data=nzmaths)
oo<-options(survey.lonely.psu="remove")
results<-withPV(list(maths~PV1MATH+PV2MATH+PV3MATH+PV4MATH+PV5MATH), design=des,
action= quote(svyglm(maths~ ST04Q01*(PCGIRLS+SMRATIO)+MATHEFF+OPENPS, design=.DESIGN))
)
summary(MIcombine(results))
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{survey}% use one of RShowDoc("KEYWORDS")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment