[R-sig-ME] MCMCglmm predictions using new data error
Jarrod Hadfield
j.hadfield at ed.ac.uk
Mon Feb 1 16:10:45 CET 2016
Hi Alberto,
having id=1:30 in the prediction grid should work.
I ideally it should work with a constant too, but I need to change the
source code from
Z<-Z[ordering,]
to
Z<-Z[ordering,,drop=FALSE]
on L576. I will include it in the next update.
Coercing a 1-D matrix into numeric rather than vector has to be one of
the most annoying design flaws in R!
Cheers,
Jarrod.
Quoting Alberto Gallano <alberto.gc8 at gmail.com> on Fri, 8 Jan 2016
11:51:33 -0500:
> 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).
>
> 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
> #
> ----------------------------------------------------------------------------------
>
> 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
>>
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>
>
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the R-sig-mixed-models
mailing list