Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when predicting new fitted values from R gamlss object

Tags:

r

predict

gam

I have a gamlss model that I'd like to use to make new y predictions (and confidence intervals) from in order to visualize how well the model fits the real data. I'd like to make predictions from a new data set of randomized predictor values (rather than the original data), but I'm running into an error message. Here's some example code:

library(gamlss)    

# example data
irr <- c(0,0,0,0,0,0.93,1.4,1.4,2.3,1.5)
lite <- c(0,1,2,2.5)
blck <- 1:8
raw <- data.frame(
   css =abs(rnorm(500, mean=0.5, sd=0.1)),
   nit =abs(rnorm(500, mean=0.72, sd=0.5)),
   irr =sample(irr, 500, replace=TRUE),
   lit =sample(lite, 500, replace=TRUE),
   block =factor(sample(blck, 500, replace=TRUE))
)

# the model
mod <- gamlss(css~nit + irr + lit + random(block), 
       sigma.fo=~irr*nit + random(block), data=raw, family=BE)

# new data (predictors) for making css predictions
pred <- data.frame(
nit =abs(rnorm(500, mean=0.72, sd=0.5)),
irr =sample(irr, 500, replace=TRUE),
lit =sample(lite, 500, replace=TRUE),
block =factor(sample(blck, 500, replace=TRUE))
)

# make predictions
predmu <- predict(mod, newdata=pred, what="mu", type="response")

This gives the following error:

Error in data[match(names(newdata), names(data))] : 
  object of type 'closure' is not subsettable

When I run this on my real data, it gives this slightly different error:

Error in `[.data.frame`(data, match(names(newdata), names(data))) : 
  undefined columns selected

When I use predict without newdata, it works fine making predictions on the original data, as in:

predmu <- predict(mod, what="mu", type="response")

Am I using predict wrong? Any suggestions are greatly appreciated! Thank you.

like image 202
BonnieM Avatar asked Mar 12 '17 00:03

BonnieM


2 Answers

No, you are not wrong. I have experienced the same issue.

The documentation indicates the implementation of predict is incomplete. this appears to be an example of an incomplete feature/function.

like image 189
Hedgehog Avatar answered Oct 20 '22 08:10

Hedgehog


Hedgehog mentioned that predictions based on new-data is not possible yet. BonnieM therefore "moved the model" into lmer().

I would like to further comment on this idea: BonniM tried to get predictions based on the object mod

mod <- gamlss(css~nit + irr + lit + random(block), 
   sigma.fo=~irr*nit + random(block), data=raw, family=BE)

"Moving into lme()" in this scenario could look as follows:

mod2 <- gamlss(css~nit + irr + lit + re(random=~1|block), 
               sigma.fo=~irr*nit + re(random=~1|block), 
               data=raw, 
               family=BE)

Predictions on new-data based on mod2 are implemented within the gamlss2 package. Furthermore, mod and mod2 should be the same models. See: Stasinopoulos, M. D., Rigby, R. A., Heller, G. Z., Voudouris, V., & De Bastiani, F. (2017). Flexible regression and smoothing: using GAMLSS in R. Chapman and Hall/CRC. Chapter 10.9.1

Best regards Kai

like image 23
kaisch Avatar answered Oct 20 '22 07:10

kaisch