Learn R Programming

lavaSearch2 (version 1.4)

createContrast: Create Contrast matrix

Description

Returns a contrast matrix corresponding an object. The contrast matrix will contains the hypotheses in rows and the model coefficients in columns.

Usage

createContrast(object, ...)

# S3 method for character createContrast(object, name.param, diff.first = FALSE, add.rowname = TRUE, rowname.rhs = TRUE, ...)

# S3 method for lm createContrast(object, par, add.variance, ...)

# S3 method for gls createContrast(object, par, add.variance, ...)

# S3 method for lme createContrast(object, par, add.variance, ...)

# S3 method for lvmfit createContrast(object, par = NULL, var.test = NULL, ...)

# S3 method for list createContrast(object, par = NULL, add.variance = NULL, var.test = NULL, ...)

# S3 method for mmm createContrast(object, par = NULL, add.variance = NULL, var.test = NULL, ...)

Arguments

object

a ls.lvmfit object.

...

[internal] Only used by the generic method.

name.param

[internal] the names of all the model coefficients.

diff.first

[logical] should the contrasts between the first and any of the other coefficients define the null hypotheses.

add.rowname

[internal] should a name be defined for each hypothesis.

rowname.rhs

should the right hand side of the null hypothesis be added to the name.

par

[vector of characters] expression defining the linear hypotheses to be tested. See the examples section.

add.variance

[logical] should the variance coefficients be considered as model coefficients? Required for lm, gls, and lme models.

var.test

[character] a regular expression that is used to identify the coefficients to be tested using grep. Each coefficient will be tested in a separate hypothesis. When this argument is used, the argument par is disregarded.

Value

A list containing

  • contrast [matrix] a contrast matrix corresponding to the left hand side of the linear hypotheses.

  • null [vector] the right hand side of the linear hypotheses.

  • Q [integer] the rank of the contrast matrix.

  • ls.contrast [list, optional] the contrast matrix corresponding to each submodel. Only present when the argument object is a list of models.

Details

One can initialize an empty contrast matrix setting the argumentpar to character(0).

When using multcomp::glht one should set the argument add.variance to FALSE. When using lavaSearch2::glht2 one should set the argument add.variance to TRUE.

Examples

Run this code
# NOT RUN {
## Simulate data
mSim <- lvm(X ~ Age + Treatment,
            Y ~ Gender + Treatment,
            c(Z1,Z2,Z3) ~ eta, eta ~ treatment,
            Age[40:5]~1)
latent(mSim) <- ~eta
categorical(mSim, labels = c("placebo","SSRI")) <- ~Treatment
categorical(mSim, labels = c("male","female")) <- ~Gender
n <- 1e2
set.seed(10)
df.data <- lava::sim(mSim,n)

## Estimate separate models
lmX <- estimate(lvm(X ~ -1 + Age + Treatment), data = df.data)
lmY <- estimate(lvm(Y ~ -1 + Gender + Treatment), data = df.data)
lvmZ <- estimate(lvm(c(Z1,Z2,Z3) ~ -1 + 1*eta, eta ~ -1 + Treatment), 
                 data = df.data)

## Contrast matrix for a given model
createContrast(lmX, par = "X~Age")
createContrast(lmX, par = c("X~Age=0","X~Age+5*X~TreatmentSSRI=0"))
createContrast(lmX, par = character(0))

## Contrast matrix for the join model
ls.lvm <- list(X = lmX, Y = lmY, Z = lvmZ)
createContrast(ls.lvm, var.test = "Treatment", add.variance = FALSE)
createContrast(ls.lvm, par = character(0), add.variance = FALSE)

## Contrast for multigroup models
m <- lvm(Y~Age+Treatment)
e <- estimate(list(m,m), data = split(df.data, df.data$Gender))
createContrast(e, par = "1@Y~TreatmentSSRI - 2@Y~TreatmentSSRI = 0")
createContrast(e, par = "2@Y~TreatmentSSRI - 1@Y~TreatmentSSRI = 0")
# }

Run the code above in your browser using DataLab