##-------------------------
## Example 1: Linear Mixed Models
## Independent cluster model with 50 clusters
## Nine covariates including a fixed and random intercept
## Please note the rpql is currently not optimized for LMMs!
##-------------------------
library(mvtnorm)
library(lme4)
n <- 50; m <- 10; p <- 8;
## Generate rows of a model matrix from a multivariate normal distribution with
## AR1 covariance structure.
H <- abs(outer(1:p, 1:p, "-"))
X <- cbind(1,rmvnorm(n*m,rep(0,p),sigma=0.5^H));
Z <- X
true_betas <- c(1,3,2,1.5,-1,0,0,0,0) ## 5 important fixed effects
true_D <- matrix(0,p+1,p+1) ## 3 important random effects
true_D[1:3,1:3] <- matrix(c(9,4.8,0.6,4.8,4,1,0.6,1,1),3,3,byrow=TRUE)
simy <- gendat.glmm(id = list(cluster = rep(1:n,each=m)), X = X, beta = true_betas,
Z = list(cluster = Z), D = list(cluster = true_D), phi = 1, family = gaussian())
## Notice how id, Z, and D all are lists with one element, and that
## the name of the first element (a generic name "cluster") is the
## same for all three lists.
## id is where the action takes place. In particular, id$cluster is
## designed so that the first m elements correspond to cluster 1,
## the second m elements correspond to cluster 2, and so forth.
## In turn, the first m rows of X and Z$cluster correspond
## to cluster 1, and so on.
if (FALSE) {
dat <- data.frame(y = simy$y, simy$X, simy$Z$cluster, simy$id)
fit_satlme4 <- lmer(y ~ X - 1 + (Z - 1 | cluster), data = dat,
REML = FALSE)
fit_sat <- build.start.fit(fit_satlme4, gamma = 2)
lambda_seq <- lseq(1e-4,1,length=100)
fit <- rpqlseq(y = simy$y, X = simy$X, Z = simy$Z, id = simy$id,
family = gaussian(), lambda = lambda_seq, pen.type = "adl",
pen.weights = fit_sat$pen.weights, start = fit_sat)
summary(fit$best.fit[[5]]) ## Second of the hybrid ICs
# apply(fit$collect.ics, 2, which.min) ## Look at best fit chosen by different ICs
}
## Please see other examples in help file for the \code{rpql} function.
Run the code above in your browser using DataLab