Skip to content

variance structure

1 message · Jarrod Hadfield

#
Hi Cristina,

CC-ing back to the list ....

With a bivariate model you can include a covariance between the random  
effects for zero/not-zero and the actual value of the response when  
not zero. In two univariate analyses you assume the covariance is  
zero. Its a small difference.

Below is an example with some made up data, its quite involved.

Cheers,

Jarrod


library(MASS)
library(MCMCglmm)

G<-matrix(c(1,0.5,0.5,1),2,2)
E<-diag(2)

# generate covariance matrices for random effects (G) and residuals (E)

u<-mvrnorm(40, c(0,0), G)
e<-mvrnorm(400, c(0,0), E)

# sample random effects and residuals

group<-gl(40,10)

# group identifier

y1<-u[,1][group]+e[,1]

# Gaussian data if it had been observed

y2<-rbinom(400, 1, plogis(u[,2][group]+e[,2]))

# binary trait 1 = Gaussian data observed 0 = recorded as zero.

y1<-y1[-which(y2==0)]

# remove Gaussian data that has been set to zero.

dat<-data.frame(y=c(y1,y2), trait=c(rep("nzero", length(y1)),  
rep("zero", length(y2))),  family=c(rep("gaussian", length(y1)),  
rep("categorical", length(y2))), group=c(group[-which(y2==0)], group))

# This is the odd bit! Pass the Gaussian data and the binary data as a  
single response y, indicate which process the data belong to in trait,  
and then specify the family each datum comes from.


prior=list(R=list(V=diag(2), nu=0.002, fix=2),  
G=list(G1=list(V=diag(2), nu=2, alpha.mu=rep(0,2),  
alpha.V=diag(2)*1000)))

# set up a prior - the key thing is that the observation-level  
variance of a binary variable cannot be estimated so I 've set it to  
one.

m1<-MCMCglmm(y~trait-1, random=~us(trait):group,  
rcov=~idh(trait):units, data=dat, family=NULL, prior=prior)

# The model has two fixed effects, where the first is the intercept  
for the Gauusian data and the second is the intercpet for the binary  
variable (on the logit scale).

# us(trait):group estimates a variance for the u's of each process AND  
te covariance. idh(trait):units fixes the residual covariance between  
the wto processes to zero (it cannot be estimated)

# family=NULL indicates that you have specified a distribution for  
each datum in the data.frame

# You will get an error message if for some groups all data are zero,  
or all data are non-zero. If you decide to go ahead with the analysis  
(again I think it is overkill) and you encounter this problem I will  
show you how to get round it.



Quoting Cristina Gomes <gomes_cristina at ymail.com> on Wed, 6 Apr 2011  
12:00:51 -0700 (PDT):