contr.nhelmert: Normalized Helmert contrasts for parsimonious random effects...

View source: R/misc.R

contr.nhelmertR Documentation

Normalized Helmert contrasts for parsimonious random effects models

Description

Usage

contr.nhelmert(n, ...)

Arguments

n

a vector of levels for a factor or the number of levels.

Examples

zf <- factor(c("A","B","B","C"))
contrasts(zf) <- contr.nhelmert(3)
contrasts(zf)
library(nlme)
set.seed(123)
n_within <- 10
n_schools <- 20
n_treatments <- 3
dd <- expand.grid(id = 1:n_within, schoolid = 1:n_schools)
dd <- within(
   dd,
   {
      method <- factor(sample(LETTERS[1:n_treatments], nrow(dd), TRUE))
      methodn <- C(method, contr.nhelmert)
      methodmat <- contrasts(methodn)[methodn,]
      grade <- 10 + seq_len(n_treatments)[method] + rnorm(n_schools)[schoolid] + rnorm(nrow(dd))
   })
fitfull <- lme(grade ~ method, dd, random = ~ 1 + method | schoolid,
 control = list(returnObject = T))     
summary(fitfull)
getG(fitfull) %>% svd(nu=0,nv=0)
fitfulln <- lme(grade ~ method, dd, random = ~ 1 + methodn | schoolid,
 control = list(returnObject = T))     
summary(fitfulln)
getG(fitfulln)
getG(fitfulln) %>% svd(nu=0,nv=0)
fitpars1 <- lme(grade ~ method, dd, 
   random = list( schoolid = pdBlocked(list(pdDiag(~1), pdIdent(~ methodn - 1))) ))
fitpars2 <- lme(grade ~ method, dd, 
   random = list( schoolid = pdBlocked(list(pdDiag(~1), pdIdent(~ methodmat - 1))) ))
summary(fitpars1)
summary(fitpars2)
ranef(fitpars2) 

gmonette/spida2 documentation built on Aug. 20, 2023, 7:21 p.m.