[R-sig-ME] making predictions with MCMCglmm

YA x|nx|813 @end|ng |rom 126@com
Sat May 30 03:54:21 CEST 2020

Hi guys,

Thank you very much for the response.


MathAch is different from mathach, these are two different variables. The continuous 'MathAch' in the orignial dataset was used for the linear model m1, whereas the multinomial categorical 'mathach' I intentionally created was used for the subsequent multinomial logistic regression m3. Any other ideas?


I tried add both value of Sex and created a value of 0 for the outcome MathAch in m1, I still got errors. It looks like MCMCglmm prediction has a different logic than the glm prediction? Please see the codes and errors blow:

> predict(m1,data.frame(Sex='Male',SES=c(0.3,-0.8),MathAch=0),type='response')
Error in buildZ(rmodel.terms[r], data = data, nginverse = names(ginverse)) : 
  object School not found

> predict(m1,data.frame(Sex=c('Male','Female'),SES=c(0.3,-0.8),MathAch=0,School='1288'),type='response')
Error in predict.MCMCglmm(m1, data.frame(Sex = c("Male", "Female"), SES = c(0.3,  : 
  model for newdata has fixed effects not present in original model

> predict(m1,data.frame(Sex=c('Male'),SES=c(0.3,-0.8),MathAch=0,School='1288'),type='response')
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

> predict(m1,data.frame(Sex=c('Male','Female'),SES=c(0.3,-0.8),School='1288'),type='response')
Error in FUN(X[[i]], ...) : object 'MathAch' not found



------------------ Original ------------------
From: "sree datta"<sreedta8 using gmail.com>;
Date: Sat, May 30, 2020 00:21 AM
To: "Tom Houslay"<houslay using gmail.com>;
Cc: "r-sig-mixed-models"<r-sig-mixed-models using r-project.org>;"xinxi813"<xinxi813 using 126.com>;
Subject: Re: [R-sig-ME] making predictions with MCMCglmm

Dear YA

I noticed  the following in your code:

> &gt; dat$mathach[dat$MathAch&gt;=15]=2
> &gt; dat$mathach=as.factor(dat$mathach  

the dat$MathAch variable in line 1 will be processed as a variable different from dat$mathach on line 2 - Is this what you planned for? Since R is case-sensitive, this can create the problems you are seeing - 
The error "object mathach not found" is likely a direct result of that.


On Fri, May 29, 2020 at 7:39 AM Tom Houslay <houslay using gmail.com> wrote:

Hi YA,
 In your new data frame you just need to add a column for the response
 variable as well (set it to 0 or similar). You may have additional issues
 but that should get you over that first hurdle.
 Speaking of which, I've found when predicting from MCMCglmm that it doesn't
 like it when you only have a single value for any of your fixed effects --
 so you may want to expand your prediction data frames, for example for m1
 you could predict on those 2 SES values for both sexes (even if you are
 only interested in males, you can simply subset the predictions afterwards).
 On Fri, 29 May 2020 at 11:01, <r-sig-mixed-models-request using r-project.org>
 > Message: 1
 > Date: Fri, 29 May 2020 17:25:16 +0800
 > From: "YA" <xinxi813 using 126.com>
 > To: "r-sig-mixed-models" <r-sig-mixed-models using r-project.org>
 > Subject: [R-sig-ME] making predictions with MCMCglmm
 > Message-ID: <tencent_5701AD3CAB070C10E27910A2F3481E517A06 using qq.com>
 > Content-Type: text/plain; charset="utf-8"
 > Dear list,
 > I am still working on the MCMCglmm predictions. I realized that I didnt
 provide a reproducible code in my last email, which makes people here lack
 of clues for helping me. I am providing a reproducible example this time
 using datasets from the nlme package, so if you have any experience on this
 package, please give me some advice on programming the predictions. As you
 can see below, same error occured on different models, I guess something is
 wrong with my code. Thank you very much.
 > &gt; library(MCMCglmm)
 > &gt; library(nlme)
 > &gt; data(MathAchieve,package='nlme')
 > &gt; data(MathAchSchool,package='nlme')
 > &gt; dat=merge(MathAchSchool,MathAchieve,by='School')
 > &gt; dat$mathach[dat$MathAch<5]=0
 > &gt; dat$mathach[dat$MathAch&gt;=5 &amp; dat$MathAch<15]=1
 > &gt; dat$mathach[dat$MathAch&gt;=15]=2
 > &gt; dat$mathach=as.factor(dat$mathach)
 > &gt; str(dat)
 > # prediction on a continous outcome 'MathAch'
 > &gt; m1=MCMCglmm(MathAch~Sex+SES,random=~School+SES,dat=dat,verbose=F)
 > &gt; summary(m1)
 > &gt; predict(m1,data.frame(Sex='Male',SES=c(0.3,-0.8)),type='response')
 > Error in FUN(X[[i]], ...) : object 'MathAch' not found
 > # prediction on a binary outcome 'Sex'
 > &gt;
 > &gt; summary(m2)
 > &gt;
 > Error in FUN(X[[i]], ...) : object 'Sex' not found
 > # prediction on a three category unordered multinomial outcome 'mathach'
 > &gt;
 > &gt; summary(m3)
 > &gt;
 > Error in FUN(X[[i]], ...) : object 'mathach' not found
 > Best regards,
 > YA
 >         [[alternative HTML version deleted]]
 > ------------------------------
 > Subject: Digest Footer
 > _______________________________________________
 > R-sig-mixed-models mailing list
 > R-sig-mixed-models using r-project.org
 > https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
 > ------------------------------
 > End of R-sig-mixed-models Digest, Vol 161, Issue 44
 > ***************************************************
         [[alternative HTML version deleted]]
 R-sig-mixed-models using r-project.org mailing list
	[[alternative HTML version deleted]]

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