# NOT RUN {
####=========================================####
#### For CRAN time limitations most lines in the
#### examples are silenced with one '#' mark,
#### remove them and run the examples using
#### command + shift + C |OR| control + shift + C
####=========================================####
####=========================================####
####=========================================####
#### EXAMPLE 1
#### breeding values for 3 variance component
#### with one variance covariance structure
####=========================================####
####=========================================####
data(CPdata)
head(CPpheno)
CPgeno[1:4,1:4]
#### create the variance-covariance matrix
A <- A.mat(CPgeno)
#### look at the data and fit the model
head(CPpheno)
mix1 <- mmer2(Yield~1,
random=~g(id) + Rowf + Colf,
rcov=~units,
G=list(id=A),
data=CPpheno)
summary(mix1)
#### calculate heritability
pin(mix1, h1 ~ V1/(V1+V4) )
# assumes diag(trait) structure for univariate models
# #### for multivariate models
# mix2 <- mmer2(cbind(Yield,color)~1,
# random = ~ us(trait):g(id) +
# diag(trait):Rowf +
# diag(trait):Colf,
# rcov = ~ us(trait):units,
# G=list(id=A),
# data=CPpheno)
# summary(mix2)
########################################################
########################################################
########################################################
########################################################
########################################################
########################################################
####=========================================####
####=========================================####
#### EXAMPLE 2
#### breeding values with 3 variance components
#### for hybrid prediction
####=========================================####
####=========================================####
# data(cornHybrid)
# hybrid2 <- cornHybrid$hybrid # extract cross data
# A <- cornHybrid$K
# K1 <- A[levels(hybrid2$GCA1), levels(hybrid2$GCA1)]; dim(K1)
# K2 <- A[levels(hybrid2$GCA2), levels(hybrid2$GCA2)]; dim(K2)
# S <- kronecker(K1, K2, make.dimnames=TRUE) ; dim(S)
#
# head(hybrid2)
# ans <- mmer2(Yield ~ Location,
# random = ~ g(GCA1) + g(GCA2) + g(SCA),
# rcov = ~ units,
# G=list(GCA1=K1, GCA2=K2, SCA=S),
# data=hybrid2)
# ans$var.comp
# summary(ans)
#
# #### you can specify heterogeneus variances
# ans <- mmer2(Yield ~ 1,
# random = ~ GCA2 + at(Location):GCA2,
# rcov = ~ at(Location):units,
# data=hybrid2)
# summary(ans)
#
# ##### example of multivariate model
# ans <- mmer2(cbind(Yield,PlantHeight) ~ 1,
# random = ~ us(trait):GCA2 + us(trait):at(Location):GCA2,
# rcov = ~ diag(trait):at(Location):units,
# data=hybrid2)
# summary(ans)
########################################################
########################################################
########################################################
########################################################
########################################################
########################################################
####=========================================####
#### EXAMPLE 3
#### different models with sommer
####=========================================####
# data(example)
# head(example)
#
# #### Univariate homogeneous variance models ####
#
# ## Compound simmetry (CS) model
# ans1 <- mmer2(Yield~Env,
# random= ~ Name + Env:Name,
# rcov= ~ units,
# data=example)
# summary(ans1)
#
# #### Univariate heterogeneous variance models ####
#
# ## Compound simmetry (CS) + Diagonal (DIAG) model
# ans2 <- mmer2(Yield~Env,
# random= ~Name + at(Env):Name,
# rcov= ~ at(Env):units,
# data=example)
# summary(ans2)
#
# ##### Multivariate homogeneous variance models ####
#
# ## Multivariate Compound simmetry (CS) model
# ans3 <- mmer2(cbind(Yield, Weight) ~ Env,
# random= ~ us(trait):Name + us(trait):Env:Name,
# rcov= ~ us(trait):units,
# data=example)
# summary(ans3)
#
# ##### Multivariate heterogeneous variance models ####
#
# ## Multivariate Compound simmetry (CS) + Diagonal (DIAG) model
# ans4 <- mmer2(cbind(Yield, Weight) ~ Env,
# random= ~ us(trait):Name + us(trait):at(Env):Name,
# rcov= ~ us(trait):at(Env):units,
# data=example)
# summary(ans4)
########################################################
########################################################
########################################################
########################################################
########################################################
########################################################
####=========================================####
#### EXAMPLE 4
#### comparison with lmer, install 'lme4'
#### and run the code below
####=========================================####
#### lmer cannot use var-cov matrices so we will not
#### use them in this comparison example
# library(lme4)
# library(sommer)
# data(cornHybrid)
# hybrid2 <- cornHybrid$hybrid
# fm1 <- lmer(Yield ~ Location + (1|GCA1) + (1|GCA2) + (1|SCA),
# data=hybrid2 )
# out <- mmer2(Yield ~ Location,
# random = ~ GCA1 + GCA2 + SCA,
# rcov = ~ units,
# data=hybrid2)
# summary(fm1)
# summary(out)
# ### same BLUPs for GCA1, GCA2, SCA than lme4
# plot(out$cond.residuals, residuals(fm1))
# plot(out$u.hat[[1]], ranef(fm1)$GCA1[,1])
# plot(out$u.hat[[2]], ranef(fm1)$GCA2[,1])
# vv=which(abs(out$u.hat[[3]]) > 0)
# plot(out$u.hat[[3]][vv,], ranef(fm1)$SCA[,1])
#
# ### a more complex model specifying which locations
# out2 <- mmer2(Yield ~ 1,
# random = ~ at(Location,c("3","4")):GCA2 + at(Location,c("3","4")):SCA,
# rcov = ~ at(Location):units,
# data=hybrid2)
# summary(out2)
# }
Run the code above in your browser using DataLab