Get started

library(varTestnlme)

The varTesnlme package is very easy to use. Below are small examples on how to run it for linear, generalized linear and nonlinear mixed-effect models.

Mixed-effect models can be run using nlme or lme4, but also using saemix. varTestnlme can be used to compare two nested models using likelihood ratio tests, where the variance of at least one random effect is tested equal to 0. Fixed effects can also be tested simultaneously, as well as covariances.

# Load the packages
library(nlme)
library(lme4)
library(saemix)

Linear models

Here we focus on models run using lme4 and nlme, but saemix can also be used.

Case 1 : testing the variance of one random effect

We illustrate the results on the Orthodont dataset, which is part of the nlme package. We are interested in modelling the distance between the pituitary annd the pterygomaxillary fissure (in mm) as a function of age, in 27 children. We will fit a random slope and random intercept model, and test whether the slope is random or not.

We first need to fit the two nested models: the full model corresponding to \(H_1\) and the null model corresponding to \(H_0\), where there is no random effect associated to age.

#> Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
#> Model failed to converge with max|grad| = 0.0464695 (tol = 0.002, component 1)

Now we can run the likelihood ratio test using the varTestnlme package.

Case 2 : testing the variance of one effect with uncorrelated random effects

Case 3 : testing all the variances

In the previous section, the weights of the chi-bar-square distribution where available explicitly. However, it is not always the case. By default, since the computation of these weights can be time consuming, the function is computing bounds on the p-value. In many cases this can be enough to decide whether to reject or not the null hypothesis. If more precision is wanted or needed, it is possible to specify it via the option pval.comp, which then needs to be set to either pval.comp="approx" or to pval.comp="both". In both cases, the control argument can be used to control the computation process. It is a list which contains three slots: M (default to 5000), the size of the Monte Carlo computation, parallel (default to FALSE) to specify whether computation should be parallelized, and nbcores (default to 1) to set the number of cores to be used in case of parallel computing.

#>        Length         Class          Mode 
#>             1 varTestObject            S4
#>        Length         Class          Mode 
#>             1 varTestObject            S4

By default, the FIM is extracted from the packages, but it is also possible to compute it via parametric bootstrap. In this case, simply use the option fim="compute". The default bootstrap sampling size is B=1000 but it can be changed. To get the exact p-value one can use

#>        Length         Class          Mode 
#>             1 varTestObject            S4
#>        Length         Class          Mode 
#>             1 varTestObject            S4

Generalized linear model

m1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
             family = binomial, data = cbpp)
m0 <- glm(cbind(incidence, size - incidence) ~ period,
             family = binomial, data = cbpp)
varTest(m1,m0)

Nonlinear model

# with nlme
fm1Theo.nlme <- nlme(conc ~ SSfol(Dose, Time, lKe, lKa, lCl),
                     Theoph, 
                     fixed = lKe + lKa + lCl ~ 1,
                     start=c( -2.4, 0.45, -3.2),
                     random = pdSymm(lKa + lCl ~ 1))
fm2Theo.nlme <- nlme(conc ~ SSfol(Dose, Time, lKe, lKa, lCl),
                     Theoph, 
                     fixed = lKe + lKa + lCl ~ 1,
                     start=c( -2.4, 0.45, -3.2),
                     random = pdDiag(lCl ~ 1))
varTest(fm1Theo.nlme,fm2Theo.nlme, pval.comp = "both")

# with lme4
Th.start  <- c(lKe = -2.4, lKa = 0.45, lCl = -3.2)
nm1  <- nlmer(conc ~ SSfol(Dose , Time ,lKe , lKa , lCl) ~ 
                0+lKe+lKa+lCl +(lKe+lKa+lCl|Subject), 
              nAGQ=0, 
              Theoph,
              start = Th.start)
nm0  <- nlmer(conc ~ SSfol(Dose , Time ,lKe , lKa , lCl) ~ 
                0+lKe+lKa+lCl +(lKa+lCl|Subject), 
              nAGQ=0, 
              Theoph,
              start = Th.start)
varTest(nm1,nm0)

Testing for the presence of randomness in the model.

fm1 <- nlme(conc ~ SSfol(Dose, Time, lKe, lKa, lCl),
                     Theoph, 
                     fixed = lKe + lKa + lCl ~ 1,
                     start=c( -2.4, 0.45, -3.2),
                     random = pdDiag(lKe + lKa + lCl ~ 1))
fm0 <- nls(conc ~ SSfol(Dose, Time, lKe, lKa, lCl),
                     Theoph, 
                     start=list(lKe=-2.4,lKa=0.45,lCl=-3.2))
varTest(fm1,fm0)