Skip to content
Prev 18511 / 20628 Next

making predictions with MCMCglmm

ya
Hi guys,


Thank you very much for the response.


Sree:


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?



Tom:
?

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]]) : 
&nbsp; contrasts can be applied only to factors with 2 or more levels


&gt; 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







Cheers,


YA




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



Dear YA

I noticed&nbsp; the following in your code:


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



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


Sree
On Fri, May 29, 2020 at 7:39 AM Tom Houslay <houslay at gmail.com&gt; 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).
 
 Cheers
 
 Tom
 
 
 On Fri, 29 May 2020 at 11:01, <r-sig-mixed-models-request at r-project.org&gt;
 wrote:
 &gt;
 &gt;
 &gt;
 &gt; Message: 1
 &gt; Date: Fri, 29 May 2020 17:25:16 +0800
 &gt; From: "YA" <xinxi813 at 126.com&gt;
 &gt; To: "r-sig-mixed-models" <r-sig-mixed-models at r-project.org&gt;
 &gt; Subject: [R-sig-ME] making predictions with MCMCglmm
 &gt; Message-ID: <tencent_5701AD3CAB070C10E27910A2F3481E517A06 at qq.com&gt;
 &gt; Content-Type: text/plain; charset="utf-8"
 &gt;
 &gt; Dear list,
 &gt;
 &gt;
 &gt; 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;
 &gt;
 &gt; &amp;gt; library(MCMCglmm)
 &gt; &amp;gt; library(nlme)
 &gt; &amp;gt; data(MathAchieve,package='nlme')
 &gt; &amp;gt; data(MathAchSchool,package='nlme')
 &gt; &amp;gt; dat=merge(MathAchSchool,MathAchieve,by='School')
 &gt; &amp;gt; dat$mathach[dat$MathAch<5]=0
 &gt; &amp;gt; dat$mathach[dat$MathAch&amp;gt;=5 &amp;amp; dat$MathAch<15]=1
 &gt; &amp;gt; dat$mathach[dat$MathAch&amp;gt;=15]=2
 &gt; &amp;gt; dat$mathach=as.factor(dat$mathach)
 &gt; &amp;gt; str(dat)
 &gt;
 &gt;
 &gt; # prediction on a continous outcome 'MathAch'
 &gt;
 &gt; &amp;gt; m1=MCMCglmm(MathAch~Sex+SES,random=~School+SES,dat=dat,verbose=F)
 &gt; &amp;gt; summary(m1)
 &gt; &amp;gt; predict(m1,data.frame(Sex='Male',SES=c(0.3,-0.8)),type='response')
 &gt; Error in FUN(X[[i]], ...) : object 'MathAch' not found
 &gt;
 &gt;
 &gt; # prediction on a binary outcome 'Sex'
 &gt; &amp;gt;
 m2=MCMCglmm(Sex~SES,random=~School+SES,data=dat,family='categorical',verbose=F)
 &gt; &amp;gt; summary(m2)
 &gt; &amp;gt;
 predict(m2,data.frame(SES=0.5,School='1224'),marginal=NULL,type='response')
 &gt; Error in FUN(X[[i]], ...) : object 'Sex' not found
 &gt;
 &gt;
 &gt;
 &gt; # prediction on a three category unordered multinomial outcome 'mathach'
 &gt;
 &gt; &amp;gt;
 m3=MCMCglmm(mathach~SES,random=~School+SES,data=dat,rcov=~us(trait):units,family='categorical',verbose=F)
 &gt; &amp;gt; summary(m3)
 &gt; &amp;gt;
 predict(m3,data.frame(SES=0.5,School='1224'),marginal=NULL,type='response')
 &gt; Error in FUN(X[[i]], ...) : object 'mathach' not found
 &gt;
 &gt;
 &gt; Best regards,
 &gt;
 &gt;
 &gt; YA
 &gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[[alternative HTML version deleted]]
 &gt;
 &gt;
 &gt;
 &gt; ------------------------------
 &gt;
 &gt; Subject: Digest Footer
 &gt;
 &gt; _______________________________________________
 &gt; R-sig-mixed-models mailing list
 &gt; R-sig-mixed-models at r-project.org
 &gt; https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
 &gt;
 &gt;
 &gt; ------------------------------
 &gt;
 &gt; End of R-sig-mixed-models Digest, Vol 161, Issue 44
 &gt; ***************************************************
 
 &nbsp; &nbsp; &nbsp; &nbsp; [[alternative HTML version deleted]]
 
 _______________________________________________
 R-sig-mixed-models at r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models