Your original data does not have any cases where ApacheData$daily == 2
. The lm
object has no coefficient associated with it, so it throws an error.
Error in predict.lm in R: factor as.factor(daily) has new level 2
-
06-07-2023 - |
Pregunta
I built a linear regression model as below:
ApacheData$daily <- cut(ApacheData$daily, breaks=c(-1, 0, 1, 2, 3, 9,3000))
ApacheData$age <- cut(ApacheData$age, breaks=c(0,44,65,150))
fit <-lm(tomorrow_apache~ as.factor(state_today)
+as.numeric(daily_creat)
+ as.factor(daily)
+ as.factor(age)
+as.numeric(apache3)
+ as.factor(mv)
+ as.numeric(min_GCS), ApacheData)
and I want to use this model to predict a new input value, so I built a data frame:
new <- data.frame(state_today=1, daily_creat=2.3, daily=2 , age=25, apache3=12, mv=1, min_GCS=20)'
Then I call predict:
predict(fit, new , se.fit=TRUE)
And I get the error: Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor as.factor(daily) has new level 2
I also tried daily=as.factor(2) in data.frame() and I got the same error. Can anyone help me about this?
Thank you so much for your time!
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow