Translating lme4 models to sommer

Giovanny Covarrubias-Pazaran

2026-10-23

The sommer package was developed to provide R users with a powerful and reliable multivariate mixed model solver for different genetic and non-genetic analyses in diploid and polyploid organisms. This package allows the user to estimate variance components for a mixed model with the advantages of specifying the variance-covariance structure of the random effects, specifying heterogeneous variances, and obtaining other parameters such as BLUPs, BLUEs, residuals, fitted values, variances for fixed and random effects, etc. The core algorithms of the package are coded in C++ using the Armadillo library to optimize dense matrix operations common in the derect-inversion algorithms. Although the vignette shows examples using the mmes function with the default direct inversion algorithm (formulation=“direct”) the Henderson’s approach can be faster when the number of records surpasses the number of coefficients to estimate and setting the henderson argument to TRUE can bring significant speed ups.

The purpose of this vignette is to show how to translate the syntax formula from lme4 models to sommer models. Feel free to remove the comment marks from the lme4 code so you can compare the results.

  1. Random slopes with same intercept
  2. Random slopes and random intercepts (without correlation)
  3. Random slopes and random intercepts (with correlation)
  4. Random slopes with a different intercept
  5. Other models not available in lme4

1) Random slopes

This is the simplest model people use when a random effect is desired and the levels of the random effect are considered to have the same intercept.

# install.packages("lme4")
# library(lme4)
library(sommer)
data(DT_sleepstudy, package="enhancer")
DT <- DT_sleepstudy
###########
## lme4
###########
# fm1 <- lmer(Reaction ~ Days + (1 | Subject), data=DT)
# summary(fm1) # or vc <- VarCorr(fm1); print(vc,comp=c("Variance"))
# Random effects:
#  Groups   Name        Variance Std.Dev.
#  Subject  (Intercept) 1378.2   37.12   
#  Residual              960.5   30.99   
# Number of obs: 180, groups:  Subject, 18
###########
## sommer
###########
fm2 <- mmes(Reaction ~ Days,
  random= ~ Subject,
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                term parameter  estimate StdError   Zratio
## 1 vsm(ism(Subject))    sigma2 1378.2022 505.7744 2.724935
## 2   vsm(ism(units))    sigma2  960.3449 107.0296 8.972701

2) Random slopes and random intercepts (without correlation)

This is the a model where you assume that the random effect has different intercepts based on the levels of another variable. In addition the || in lme4 assumes that slopes and intercepts have no correlation.

###########
## lme4
###########
# fm1 <- lmer(Reaction ~ Days + (Days || Subject), data=DT)
# summary(fm1) # or vc <- VarCorr(fm1); print(vc,comp=c("Variance"))
# Random effects:
#  Groups    Name        Variance Std.Dev.
#  Subject   (Intercept) 627.57   25.051  
#  Subject.1 Days         35.86    5.988  
#  Residual              653.58   25.565  
# Number of obs: 180, groups:  Subject, 18
###########
## sommer
###########
fm2 <- mmes(Reaction ~ Days,
  random= ~ Subject + vsm(ism(Days), ism(Subject)),
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                           term parameter  estimate  StdError   Zratio
## 1            vsm(ism(Subject))    sigma2 627.72619 283.71771 2.212503
## 2 vsm(ism(Days), ism(Subject))    sigma2  35.84895  14.52471 2.468135
## 3              vsm(ism(units))    sigma2 653.57166  76.65528 8.526114

Notice that Days is a numerical (not factor) variable.

3) Random slopes and random intercepts (with correlation)

This is the a model where you assume that the random effect has different intercepts based on the levels of another variable. In addition a single | in lme4 assumes that slopes and intercepts have a correlation to be estimated.

###########
## lme4
###########
# fm1 <- lmer(Reaction ~ Days + (Days | Subject), data=DT)
# summary(fm1) # or # vc <- VarCorr(fm1); print(vc,comp=c("Variance"))
# Random effects:
#  Groups   Name        Variance Std.Dev. Corr
#  Subject  (Intercept) 612.10   24.741       
#           Days         35.07    5.922   0.07
#  Residual             654.94   25.592       
# Number of obs: 180, groups:  Subject, 18
###########
## sommer
###########
fm2 <- mmes(Reaction ~ Days,
  # control = mmes_control(formulation="henderson"),
            random= ~ vsm(usm(cbind(1,Days)), ism(Subject)),
  data=DT,
  control = mmes_control(convergence = list(maxIters = 200),
  execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                                     term         parameter   estimate  StdError
## 1 vsm(usm(cbind(1, Days)), ism(Subject))        variance[] 612.019224 288.74643
## 2 vsm(usm(cbind(1, Days)), ism(Subject)) covariance[Days,]   9.664236  46.66108
## 3 vsm(usm(cbind(1, Days)), ism(Subject))    variance[Days]  35.057071  14.77457
## 4                        vsm(ism(units))            sigma2 654.941025  77.18554
##      Zratio
## 1 2.1195733
## 2 0.2071156
## 3 2.3727978
## 4 8.4852814
cov2cor(fm2$theta[[1]])
##           [,1]      [,2]
## [1,] 1.0000000 0.0659777
## [2,] 0.0659777 1.0000000

Notice that this last model require a new function called covm() which creates the two random effects as before but now they have to be encapsulated in covm() instead of just added.

4) Random slopes with a different intercept

This is the a model where you assume that the random effect has different intercepts based on the levels of another variable but there’s not a main effect. The 0 in the intercept in lme4 assumes that random slopes interact with an intercept but without a main effect.

###########
## lme4
###########
# fm1 <- lmer(Reaction ~ Days + (0 + Days | Subject), data=DT)
# summary(fm1) # or vc <- VarCorr(fm1); print(vc,comp=c("Variance"))
# Random effects:
#  Groups   Name Variance Std.Dev.
#  Subject  Days  52.71    7.26   
#  Residual      842.03   29.02   
# Number of obs: 180, groups:  Subject, 18
###########
## sommer
###########
fm2 <- mmes(Reaction ~ Days,
  random= ~ vsm(ism(Days), ism(Subject)),
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                           term parameter  estimate StdError   Zratio
## 1 vsm(ism(Days), ism(Subject))    sigma2  52.70579 19.09378 2.760365
## 2              vsm(ism(units))    sigma2 842.02965 93.84896 8.972179

4) Other models available in sommer but not in lme4

One of the strengths of sommer is the availability of other variance covariance structures. In this section we show 4 models available in sommer that are not available in lme4 and might be useful.

library(orthopolynom)
## diagonal model
fm2 <- mmes(Reaction ~ Days,
  random= ~ vsm(dsm(Daysf), ism(Subject)),
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                             term   parameter  estimate  StdError   Zratio
## 1  vsm(dsm(Daysf), ism(Subject)) variance[0]  656.2651  114.9900 5.707151
## 2  vsm(dsm(Daysf), ism(Subject)) variance[1]  713.5638  445.0486 1.603339
## 3  vsm(dsm(Daysf), ism(Subject)) variance[2]  516.6946  383.4608 1.347451
## 4  vsm(dsm(Daysf), ism(Subject)) variance[3] 1072.7899  556.7946 1.926725
## 5  vsm(dsm(Daysf), ism(Subject)) variance[4] 1371.9225  651.2555 2.106581
## 6  vsm(dsm(Daysf), ism(Subject)) variance[5] 2216.1361  921.8138 2.404104
## 7  vsm(dsm(Daysf), ism(Subject)) variance[6] 3427.6151 1317.2583 2.602083
## 8  vsm(dsm(Daysf), ism(Subject)) variance[7] 2056.3370  872.7117 2.356261
## 9  vsm(dsm(Daysf), ism(Subject)) variance[8] 3114.2466 1218.9363 2.554889
## 10 vsm(dsm(Daysf), ism(Subject)) variance[9] 3989.4234 1510.9733 2.640300
## 11               vsm(ism(units))      sigma2  362.9853  255.5421 1.420452
## unstructured model
fm2 <- mmes(Reaction ~ Days,
  random= ~ vsm(usm(Daysf), ism(Subject)),
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                             term       parameter   estimate    StdError
## 1  vsm(usm(Daysf), ism(Subject))     variance[0] 1268.77852  453.444065
## 2  vsm(usm(Daysf), ism(Subject)) covariance[1,0] 1025.26011  412.018184
## 3  vsm(usm(Daysf), ism(Subject)) covariance[2,0]  542.32445  302.772674
## 4  vsm(usm(Daysf), ism(Subject)) covariance[3,0]  800.96870  418.325945
## 5  vsm(usm(Daysf), ism(Subject)) covariance[4,0]  760.09680  430.117020
## 6  vsm(usm(Daysf), ism(Subject)) covariance[5,0]  935.56751  543.891217
## 7  vsm(usm(Daysf), ism(Subject)) covariance[6,0]  668.59057  587.829326
## 8  vsm(usm(Daysf), ism(Subject)) covariance[7,0]  934.99295  516.331803
## 9  vsm(usm(Daysf), ism(Subject)) covariance[8,0]  923.15021  609.787646
## 10 vsm(usm(Daysf), ism(Subject)) covariance[9,0] 1443.87797  724.815191
## 11 vsm(usm(Daysf), ism(Subject))     variance[1] 1283.40078  456.728924
## 12 vsm(usm(Daysf), ism(Subject)) covariance[2,1]  830.27716  337.251946
## 13 vsm(usm(Daysf), ism(Subject)) covariance[3,1] 1139.78810  461.506048
## 14 vsm(usm(Daysf), ism(Subject)) covariance[4,1] 1041.70165  462.612795
## 15 vsm(usm(Daysf), ism(Subject)) covariance[5,1] 1182.37196  572.665606
## 16 vsm(usm(Daysf), ism(Subject)) covariance[6,1]  853.23543  602.848268
## 17 vsm(usm(Daysf), ism(Subject)) covariance[7,1]  929.33238  515.203643
## 18 vsm(usm(Daysf), ism(Subject)) covariance[8,1] 1047.58355  622.380620
## 19 vsm(usm(Daysf), ism(Subject)) covariance[9,1] 1517.96895  735.589826
## 20 vsm(usm(Daysf), ism(Subject))     variance[2]  864.18179  305.515212
## 21 vsm(usm(Daysf), ism(Subject)) covariance[3,2] 1058.69346  395.030295
## 22 vsm(usm(Daysf), ism(Subject)) covariance[4,2]  912.38538  383.888145
## 23 vsm(usm(Daysf), ism(Subject)) covariance[5,2]  861.24333  456.261634
## 24 vsm(usm(Daysf), ism(Subject)) covariance[6,2]  917.77390  511.519355
## 25 vsm(usm(Daysf), ism(Subject)) covariance[7,2]  925.94761  434.135584
## 26 vsm(usm(Daysf), ism(Subject)) covariance[8,2]  833.40150  502.396582
## 27 vsm(usm(Daysf), ism(Subject)) covariance[9,2]  970.10478  569.046106
## 28 vsm(usm(Daysf), ism(Subject))     variance[3] 1625.70109  573.919675
## 29 vsm(usm(Daysf), ism(Subject)) covariance[4,3] 1592.55514  578.038076
## 30 vsm(usm(Daysf), ism(Subject)) covariance[5,3] 1675.69555  687.760610
## 31 vsm(usm(Daysf), ism(Subject)) covariance[6,3] 1787.99565  765.354119
## 32 vsm(usm(Daysf), ism(Subject)) covariance[7,3] 1284.71638  602.347291
## 33 vsm(usm(Daysf), ism(Subject)) covariance[8,3] 1609.52031  744.950281
## 34 vsm(usm(Daysf), ism(Subject)) covariance[9,3] 1745.53527  830.511762
## 35 vsm(usm(Daysf), ism(Subject))     variance[4] 1821.88734  632.943941
## 36 vsm(usm(Daysf), ism(Subject)) covariance[5,4] 2005.33942  755.257543
## 37 vsm(usm(Daysf), ism(Subject)) covariance[6,4] 2079.29219  829.907539
## 38 vsm(usm(Daysf), ism(Subject)) covariance[7,4] 1551.39672  653.864246
## 39 vsm(usm(Daysf), ism(Subject)) covariance[8,4] 2031.24336  822.544602
## 40 vsm(usm(Daysf), ism(Subject)) covariance[9,4] 2200.83568  914.359995
## 41 vsm(usm(Daysf), ism(Subject))     variance[5] 2934.25537 1029.107633
## 42 vsm(usm(Daysf), ism(Subject)) covariance[6,5] 2605.86800 1054.744915
## 43 vsm(usm(Daysf), ism(Subject)) covariance[7,5] 1943.83905  836.864790
## 44 vsm(usm(Daysf), ism(Subject)) covariance[8,5] 3061.11908 1125.462884
## 45 vsm(usm(Daysf), ism(Subject)) covariance[9,5] 3240.13035 1235.852640
## 46 vsm(usm(Daysf), ism(Subject))     variance[6] 3988.45679 1363.983743
## 47 vsm(usm(Daysf), ism(Subject)) covariance[7,6] 2307.78914  961.835533
## 48 vsm(usm(Daysf), ism(Subject)) covariance[8,6] 2930.00477 1196.080618
## 49 vsm(usm(Daysf), ism(Subject)) covariance[9,6] 2213.36722 1213.841096
## 50 vsm(usm(Daysf), ism(Subject))     variance[7] 2534.50387  883.079248
## 51 vsm(usm(Daysf), ism(Subject)) covariance[8,7] 2435.35192  980.468596
## 52 vsm(usm(Daysf), ism(Subject)) covariance[9,7] 2401.95756 1059.128113
## 53 vsm(usm(Daysf), ism(Subject))     variance[8] 3813.09647 1337.192658
## 54 vsm(usm(Daysf), ism(Subject)) covariance[9,8] 3850.07266 1432.545785
## 55 vsm(usm(Daysf), ism(Subject))     variance[9] 4812.81819 1692.833775
## 56               vsm(ism(units))          sigma2   20.04725    6.613576
##      Zratio
## 1  2.798093
## 2  2.488386
## 3  1.791194
## 4  1.914700
## 5  1.767186
## 6  1.720137
## 7  1.137389
## 8  1.810837
## 9  1.513888
## 10 1.992064
## 11 2.809984
## 12 2.461890
## 13 2.469714
## 14 2.251779
## 15 2.064681
## 16 1.415340
## 17 1.803816
## 18 1.683188
## 19 2.063608
## 20 2.828605
## 21 2.680031
## 22 2.376696
## 23 1.887608
## 24 1.794211
## 25 2.132854
## 26 1.658852
## 27 1.704791
## 28 2.832628
## 29 2.755104
## 30 2.436452
## 31 2.336168
## 32 2.132850
## 33 2.160574
## 34 2.101759
## 35 2.878434
## 36 2.655173
## 37 2.505450
## 38 2.372659
## 39 2.469463
## 40 2.406968
## 41 2.851262
## 42 2.470614
## 43 2.322764
## 44 2.719876
## 45 2.621777
## 46 2.924123
## 47 2.399359
## 48 2.449672
## 49 1.823441
## 50 2.870075
## 51 2.483865
## 52 2.267863
## 53 2.851569
## 54 2.687574
## 55 2.843054
## 56 3.031227
## random regression (legendre polynomials)
fm2 <- mmes(Reaction ~ Days,
  random= ~ vsm(dsm(leg(Days,1)), ism(Subject)),
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                                   term      parameter  estimate   StdError
## 1 vsm(dsm(leg(Days, 1)), ism(Subject)) variance[leg0] 2817.4417 1011.41776
## 2 vsm(dsm(leg(Days, 1)), ism(Subject)) variance[leg1]  473.4674  199.55780
## 3                      vsm(ism(units))         sigma2  654.9410   77.18554
##     Zratio
## 1 2.785636
## 2 2.372583
## 3 8.485282
## unstructured random regression (legendre)
fm2 <- mmes(Reaction ~ Days,
  random= ~ vsm(usm(leg(Days,1)), ism(Subject)),
  data=DT,
  control = mmes_control(execution = list(inverseTol = 1e-6),
  diagnostics = list(verbose = FALSE)))
summary(fm2)$varcomp
##                                   term             parameter  estimate
## 1 vsm(usm(leg(Days, 1)), ism(Subject))        variance[leg0] 2817.4277
## 2 vsm(usm(leg(Days, 1)), ism(Subject)) covariance[leg1,leg0]  869.9652
## 3 vsm(usm(leg(Days, 1)), ism(Subject))        variance[leg1]  473.4643
## 4                      vsm(ism(units))                sigma2  654.9410
##     StdError   Zratio
## 1 1011.41057 2.785642
## 2  381.08010 2.282893
## 3  199.55625 2.372586
## 4   77.18554 8.485281

Literature

Covarrubias-Pazaran G. 2016. Genome assisted prediction of quantitative traits using the R package sommer. PLoS ONE 11(6):1-15.

Covarrubias-Pazaran G. 2018. Software update: Moving the R package sommer to multivariate mixed models for genome-assisted prediction. doi: https://doi.org/10.1101/354639

Bernardo Rex. 2010. Breeding for quantitative traits in plants. Second edition. Stemma Press. 390 pp.

Gilmour et al. 1995. Average Information REML: An efficient algorithm for variance parameter estimation in linear mixed models. Biometrics 51(4):1440-1450.

Henderson C.R. 1975. Best Linear Unbiased Estimation and Prediction under a Selection Model. Biometrics vol. 31(2):423-447.

Kang et al. 2008. Efficient control of population structure in model organism association mapping. Genetics 178:1709-1723.

Lee, D.-J., Durban, M., and Eilers, P.H.C. (2013). Efficient two-dimensional smoothing with P-spline ANOVA mixed models and nested bases. Computational Statistics and Data Analysis, 61, 22 - 37.

Lee et al. 2015. MTG2: An efficient algorithm for multivariate linear mixed model analysis based on genomic information. Cold Spring Harbor. doi: https://doi.org/10.1101/027201.

Maier et al. 2015. Joint analysis of psychiatric disorders increases accuracy of risk prediction for schizophrenia, bipolar disorder, and major depressive disorder. Am J Hum Genet; 96(2):283-294.

Rodriguez-Alvarez, Maria Xose, et al. Correcting for spatial heterogeneity in plant breeding experiments with P-splines. Spatial Statistics 23 (2018): 52-71.

Searle. 1993. Applying the EM algorithm to calculating ML and REML estimates of variance components. Paper invited for the 1993 American Statistical Association Meeting, San Francisco.

Yu et al. 2006. A unified mixed-model method for association mapping that accounts for multiple levels of relatedness. Genetics 38:203-208.

Tunnicliffe W. 1989. On the use of marginal likelihood in time series model estimation. JRSS 51(1):15-27.