amices / mice

Multivariate Imputation by Chained Equations
https://amices.org/mice/
GNU General Public License v2.0
428 stars 107 forks source link

mice.impute.quadratic bug?? #575

Open johamunoz opened 1 year ago

johamunoz commented 1 year ago

Dear all,

** Describe the issue: I am trying to use the function mice.impute.quadratic in a binomial model. The function works when the outcome "y" is specified as a continuous variable but as soon as I specify it as a factor, I get an error. Is this function applicable for this scenario or is this a bug?

Please look at the following reproducible example Thank you very much Johanna

To Reproduce

Create data

N <- 500 Beta <- c(-2.3365665,0.8569439,0.3011304) X <- runif(n=N, min=-3, max=3) XX <- X^2 linear <- plogis(Beta[[1]]+Beta[[2]]X + Beta[[3]]XX) Y <- rbinom(n = N, size = 1, prob = linear) ID <- 1:N

dat <- data.frame(x = X, xx = XX, y = Y, ID = ID)

Ampute

patterns = rbind(c( 1, 1, 0, 1), c(0, 0, 1, 1), c(0, 0, 0, 1)) # (0=missing, 1=observed) freq = c(1/3, 1/3,1/3) # ocurrence of patterns mech = c("MAR") weights = ampute.default.weights(patterns, mech) # Weights of weighted sum scores

data_inc <- ampute(data = dat, patterns = patterns, prop = prop, #Percentage of missing cells freq = freq, mech = mech, weights=weights)$amp

Prepare data for imputation

ini <- mice(data_inc, maxit = 0) meth <- c("quadratic", "~I(x^2)", "logreg","") pred <- ini$pred pred["x", "xx"] <- 0

Impute data with y as continuous

imp <- mice(data_inc, meth = meth, pred = pred, quad.outcome = "y")

Warning message:Type mismatch for variable(s): y Imputation method logreg is for categorical data.

Impute data with y as factor

data_inc $y<- as.factor(data_inc$y) imp <- mice(data_inc, meth = meth, pred = pred, quad.outcome = "y")

iter imp variable1 1 xError in mice.impute.quadratic(y = c(-2.00387528445572, -0.957946232054383, : The name specified for the outcome in 'quad.outcome' can not be found in the data

johamunoz commented 1 year ago

Also when I tried to do it with the "smcfcs" method. I don't get correctly imputed values for y, (I get values out of 0 or 1) as you can see here data_inc$x_sq<-NULL imp<- mice::mice(data_inc, method="smcfcs", sm= y ~ x + I(x^2), m=5, maxit=10, smtype="logistic") summary(complete(imp,1))

However if I use Professor Barletts "smcfcs" package I get consistent imputed y values. data_inc$x_sq <- data_inc$x^2 nonmice <- smcfcs::smcfcs(data_inc, smtype="logistic", smformula=y ~ x + x_sq, method=c("norm", "", "", "x^2"))$impDatasets summary(nonmice[[1]]).

If you could show me what my error is or guide me on how to use the "quadratic "or "smcfcs "method properly I would appreciate it very much since I need to use the mice.mids function that does not work with outputs from the smcfcs package. Thank you very much Johanna