## Not run:
# data(Becker09)
#
# #### Fixed-effects model
# ## First stage analysis
# fixed1 <- tssem1(Becker09$data, Becker09$n, method="FEM")
# summary(fixed1)
#
# ## Prepare a regression model using create.mxMatrix()
# A1 <- create.mxMatrix(c(0, "0.1*Cog2Per", "0.1*SO2Per", "0.1*SC2Per",
# 0, 0, 0, 0,
# 0, 0, 0, 0,
# 0, "0.1*Cog2SC", "0.1*SO2SC",0),
# type="Full", byrow=TRUE, ncol=4, nrow=4,
# as.mxMatrix=FALSE)
#
# ## This step is not necessary but it is useful for inspecting the model.
# dimnames(A1)[[1]] <- dimnames(A1)[[2]] <- c("Per","Cog","SO","SC")
#
# ## Display A1
# A1
#
# S1 <- create.mxMatrix(c("0.1*var_Per",
# 0, 1,
# 0, "0.1*cor", 1,
# 0, 0, 0, "0.1*var_SC"), byrow=TRUE, type="Symm",
# as.mxMatrix=FALSE)
#
# ## This step is not necessary but it is useful for inspecting the model.
# dimnames(S1)[[1]] <- dimnames(S1)[[2]] <- c("Per","Cog","SO","SC")
#
# ## Display S1
# S1
#
# ################################################################################
# ## Alternative model specification in lavaan model syntax
# model <- "## Regression paths
# Per~Cog2Per*Cog
# Per~SO2Per*SO
# Per~SC2Per*SC
# SC~Cog2SC*Cog
# SC~SO2SC*SO
# ## Fix the variances of Cog and SO at 1
# Cog~~1*Cog
# SO~~1*SO
# ## Label the correlation between Cog and SO
# Cog~~cor*SO
# ## Label the error variances of Per and SC
# Per~~var_Per*Per
# SC~~var_SC*SC"
#
# RAM <- lavaan2RAM(model, obs.variables=c("Per","Cog","SO","SC"))
# RAM
#
# A1 <- RAM$A
# S1 <- RAM$S
# ################################################################################
#
# ## Second stage analysis
# fixed2 <- tssem2(fixed1, Amatrix=A1, Smatrix=S1, diag.constraints=TRUE,
# intervals.type="LB", model.name="TSSEM2 Becker09",
# mx.algebras=list( Cog=mxAlgebra(Cog2SC*SC2Per, name="Cog"),
# SO=mxAlgebra(SO2SC*SC2Per, name="SO"),
# Cog_SO=mxAlgebra(Cog2SC*SC2Per+SO2SC*SC2Per,
# name="Cog_SO")) )
# summary(fixed2)
#
# #### Fixed-effects model: with type of sport as cluster
# ## First stage analysis
# cluster1 <- tssem1(Becker09$data, Becker09$n, method="FEM",
# cluster=Becker09$Type_of_sport)
# summary(cluster1)
#
# ## Second stage analysis
# cluster2 <- tssem2(cluster1, Amatrix=A1, Smatrix=S1, diag.constraints=TRUE,
# intervals.type="LB", model.name="TSSEM2 Becker09",
# mx.algebras=list( Cog=mxAlgebra(Cog2SC*SC2Per, name="Cog"),
# SO=mxAlgebra(SO2SC*SC2Per, name="SO"),
# Cog_SO=mxAlgebra(Cog2SC*SC2Per+SO2SC*SC2Per,
# name="Cog_SO")) )
# summary(cluster2)
#
# ## Convert the model to semPlotModel object with 2 plots
# ## Use the short forms of the variable names
# my.plots <- lapply(X=cluster2, FUN=meta2semPlot, manNames=c("Per","Cog","SO","SC") )
#
# ## Load the library
# library("semPlot")
#
# ## Setup two plots
# layout(t(1:2))
# ## The labels are overlapped. We may modify it by using layout="spring"
# semPaths(my.plots[[1]], whatLabels="est", nCharNodes=10, color="orange",
# layout="spring", edge.label.cex=0.8)
# title("Individual sport")
# semPaths(my.plots[[2]], whatLabels="est", nCharNodes=10, color="skyblue",
# layout="spring", edge.label.cex=0.8)
# title("Team sport")
#
#
# #### Random-effects model
# ## First stage analysis
# random1 <- tssem1(Becker09$data, Becker09$n, method="REM", RE.type="Diag")
# summary(random1)
#
# ## Second stage analysis
# random2 <- tssem2(random1, Amatrix=A1, Smatrix=S1, diag.constraints=TRUE,
# intervals.type="LB", model.name="TSSEM2 Becker09",
# mx.algebras=list( Cog=mxAlgebra(Cog2SC*SC2Per, name="Cog"),
# SO=mxAlgebra(SO2SC*SC2Per, name="SO"),
# Cog_SO=mxAlgebra(Cog2SC*SC2Per+SO2SC*SC2Per,
# name="Cog_SO")) )
# summary(random2)
#
# ## Load the library
# library("semPlot")
#
# ## Convert the model to semPlotModel object
# my.plot <- meta2semPlot(random2, manNames=c("Per","Cog","SO","SC"))
#
# ## Plot the model with labels
# ## The labels are overlapped.
# ## semPaths(my.plot, whatLabels="path", nCharEdges=10)
# semPaths(my.plot, whatLabels="path", nCharEdges=10, nCharNodes=10, layout="spring", color="red")
#
# ## Plot the parameter estimates
# semPaths(my.plot, whatLabels="est", nCharNodes=10, layout="spring", color="green")
# ## End(Not run)
Run the code above in your browser using DataLab