Skip to content

Commit ada7525

Browse files
author
Steve Walker
committed
added Zt functions to template approach
1 parent 28ed4b4 commit ada7525

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

R/templateApproach.R

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1-
##' Make template for relative covariance factor
1+
##' Create a section of a transposed random effects model matrix
2+
##'
3+
##' @param grp Grouping factor for a particular random effects term.
4+
##' @param mm Dense model matrix for a particular random effects term.
5+
##' @return Section of a random effects design matrix corresponding to a
6+
##' particular term.
7+
##' @examples
8+
##' ## consider a term (x | g) with:
9+
##' ## number of observations, n = 6
10+
##' ## number of levels, nl = 3
11+
##' ## number of columns ('predictors'), nc = 2
12+
##' (X <- cbind("(Intercept)"=1,x=1:6)) # an intercept in the first column
13+
##' # and 1:6 predictor in the other
14+
##' (g <- as.factor(letters[rep(1:3,2)])) # grouping factor
15+
##' nrow(X) # n = 6
16+
##' nrow(X) == length(g) # and consistent n between X and g
17+
##' ncol(X) # nc = 2
18+
##' nlevels(g) # nl = 3
19+
##' Zsection(g, X)
20+
mkZtSection <- function(grp,mm) {
21+
Jt <- as(as.factor(grp), Class="sparseMatrix")
22+
KhatriRao(Jt,t(mm))
23+
}
24+
25+
##' Make transposed random-effects model matrix
26+
mkZt <- function(grp,mm){
27+
ZtSections <- mapply(mkZtSection, grp, mm)
28+
rBind(ZtSections)
29+
}
30+
31+
32+
##' Make a single template for a relative covariance factor
233
##'
334
##' @param nc Number of columns in a dense model matrix for a particular
435
##' random effects term
@@ -16,7 +47,7 @@ mkTemplate <- function(nc){
1647
sparseMatrix(i=i,j=j,x=theta)
1748
}
1849

19-
##' Make templates for relative covariance factor
50+
##' Make list of templates for relative covariance factor
2051
mkTemplates <- function(nc) lapply(nc, mkTemplate)
2152

2253
##' Make vector of indices giving the mapping from theta to Lambdat
@@ -51,3 +82,4 @@ mkTheta <- function(templates){
5182
thetas <- lapply(templates, slot, "x")
5283
unlist(thetas)
5384
}
85+

0 commit comments

Comments
 (0)