[R-sig-ME] MCMCglmm predictions using new data error

Sven Templer sven.templer at gmail.com
Sat Jan 9 08:21:09 CET 2016


Hi Alberto,

see comments inline.

Best,
Sven

> On 08 Jan 2016, at 17:51, Alberto Gallano <alberto.gc8 at gmail.com> wrote:
> 
> Hi Sven,
> 
> thanks so much. That works. Do you know if there is any documentation as to how to set up new data for the MCMCglmm predict method? Adding a constant response vector to the prediction grid is different to every other R predict method (that I know of).

I just interpreted the error message and looked at the predict.MCMCglmm method. In the documentation the description is not very detailed and I am not aware of other sources. Maybe have a look at the vignettes? 

> 
> One further problem i'm having is that I actually have random effects, and now the predict function throws an error when these are not entered into the prediction grid. It's unclear to me how they should be entered - using a constant doesn't work. The number of random effect grouping levels may not equal the dimensions of the rest of the prediction grid, so just adding a vector with each unique grouping level won't work here either. Any thoughts would be much appreciated. See "pred_grid2" at bottom of code below:
> 
> 
> # ----------------------------------------------------------------------------------
> library(MCMCglmm)
> 
> set.seed(123)
> 
> dat <- data.frame(
>     y = rnorm(100),
>     x = rnorm(100), 
>     id = factor(rep(paste("ID", 1:20), 5))
>     )
> 
> fit <- MCMCglmm(
>     fixed = y ~ x,
>     random = ~ id,
>     rcov = ~ units,
>     data = dat,
>     family = "gaussian",
>     pr = TRUE, pl = TRUE,
>     saveX = TRUE,  saveZ = TRUE,
>     nitt = 1.3e+4, thin = 10, burnin = 3e+3
> )
> 
> 
> pred_grid1 <- with(dat, 
>     data.frame( 
>     y = 0,
>     x = seq(-1, 1, length.out = 30)
>     ))
> 
> predict(fit, newdata = pred_grid1)
> 
> # > Error in buildZ(rmodel.terms[r], data = data, nginverse = names(ginverse)) : object id not found
>   
>   
> pred_grid2 <- with(dat, 
>     data.frame( 
>     y = 0,
>     x = seq(-1, 1, length.out = 30),
>     id = 0
>     ))
> 
> predict(fit, newdata = pred_grid2)
> 
> # > Error in MCMCglmm(fixed = object$Fixed$formula, random = object$Random$formula,  : trying to get slot "Dim" from an object of a basic class ("numeric") with no slots

Again I would investigate the error message 'trying to get slot "Dim"'. I looks like it expects a class of the 'Matrix' package. But right now I have no idea how to solve it and do not know of any examples.

> # ----------------------------------------------------------------------------------
> 
> On Thu, Jan 7, 2016 at 3:06 PM, Sven E. Templer <sven.templer at gmail.com> wrote:
> Hi Alberto,
> 
> avoid the error from predict by running
> 
> pred_grid$y <- 0
> 
> before
> 
> predict(fit, newdata = pred_grid)
> 
> Best,
> Sven
> 
> > On 06 Jan 2016, at 23:39, Alberto Gallano <alberto.gc8 at gmail.com> wrote:
> >
> > I'm trying to make predictions from an MCMCglmm model using new data. This
> > is a feature that was recently added to the predict.MCMCglmm function
> > (version 2.22). However, when I set things up as I would for other predict
> > methods, I get the following error:
> >
> >> Error in eval(expr, envir, enclos) : object 'y' not found
> >
> > where 'y' is my response vector. I'm including a simplified replicable
> > example below. Is the set up of the prediction grid different for the
> > MCMCglmm predict method compared with other methods?
> >
> > best,
> > Alberto
> >
> > #
> > ----------------------------------------------------------------------------------
> > library(MCMCglmm)
> >
> > set.seed(123)
> >
> > dat <- data.frame(x = rnorm(100), y = rnorm(100))
> >
> >
> > fit <- MCMCglmm(
> >
> >    fixed = y ~ x,
> >
> >    rcov = ~ units,
> >
> >    data = dat,
> >
> >    family = "gaussian",
> >
> >    pr = TRUE, pl = TRUE,
> >
> >    saveX = TRUE,  saveZ = TRUE,
> >
> >    nitt = 1.3e+4, thin = 10, burnin = 3e+3
> >
> > )
> >
> >
> > pred_grid <- data.frame(x = seq(-1, 1, length.out = 30))
> >
> >
> > predict(fit, newdata = pred_grid)
> >
> > #
> > ----------------------------------------------------------------------------------
> >
> >       [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-mixed-models at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
> 
> 



More information about the R-sig-mixed-models mailing list