if (FALSE) {
#############################################################################
# EXAMPLE 1: data.lsem01 | Age differentiation
#############################################################################
data(data.lsem01, package="sirt")
dat <- data.lsem01
# specify lavaan model
lavmodel <- "
F=~ v1+v2+v3+v4+v5
F ~~ 1*F"
# define grid of moderator variable age
moderator.grid <- seq(4,23,1)
#********************************
#*** Model 1: estimate LSEM with bandwidth 2
mod1 <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, std.lv=TRUE)
summary(mod1)
plot(mod1, parindex=1:5)
# perform permutation test for Model 1
pmod1 <- sirt::lsem.permutationTest( mod1, B=10 )
# only for illustrative purposes the number of permutations B is set
# to a low number of 10
summary(pmod1)
plot(pmod1, type="global")
#** estimate Model 1 based on pseudo weights
mod1b <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, std.lv=TRUE, pseudo_weights=50 )
summary(mod1b)
#** estimation with sampling weights
# generate random sampling weights
set.seed(987)
weights <- stats::runif(nrow(dat), min=.4, max=3 )
mod1c <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, sampling_weights=weights)
summary(mod1c)
#********************************
#*** Model 2: estimate multiple group model with 4 age groups
# define breaks for age groups
moderator.grid <- seq( 3.5, 23.5, len=5) # 4 groups
# estimate model
mod2 <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, type="MGM", std.lv=TRUE)
summary(mod2)
# output step functions
smod2 <- sirt::lsem.MGM.stepfunctions( object=mod2, moderator.grid=seq(4,23,1) )
str(smod2)
#********************************
#*** Model 3: define standardized loadings as derived variables
# specify lavaan model
lavmodel <- "
F=~ a1*v1+a2*v2+a3*v3+a4*v4
v1 ~~ s1*v1
v2 ~~ s2*v2
v3 ~~ s3*v3
v4 ~~ s4*v4
F ~~ 1*F
# standardized loadings
l1 :=a1 / sqrt(a1^2 + s1 )
l2 :=a2 / sqrt(a2^2 + s2 )
l3 :=a3 / sqrt(a3^2 + s3 )
l4 :=a4 / sqrt(a4^2 + s4 )
"
# estimate model
mod3 <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, std.lv=TRUE)
summary(mod3)
plot(mod3)
#********************************
#*** Model 4: estimate LSEM and automatically include standardized solutions
lavmodel <- "
F=~ 1*v1+v2+v3+v4
F ~~ F"
mod4 <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, standardized=TRUE)
summary(mod4)
# permutation test (use only few permutations for testing purposes)
pmod1 <- sirt::lsem.permutationTest( mod4, B=3 )
#**** compute LSEM local weights
wgt <- sirt::lsem_local_weights(data.mod=dat$age, moderator.grid=moderator.grid,
h=2)$weights
print(str(weights))
#********************************
#*** Model 5: invariance parameter constraints and other constraints
lavmodel <- "
F=~ 1*v1+v2+v3+v4
F ~~ F"
moderator.grid <- seq(4,23,4)
#- estimate model without constraints
mod5a <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, standardized=TRUE)
summary(mod5a)
# extract parameter names
mod5a$model_parameters
#- invariance constraints on residual variances
par_invariant <- c("F=~v2","v2~~v2")
mod5b <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, standardized=TRUE, par_invariant=par_invariant)
summary(mod5b)
#- bootstrap for statistical inference
bmod5b <- sirt::lsem.bootstrap(mod5b, R=100)
# inspect parameter values and standard errors
bmod5b$parameters
#- user-defined replication design
R <- 100 # bootstrap samples
N <- nrow(dat)
repl_design <- matrix(0, nrow=N, ncol=R)
for (rr in 1:R){
indices <- sort( sample(1:N, replace=TRUE) )
repl_design[,rr] <- sapply(1:N, FUN=function(ii){ sum(indices==ii) } )
}
head(repl_design)
bmod5b1 <- sirt::lsem.bootstrap(mod5a, repl_design=repl_design, repl_factor=1/R)
#- compare model mod5b with joint estimation without constraints
mod5c <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, standardized=TRUE, est_joint=TRUE)
summary(mod5c)
#- linear and quadratic functions
par_invariant <- c("F=~v1","v2~~v2")
par_linear <- c("v1~~v1")
par_quadratic <- c("v4~~v4")
mod5d <- sirt::lsem.estimate( dat1, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, par_invariant=par_invariant, par_linear=par_linear,
par_quadratic=par_quadratic)
summary(mod5d)
#- user-defined constraints: step functions for parameters
# inspect parameter table (from lavaan) of fitted model
pj <- mod5d$partable_joint
#* modify parameter table for user-defined constraints
# define step function for F=~v1 which is constant on intervals 1:4 and 5:7
pj2 <- pj[ pj$con==1, ]
pj2[ c(5,6), "lhs" ] <- "p1g5"
pj2 <- pj2[ -4, ]
partable_joint <- rbind(pj1, pj2)
# estimate model with constraints
mod5e <- lsem::lsem.estimate( dat1, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel, h=2, std.lv=TRUE, estimator="ML",
partable_joint=partable_joint)
summary(mod5e)
#############################################################################
# EXAMPLE 2: data.lsem01 | FIML with missing data
#############################################################################
data(data.lsem01)
dat <- data.lsem01
# induce artifical missing values
set.seed(98)
dat[ runif(nrow(dat)) < .5, c("v1")] <- NA
dat[ runif(nrow(dat)) < .25, c("v2")] <- NA
# specify lavaan model
lavmodel1 <- "
F=~ v1+v2+v3+v4+v5
F ~~ 1*F"
# define grid of moderator variable age
moderator.grid <- seq(4,23,2)
#*** estimate LSEM with FIML
mod1 <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel1, h=2, std.lv=TRUE, estimator="ML", missing="fiml")
summary(mod1)
#############################################################################
# EXAMPLE 3: data.lsem01 | WLSMV estimation
#############################################################################
data(data.lsem01)
dat <- data.lsem01
# create artificial dichotomous data
for (vv in 2:6){
dat[,vv] <- 1*(dat[,vv] > mean(dat[,vv]))
}
# specify lavaan model
lavmodel1 <- "
F=~ v1+v2+v3+v4+v5
F ~~ 1*F
v1 | t1
v2 | t1
v3 | t1
v4 | t1
v5 | t1
"
# define grid of moderator variable age
moderator.grid <- seq(4,23,2)
#*** local WLSMV estimation
mod1 <- sirt::lsem.estimate( dat, moderator="age", moderator.grid=moderator.grid,
lavmodel=lavmodel1, h=2, std.lv=TRUE, estimator="DWLS", ordered=paste0("v",1:5),
residualize=FALSE, pseudo_weights=10000, parameterization="THETA" )
summary(mod1)
}
Run the code above in your browser using DataLab