# \donttest{
# Simulation of a layered SCM
set.seed(1)
pk <- c(3, 5, 4)
simul <- SimulateStructural(n = 100, pk = pk)
print(simul)
summary(simul)
plot(simul)
# Choosing the proportions of explained variances for endogenous variables
set.seed(1)
simul <- SimulateStructural(
n = 1000,
pk = c(2, 3),
nu_between = 1,
ev = c(NA, NA, 0.5, 0.7, 0.9),
output_matrices = TRUE
)
# Checking expected proportions of explained variances
1 - simul$Smat["x3", "x3"] / simul$sigma["x3", "x3"]
1 - simul$Smat["x4", "x4"] / simul$sigma["x4", "x4"]
1 - simul$Smat["x5", "x5"] / simul$sigma["x5", "x5"]
# Checking observed proportions of explained variances (R-squared)
summary(lm(simul$data[, 3] ~ simul$data[, which(simul$theta[, 3] != 0)]))
summary(lm(simul$data[, 4] ~ simul$data[, which(simul$theta[, 4] != 0)]))
summary(lm(simul$data[, 5] ~ simul$data[, which(simul$theta[, 5] != 0)]))
# Simulation including latent and manifest variables
set.seed(1)
simul <- SimulateStructural(
n = 100,
pk = c(2, 3),
n_manifest = c(2, 3, 2, 1, 2)
)
plot(simul)
# Showing manifest variables in red
if (requireNamespace("igraph", quietly = TRUE)) {
mygraph <- plot(simul)
ids <- which(igraph::V(mygraph)$name %in% colnames(simul$data))
igraph::V(mygraph)$color[ids] <- "red"
igraph::V(mygraph)$frame.color[ids] <- "red"
plot(mygraph)
}
# Choosing proportions of explained variances for latent and manifest variables
set.seed(1)
simul <- SimulateStructural(
n = 100,
pk = c(3, 2),
n_manifest = c(2, 3, 2, 1, 2),
ev = c(NA, NA, NA, 0.7, 0.9),
ev_manifest = 0.8,
output_matrices = TRUE
)
plot(simul)
# Checking expected proportions of explained variances
(simul$sigma_full["f4", "f4"] - simul$Smat["f4", "f4"]) / simul$sigma_full["f4", "f4"]
(simul$sigma_full["f5", "f5"] - simul$Smat["f5", "f5"]) / simul$sigma_full["f5", "f5"]
(simul$sigma_full["x1", "x1"] - simul$Smat["x1", "x1"]) / simul$sigma_full["x1", "x1"]
# }
Run the code above in your browser using DataLab