Error in with() after Impute then Transform #338
Unanswered
SunshineCheesesauce
asked this question in
Q&A
Replies: 1 comment
-
I believe your issue is answered here on stack overflow. Closing as this seems unrelated to |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I have been trying to transform a variable after imputation, based on the best fitting model applied to the imputed data.
I passively impute 3 'empty' variables that I will later add information to based on results of a best fit linear regression model of imputed data.
Reproducible code:
data <- boys[boys$age >= 8, -4]
data$DV <- NA
data$R1 <- NA
data$P1 <- NA
init = mice(data, maxit = 0)
meth = init$method
predM = init$predictorMatrix
predM[, c("DV", "R1", "P1")]=0
imptest = mice(data = data, method = meth, predictorMatrix = predM, seed = 28382, m = 10, print = FALSE)
scope <- list(upper = ~ age + hgt + wgt + hc + gen + phb + reg,
lower = ~1)
expr <- expression(f1 <- lm(tv ~ 1),
f2 <- step(f1, scope = scope))
fit <- with(imptest, expr)
formulas <- lapply(fit$analyses, formula)
terms <- lapply(formulas, terms)
votes <- unlist(lapply(terms, labels))
table(votes)
testmodel <- with(imptest, lm(tv ~ age + gen + phb + reg))
for(i in 1:10){
imptest$data$R1= residuals(testmodel$analyses[[i]])
imptest$data$P1= predict(testmodel$analyses[[i]])
}
imptest$data$DV <- imptest$data$P1 - imptest$data$tv
range(imptest$data$tv, na.rm = T)
range(imptest$data$DV, na.rm = T)
range(imptest$data$P1, na.rm = T)
scope <- list(upper = ~ DV + hgt + wgt + hc + gen + phb + reg,
lower = ~1)
expr <- expression(f1 <- lm(age ~ 1),
f2 <- step(f1, scope = scope))
fit <- with(imptest, expr)
When I reach this bit I get the error:
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :contrasts can be applied only to factors with 2 or more levels
Can you help? Thanks
Beta Was this translation helpful? Give feedback.
All reactions