You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A weird bug is occuring, maybe related to #410.
When imputing a factor variable that should be constant within a class (e.g. smoking for a patient with longitudinal measurements), something goes wrong.
Reprex (from the vignette of the 2lonly.pmm function) which gives as a warning:
Warning message:
In `[<-.factor`(`*tmp*`, cc, value= c(`64`=1, `64`=1, `64`=1, :invalidfactorlevel, NAgenerated
And the resulting dataframe has missing values in column V.
# simulate some data# x,y ... level 1 variables# v,w ... level 2 variablesG<-250# number of groupsn<-20# number of personsbeta<-.3# regression coefficientrho<-.30# residual intraclass correlationrho.miss<-.10# correlation with missing responsemissrate<-.50# missing proportiony1<- rep(rnorm(G, sd= sqrt(rho)), each=n) + rnorm(G*n, sd= sqrt(1-rho))
w<- rep(round(rnorm(G), 2), each=n)
v<- rep(round(runif(G, 0, 3)), each=n)
x<- rnorm(G*n)
y<-y1+beta*x+.2*w+.1*vdfr0<-dfr<-data.frame("group"= rep(1:G, each=n), "x"=x, "y"=y, "w"=w, "v"=v)
dfr[rho.miss*x+ rnorm(G*n, sd= sqrt(1-rho.miss)) < qnorm(missrate), "y"] <-NAdfr[rep(rnorm(G), each=n) < qnorm(missrate), "w"] <-NAdfr[rep(rnorm(G), each=n) < qnorm(missrate), "v"] <-NA# empty mice imputationimp0<- mice(as.matrix(dfr), maxit=0)
predM<-imp0$predictorMatriximpM<-imp0$method# multilevel imputationpredM1<-predMpredM1[c("w", "y", "v"), "group"] <--2predM1["y", "x"] <-1# fixed x effects imputationimpM1<-impMimpM1[c("y", "w", "v")] <- c("2l.pan", "2lonly.norm", "2lonly.pmm")
# turn v into a categorical variabledfr$v<- as.factor(dfr$v)
levels(dfr$v) <-LETTERS[1:4]
# y ... imputation using pan# w ... imputation at level 2 using norm# v ... imputation at level 2 using pmm# skip imputation on solarisis.solaris<-function() grepl("SunOS", Sys.info()["sysname"])
if (!is.solaris()) {
imp<- mice(dfr,
m=1, predictorMatrix=predM1,
method=impM1, maxit=1, paniter=500
)
}
The text was updated successfully, but these errors were encountered:
stefvanbuuren
changed the title
Error when imputing level 2 factor variables with 2lonly.pmm
Warning when imputing level 2 factor variables with 2lonly.pmm
May 15, 2023
A weird bug is occuring, maybe related to #410.
When imputing a factor variable that should be constant within a class (e.g. smoking for a patient with longitudinal measurements), something goes wrong.
Reprex (from the vignette of the 2lonly.pmm function) which gives as a warning:
Warning message:
And the resulting dataframe has missing values in column V.
The text was updated successfully, but these errors were encountered: