addModerator: addModerator

Description Usage Arguments Value Examples

View source: R/main.R

Description

Adds moderators to a ctsem object. The general structure is y = (a1 + a2*moderatorValues[,1] + a3*moderatorValue[,2] + ...)*predictor

Usage

1
addModerator(model, moderatedVariable, variableInMatrix, moderatorValues)

Arguments

model

model of type ctsemFit or MxModel

moderatedVariable

label of the variable which is to be moderated (MUST be identical to the label of the parameter in the ctsemFit or MxModel object)

variableInMatrix

name of the mxMatrix where the moderatedVariable is located

moderatorValues

matrix with nrow = sample size and ncol = number of moderators. Contains for each person the values of each moderator. The colnames of this matrix will be used to label the moderators.

Value

an MxModel with moderator on the specific variable. The main effect is labeled moderatedVariable_MAIN, the interaction effects are in moderatedVariable_INTERACTION

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(ctsemOMX)
library(addModeratorToCtsem)

#### Example: ####
# Model: dx(t) = (a + a1*mod1 + a2*mod2)*x(t)dt + g*W(t)dt
n <- 100 # sample size
moderators <- matrix(rnorm(2*n,0,.05), ncol = 2) # values of the moderator variables

a <- -.2 # main effect
aInd <- -.2 - 1*moderators[,1] - .5*moderators[,2] # individual drift values (a+a1*mod1+a2*mod2)
hist(aInd)

# simulate data for a first order stochastic differential equation
data <- c()
for(i in 1:n){
  generatingModel<-ctModel(Tpoints=100,n.latent=1,n.TDpred=0,n.TIpred=0,n.manifest=1,
                           MANIFESTVAR=diag(0,1),
                           LAMBDA=diag(1,1),
                           DRIFT=matrix(c(aInd[i]),nrow=1),
                           DIFFUSION=matrix(c(1),1),
                           T0MEANS=matrix(0,ncol=1,nrow=1),
                           T0VAR=diag(1,1))
  dat <- ctGenerate(generatingModel,n.subjects=1,burnin=10)
  dat[,"id"] <- i
  data <-rbind(data, dat)
}

# set up model with ctsem
model<-ctModel(Tpoints=100,n.latent=1,n.TDpred=0,n.TIpred=0,n.manifest=1,
               MANIFESTVAR=diag(0,1),
               LAMBDA=diag(1,1),
               DRIFT=matrix(c("a"),nrow=1),
               DIFFUSION=matrix("g",1),
               T0MEANS=matrix(0,ncol=1,nrow=1),
               T0VAR=diag(1,1))

# fit model. IMPORTANT: objective must be set to "Kalman"
fitModel <- ctFit(dat = data,
                  ctmodelobj = model,
                  fit = T,
                  objective = "Kalman")

# create moderated model
moderatedModel <- addModerator(model = fitModel,
                               moderatedVariable = "a",
                               variableInMatrix = "DRIFT",
                               moderatorValues = moderators)

# fit moderated model
fit.moderatedModel <- mxRun(moderatedModel)

# the main effect is always labeled moderatedVariable_MAIN
fit.moderatedModel$a_MAIN
# the interaction effects are always labeled moderatedVariable_INTERACTION
fit.moderatedModel$a_INTERACTION

jhorzek/addModeratorToCtsem documentation built on Dec. 21, 2021, 12:02 a.m.