I'm trying to use the 'predict' function on new data after running an nlmer model, and am running into issues.
The dataset I use for the model looks like so (except patient_id has an actual value, which I can't share):
> data
patient_id variable value M_visit_time M_agesero D_intercept D_agesero
1: SECRET vl 4.542850 1.624658 33.16164 0 0.00000
2: SECRET vl 4.408664 2.010959 33.16164 0 0.00000
3: SECRET vl 4.493095 2.219178 33.16164 0 0.00000
4: SECRET vl 4.540980 2.583562 33.16164 0 0.00000
5: SECRET vl 2.844477 3.293151 33.16164 0 0.00000
...
I run the model:
model_formula <- "value~ModelGradient(time=M_visit_time,b0,b2,b3)~ M_agesero +
D_intercept + D_agesero + (b0|patient_id)"
Model<- ~b0+b2*exp(-b3*time)
ModelGradient<-deriv(Model,namevec=c("b0","b2","b3"),
function.arg=c("time","b0","b2","b3"))
out<-do.call("nlmer", list( as.formula(model_formula),
data=quote(data),
start = c(b0=3,b2=1,b3=4),
control=nlmerControl(optimizer="bobyqa",
optCtrl=list(maxfun=200000))))
The dataset for which I'd like predictions looks like this:
> newdata
patient_id variable value M_visit_time M_agesero D_intercept D_agesero
1: SECRET observed_survival 1.198478 0.0 0.00000 1 33.16164
2: SECRET vl 4.542850 0.0 33.16164 0 0.00000
3: SECRET vl 4.542850 0.5 33.16164 0 0.00000
4: SECRET vl 4.542850 1.0 33.16164 0 0.00000
5: SECRET vl 4.542850 1.5 33.16164 0 0.00000
...
But when I run the command
newdata$predicted <- predict(out, newdata=newdata)
I get the error:
Error in `[.data.frame`(fr, vars) : undefined columns selected
Running data$predicted <- predict(out)
works just fine, however.
Input appreciated!