7

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.

BonnieM
  • 191
  • 1
  • 13

4 Answers4

6

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.

Hedgehog
  • 5,487
  • 4
  • 36
  • 43
  • Bummer! Well, thanks for the verification and info, @Hedgehog! I've since moved this model into lmer(), which worked for it. But it would be helpful to get this worked out for gamlss. – BonnieM Mar 09 '18 at 14:15
  • Sorry that is as close to an answer as you will get until the implementation is complete. – Hedgehog Mar 11 '18 at 21:26
1

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

kaisch
  • 21
  • 4
0

I had a lot of random problems in this direction, and found fitting using the weights argument, and some extra dummy observations set to weight zero (but the predictors I was interested in) to be one workaround.

Charlie
  • 481
  • 4
  • 16
0

I was able to overcome the undefined columns selected error by ensuring that the new data for the newdata parameter had the EXACT column structure as what was used when running the gamlss model.