IVs are CEN
# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV)
IV <- gsub("O WITH C EX A ES;", "", IV)
IV <- gsub("C WITH EX ES;", "", IV)
IV <- gsub("EX WITH A ES;", "EX WITH ES;", IV)
IV <- gsub("A WITH ES;", "", IV)
IV <- mplus2lavaan.modelSyntax(IV)
cat(IV)
## X ~ C + EX + ES
## Y ~ X
## Y ~~ X
## C ~~ EX + A + ES
## EX ~~ ES
# 1.2 model fit
IV.O <- sem(IV,sample.cov = P,sample.nobs = Nhar) #xxx
lavaan_summary(IV.O)
##
## Fit Measures (lavaan):
## χ²(6, N = 517) = 37.428, p = 1e-06 ***
## χ²/df = 6.238
## AIC = 8640.167 (Akaike Information Criterion)
## BIC = 8703.887 (Bayesian Information Criterion)
## CFI = 0.850 (Comparative Fit Index)
## TLI = 0.625 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.833 (Normed Fit Index)
## IFI = 0.856 (Incremental Fit Index)
## GFI = 0.977 (Goodness-of-Fit Index)
## AGFI = 0.920 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.101, 90% CI [0.071, 0.133] (Root Mean Square Error of Approximation)
## SRMR = 0.069 (Standardized Root Mean Square Residual)
##
## Model Estimates (lavaan):
## ─────────────────────────────────────────────────────────────────────
## Estimate S.E. z p LLCI ULCI Beta
## ─────────────────────────────────────────────────────────────────────
## Regression Paths:
## X <- C 0.173 (0.039) 4.448 <.001 *** 0.097 0.250 0.172
## X <- EX 0.133 (0.038) 3.488 <.001 *** 0.058 0.208 0.133
## X <- ES 0.120 (0.038) 3.135 .002 ** 0.045 0.195 0.120
## Y <- X 0.759 (0.175) 4.334 <.001 *** 0.416 1.102 0.758
## ─────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "C", "EX")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.O,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
## Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1 47.731 0.000
## ─────────────────────────────
# 2.3. R2
R2xzw.MASEM(P = P,method = 'UIMASEM',y.nm='Y',X.nm='X',Z.nm=variables)
## $R2x.z
## [,1]
## [1,] 0.09242
# 3. X->Y
lavaanPlot(model = IV.O, coefs = T, sig = 0.05)
#RESULTS SUMMARY
Summary.fit <- rbindlist(list(IV.Af[[2]], IV.Lf[[2]], IV.Of[[2]], IV.Mf[[2]]), use.names = TRUE)%>%print_table()
## ──────────────────────────────────────────────────────────────────────────────────────
## Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue CFI SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────
## 1 Model 1 7.538 4.000 0.110 0.986 0.020
## 2 Model 2 11.237 6.000 0.081 0.979 0.025 3.699 2.000 0.157
## 3 Model 1 7.868 4.000 0.097 0.985 0.020
## 4 Model 2 12.049 6.000 0.061 0.977 0.025 4.181 2.000 0.124
## 5 Model 1 8.017 4.000 0.091 0.985 0.020
## 6 Model 2 12.111 6.000 0.060 0.977 0.025 4.094 2.000 0.129
## 7 Model 1 7.986 4.000 0.092 0.985 0.020
## 8 Model 2 12.064 6.000 0.061 0.977 0.025 4.078 2.000 0.130
## ──────────────────────────────────────────────────────────────────────────────────────
#Fit <- SEM.FitCombine(model_list, Mplus = F)#,title = "")
SEM.FitCombine <- function(model_list, Mplus = TRUE, ShortTable = T, title = "Model fit results") {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(model_list, function(model) {
table_fit(model) %>% setDT()
})
# 第 2 步:合并所有模型的拟合结果
In.fit <- rbindlist(fit_tables, use.names = TRUE)
print_table(In.fit)
return(In.fit)
}
# 第 3 和第 4 步:根据 Mplus 参数,执行不同的差异计算和选择列
if (Mplus) {
# 使用 Mplus 特定的列名和计算方法
In.fit <- In.fit %>%
mutate(
ChiSq_Diff = ChiSqM_Value - first(ChiSqM_Value),
DF_Diff = ChiSqM_DF - first(ChiSqM_DF),
ChiSq_p = pchisq(ChiSq_Diff, df = DF_Diff, lower.tail = FALSE),
Model = paste0("Model ", 1:nrow(In.fit))
)
merged_fit <- In.fit#[, .(Model,Parameters, ChiSqM_Value, ChiSqM_DF, ChiSqM_PValue, LL, UnrestrictedLL, CFI, TLI,AIC, BIC, RMSEA_Estimate, SRMR, Filename, ChiSq_Diff, DF_Diff, ChiSq_p)]
} else {
In.fit <- In.fit %>%
mutate(
ChiSq_Diff = chisq - first(chisq),
DF_Diff = df - first(df),
ChiSq_p = pchisq(ChiSq_Diff, df = DF_Diff, lower.tail = FALSE),
Model = paste0("Model ", 1:nrow(In.fit))
)
merged_fit <- In.fit %>%#.[, .(Model,chisq, df, pvalue, LL, unrestricted.logl, cfi, tli, aic, bic, rmsea, srmr,ChiSq_Diff, DF_Diff, ChiSq_p)]%>%
setnames(old = c("chisq", "df", "pvalue", "LL", "unrestricted.logl", "cfi", "tli","aic", "bic", "rmsea", "srmr", "ChiSq_Diff", "DF_Diff", "ChiSq_p"),
new = c("ChiSqM_Value", "ChiSqM_DF", "ChiSqM_PValue", "LL", "UnrestrictedLL",
"CFI", "TLI", "AIC", "BIC", "RMSEA_Estimate", "SRMR", "ChiSq_Diff",
"DF_Diff", "ChiSq_p"))
}
# 第 5 步:打印表格
if (ShortTable) {
merged_fit <- merged_fit[, .(Model, ChiSqM_Value, ChiSqM_DF, ChiSqM_PValue, CFI, SRMR, ChiSq_Diff, DF_Diff, ChiSq_p)]
} else {
merged_fit <- merged_fit[, .(Model, ChiSqM_Value, ChiSqM_DF, ChiSqM_PValue, LL, UnrestrictedLL,
CFI, TLI, AIC, BIC, RMSEA_Estimate, SRMR, ChiSq_Diff, DF_Diff, ChiSq_p)]
}
merged_fit=merged_fit[1, `:=`(ChiSq_Diff = NA, DF_Diff = NA, ChiSq_p = NA)]
if (exists("print_table") && nzchar(title)) {
print_table(merged_fit, title = title)
}
# 返回最终的合并结果
return(list(In.fit,merged_fit))
}
## Error: <text>:59:1: 意外的'}'
## 58: return(list(In.fit,merged_fit))
## 59: }
## ^
SEM.FitCombine <- function(model_list, title = "SEM results") {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(seq_along(model_list), function(i) {
model <- model_list[[i]]
fit_table <- table_results(model) %>% setDT()
# 为每个模型结果添加前缀,避免列名冲突
setnames(fit_table, old = names(fit_table)[-1], new = paste0("M", i, ".", names(fit_table)[-1]))
return(fit_table)
})
# 第 2 步:在 `label` 列上合并所有模型的拟合结果
In.fit <- Reduce(function(x, y) merge(x, y, by = "label", all = TRUE), fit_tables) %>% setDT()
# 将所有列转换为 factor
In.fit[, (names(In.fit)) := lapply(.SD, as.string)]
# 打印合并结果,确保数据格式正确
print_table(In.fit, title = title)
return(In.fit)
}
# 调用 SEM.FitCombine 函数
model_list <- list(IV.A, IV.L, IV.O, IV.M)
combined_results <- SEM.FitCombine(model_list, title = "Combined SEM Fit Results")
## Error in eval(jsub, SDenv, parent.frame()): 找不到对象'as.string'
SEM.FitCombine <- function(model_list, title = "SEM results", ShortTable = FALSE) {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(seq_along(model_list), function(i) {
model <- model_list[[i]]
fit_table <- table_results(model, digits = 3, format_numeric = FALSE) %>% setDT()
# 为每个模型结果添加前缀,避免列名冲突
setnames(fit_table, old = names(fit_table)[4], new = "p")
setnames(fit_table, old = names(fit_table)[-1], new = paste0("M", i, ".", names(fit_table)[-1]))
return(fit_table)
})
# 第 2 步:在 `label` 列上合并所有模型的拟合结果
In.fit <- Reduce(function(x, y) merge(x, y, by = "label", all = TRUE), fit_tables) %>% setDT()
# 如果 ShortTable 为 TRUE,删除包含 'confint' 或 '.p' 的列
if (ShortTable) {
columns_to_remove <- grep("confint|\\.p", names(In.fit), value = TRUE)
In.fit[, (columns_to_remove) := NULL]
}
# 打印合并结果(如果 print_table 函数存在且 title 不为空)
if (exists("print_table") && nzchar(title)) {
print_table(In.fit, title = title)
}
return(In.fit)
}
# 调用 SEM.FitCombine 函数
model_list <- list(IV.A, IV.L, IV.O, IV.M)
combined_results <- SEM.FitCombine(model_list, title = "Combined SEM Fit Results", ShortTable = TRUE)
## Combined SEM Fit Results
## ────────────────────────────────────────────────────────────────────────────────────
## label M1.est_sig M1.se M2.est_sig M2.se M3.est_sig M3.se M4.est_sig M4.se
## ────────────────────────────────────────────────────────────────────────────────────
## 1 C.WITH.A NA 0.243*** 0.044 0.243*** 0.044 0.243*** 0.044
## 2 C.WITH.ES 0.230*** 0.047 0.191*** 0.043 0.191*** 0.043 0.191*** 0.043
## 3 C.WITH.EX 0.190*** 0.047 0.141*** 0.042 0.141*** 0.043 0.141*** 0.043
## 4 EX.WITH.ES 0.259*** 0.047 0.259*** 0.045 0.259*** 0.045 0.259*** 0.045
## 5 Variances.A NA 0.998*** 0.062 0.998*** 0.062 0.998*** 0.062
## 6 Variances.C 0.998*** 0.065 0.976*** 0.060 0.976*** 0.061 0.976*** 0.061
## 7 Variances.ES 0.998*** 0.065 0.998*** 0.062 0.998*** 0.062 0.998*** 0.062
## 8 Variances.EX 0.998*** 0.065 0.998*** 0.062 0.998*** 0.062 0.998*** 0.062
## 9 Variances.X 0.909*** 0.059 0.910*** 0.056 0.910*** 0.057 0.910*** 0.057
## 10 Variances.Y 1.298*** 0.219 1.362*** 0.235 1.330*** 0.225 1.330*** 0.225
## 11 X.ON.C 0.173*** 0.041 0.174*** 0.039 0.173*** 0.039 0.173*** 0.039
## 12 X.ON.ES 0.120** 0.040 0.119** 0.038 0.120** 0.038 0.120** 0.038
## 13 X.ON.EX 0.133*** 0.040 0.133*** 0.038 0.133*** 0.038 0.133*** 0.038
## 14 X.WITH.Y -0.576*** 0.169 -0.618*** 0.170 -0.597*** 0.168 -0.597*** 0.169
## 15 Y.ON.X 0.757*** 0.176 0.760*** 0.177 0.759*** 0.175 0.759*** 0.175
## ────────────────────────────────────────────────────────────────────────────────────
SEM.FitCombine <- function(model_list, title = "SEM results") {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(seq_along(model_list), function(i) {
model <- model_list[[i]]
fit_table <- table_results(model,digits = 3, format_numeric = F) %>% setDT()
# 为每个模型结果添加前缀,避免列名冲突
setnames(fit_table, old =names(fit_table)[4], new = "p")
setnames(fit_table, old = names(fit_table)[-1], new = paste0("M", i, ".", names(fit_table)[-1]))
return(fit_table)
})
# 第 2 步:在 `label` 列上合并所有模型的拟合结果
In.fit <- Reduce(function(x, y) merge(x, y, by = "label", all = TRUE), fit_tables) %>% setDT()
if (exists("print_table") && nzchar(title)) {
print_table(In.fit, title = title)
}
return(In.fit)
}
# 调用 SEM.FitCombine 函数
model_list <- list(IV.A, IV.L, IV.O, IV.M)
combined_results <- SEM.FitCombine(model_list, title = "Combined SEM Fit Results")
## Combined SEM Fit Results
## ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## label M1.est_sig M1.se M1.p M1.confint M2.est_sig M2.se M2.p M2.confint M3.est_sig M3.se M3.p M3.confint M4.est_sig M4.se M4.p M4.confint
## ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## 1 C.WITH.A NA NA 0.243*** 0.044 0.000 [0.158, 0.329] 0.243*** 0.044 0.000 [0.158, 0.329] 0.243*** 0.044 0.000 [0.158, 0.329]
## 2 C.WITH.ES 0.230*** 0.047 0.000 [0.137, 0.322] 0.191*** 0.043 0.000 [0.107, 0.275] 0.191*** 0.043 0.000 [0.107, 0.275] 0.191*** 0.043 0.000 [0.106, 0.275]
## 3 C.WITH.EX 0.190*** 0.047 0.000 [0.098, 0.281] 0.141*** 0.042 0.001 [0.058, 0.224] 0.141*** 0.043 0.001 [0.058, 0.224] 0.141*** 0.043 0.001 [0.057, 0.224]
## 4 EX.WITH.ES 0.259*** 0.047 0.000 [0.167, 0.352] 0.259*** 0.045 0.000 [0.171, 0.348] 0.259*** 0.045 0.000 [0.171, 0.348] 0.259*** 0.045 0.000 [0.170, 0.349]
## 5 Variances.A NA NA 0.998*** 0.062 0.000 [0.877, 1.120] 0.998*** 0.062 0.000 [0.876, 1.120] 0.998*** 0.062 0.000 [0.876, 1.120]
## 6 Variances.C 0.998*** 0.065 0.000 [0.871, 1.125] 0.976*** 0.060 0.000 [0.857, 1.094] 0.976*** 0.061 0.000 [0.857, 1.094] 0.976*** 0.061 0.000 [0.857, 1.094]
## 7 Variances.ES 0.998*** 0.065 0.000 [0.871, 1.125] 0.998*** 0.062 0.000 [0.877, 1.120] 0.998*** 0.062 0.000 [0.876, 1.120] 0.998*** 0.062 0.000 [0.876, 1.120]
## 8 Variances.EX 0.998*** 0.065 0.000 [0.871, 1.125] 0.998*** 0.062 0.000 [0.877, 1.120] 0.998*** 0.062 0.000 [0.876, 1.120] 0.998*** 0.062 0.000 [0.876, 1.120]
## 9 Variances.X 0.909*** 0.059 0.000 [0.794, 1.025] 0.910*** 0.056 0.000 [0.799, 1.020] 0.910*** 0.057 0.000 [0.799, 1.020] 0.910*** 0.057 0.000 [0.798, 1.021]
## 10 Variances.Y 1.298*** 0.219 0.000 [0.869, 1.728] 1.362*** 0.235 0.000 [0.902, 1.821] 1.330*** 0.225 0.000 [0.889, 1.771] 1.330*** 0.225 0.000 [0.888, 1.772]
## 11 X.ON.C 0.173*** 0.041 0.000 [0.093, 0.253] 0.174*** 0.039 0.000 [0.098, 0.250] 0.173*** 0.039 0.000 [0.097, 0.250] 0.173*** 0.039 0.000 [0.097, 0.250]
## 12 X.ON.ES 0.120** 0.040 0.003 [0.042, 0.199] 0.119** 0.038 0.002 [0.045, 0.193] 0.120** 0.038 0.002 [0.045, 0.195] 0.120** 0.038 0.002 [0.045, 0.195]
## 13 X.ON.EX 0.133*** 0.040 0.001 [0.054, 0.212] 0.133*** 0.038 0.000 [0.059, 0.207] 0.133*** 0.038 0.000 [0.058, 0.208] 0.133*** 0.038 0.000 [0.058, 0.208]
## 14 X.WITH.Y -0.576*** 0.169 0.001 [-0.908, -0.244] -0.618*** 0.170 0.000 [-0.952, -0.285] -0.597*** 0.168 0.000 [-0.928, -0.267] -0.597*** 0.169 0.000 [-0.928, -0.267]
## 15 Y.ON.X 0.757*** 0.176 0.000 [0.413, 1.102] 0.760*** 0.177 0.000 [0.413, 1.106] 0.759*** 0.175 0.000 [0.416, 1.102] 0.759*** 0.175 0.000 [0.415, 1.102]
## ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# 定义 SEM.FitCombine 函数
print_table
## function (x, digits = 3, nspaces = 1, row.names = TRUE, col.names = TRUE,
## title = "", note = "", append = "", line = TRUE, file = NULL,
## file.align.head = "auto", file.align.text = "auto")
## {
## if (!inherits(x, c("matrix", "data.frame", "data.table"))) {
## coef.table = coef(summary(x))
## if (!is.null(coef.table))
## x = coef.table
## }
## x = as.data.frame(x)
## sig = NULL
## if (length(digits) == 1)
## digits = rep(digits, length(x))
## for (j in 1:length(x)) {
## if (inherits(x[, j], "factor"))
## x[, j] = as.character(x[, j])
## if (grepl("Pr\\(|pval|p.value|<i>p</i>", names(x)[j])) {
## sig = formatF(sig.trans(x[, j]), 0)
## if (grepl("<i>p</i>", names(x)[j]) == FALSE)
## names(x)[j] = "p"
## x[, j] = p.trans(x[, j])
## }
## else {
## x[, j] = formatF(x[, j], digits[j])
## }
## if (grepl("^S\\.E\\.$|^Std\\. Error$|^se$|^SE$|^BootSE$",
## names(x)[j])) {
## x[, j] = paste0("(", x[, j], ")")
## x[grepl("\\d", x[, j]) == FALSE, j] = ""
## if (grepl("S\\.E\\.", names(x)[j]) == FALSE)
## names(x)[j] = "S.E."
## }
## if (grepl("^S\\.D\\.$|^Std\\. Deviation$", names(x)[j])) {
## x[, j] = paste0("(", x[, j], ")")
## x[grepl("\\d", x[, j]) == FALSE, j] = ""
## if (grepl("S\\.D\\.", names(x)[j]) == FALSE)
## names(x)[j] = "S.D."
## }
## names(x)[j] = gsub(" value$|val$", "", names(x)[j])
## }
## if (is.null(sig) == FALSE & "sig" %notin% names(x)) {
## p.pos = which(names(x) %in% c("p", "<i>p</i>"))
## nvars = length(names(x))
## if (p.pos < nvars)
## x = cbind(x[1:p.pos], ` ` = sig, x[(p.pos + 1):nvars])
## else x = cbind(x, ` ` = sig)
## x$` ` = as.character(x$` `)
## }
## if (inherits(row.names, "character")) {
## row.names(x) = row.names
## row.names = TRUE
## }
## if (inherits(col.names, "character")) {
## names(x) = col.names
## col.names = TRUE
## }
## linechar = ifelse(line, "─", "-")
## title.length = nchar(names(x), type = "width")
## vars.length = c()
## for (j in 1:length(x)) vars.length[j] = max(nchar(x[, j],
## type = "width"))
## n.lines = apply(rbind(title.length, vars.length), 2, max) +
## nspaces
## n.lines.rn = max(nchar(row.names(x), type = "width")) + nspaces
## if (row.names)
## table.line = rep_char(linechar, sum(n.lines) + n.lines.rn)
## else table.line = rep_char(linechar, sum(n.lines))
## if (is.null(file)) {
## if (title != "")
## Print(title)
## Print(table.line)
## if (row.names)
## cat(rep_char(" ", n.lines.rn))
## for (j in 1:length(x)) {
## name.j = names(x)[j]
## cat(rep_char(" ", n.lines[j] - nchar(name.j, type = "width")) %^%
## name.j)
## }
## cat("\n")
## Print(table.line)
## for (i in 1:nrow(x)) {
## if (row.names) {
## row.name.i = row.names(x)[i]
## cat(row.name.i %^% rep_char(" ", n.lines.rn -
## nchar(row.name.i, type = "width")))
## }
## for (j in 1:length(x)) {
## x.ij = ifelse(is.na(x[i, j]) | grepl("NA$", x[i,
## j]), "", x[i, j])
## cat(rep_char(" ", n.lines[j] - nchar(x.ij, type = "width")) %^%
## x.ij)
## }
## cat("\n")
## }
## Print(table.line)
## if (note != "")
## Print(note)
## }
## if (row.names) {
## x = cbind(rn = row.names(x), x)
## names(x)[1] = ""
## }
## if (!is.null(file)) {
## html = df_to_html(x, title = title, note = note, append = append,
## file = file, align.head = file.align.head, align.text = file.align.text)
## }
## else {
## html = NULL
## }
## invisible(list(df = x, html = html))
## }
## <bytecode: 0x0000019254a32b58>
## <environment: namespace:bruceR>
SEM.FitCombine <- function(model_list, title = "SEM results") {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(seq_along(model_list), function(i) {
model <- model_list[[i]]
fit_table <- table_results(model) %>% setDT()
# 为每个模型结果添加前缀,避免列名冲突
setnames(fit_table, old = names(fit_table)[-1], new = paste0("M", i, ".", names(fit_table)[-1]))
return(fit_table)
})
# 第 2 步:在 `label` 列上合并所有模型的拟合结果
In.fit <- Reduce(function(x, y) merge(x, y, by = "label", all = TRUE), fit_tables) %>% setDT()
# 将所有非 'label' 列的字符转换为数值型
num_cols <- setdiff(names(In.fit), "label") # 获取所有除 'label' 外的列名
In.fit[, (num_cols) := lapply(.SD, as.numeric), .SDcols = num_cols] # 转换为数值型
# 打印合并结果
print_table(In.fit, title = title)
return(In.fit)
}
# 调用 SEM.FitCombine 函数
model_list <- list(IV.A, IV.L, IV.O, IV.M)
combined_results <- SEM.FitCombine(model_list, title = "Combined SEM Fit Results")
## Error in if (p.pos < nvars) x = cbind(x[1:p.pos], ` ` = sig, x[(p.pos + : the condition has length > 1
# 定义 SEM.FitCombine 函数
SEM.FitCombine <- function(model_list, title = "SEM results") {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(seq_along(model_list), function(i) {
model <- model_list[[i]]
fit_table <- table_results(model) %>% setDT()
# 为每个模型结果添加前缀,避免列名冲突
setnames(fit_table, old = names(fit_table)[-1], new = paste0("M", i, ".", names(fit_table)[-1]))
return(fit_table)
})
# 第 2 步:在 `label` 列上合并所有模型的拟合结果
In.fit <- Reduce(function(x, y) merge(x, y, by = "label", all = TRUE), fit_tables)%>%setDT()
# 打印合并结果
print_table(In.fit)
return(In.fit)
}
# 调用 SEM.FitCombine 函数
model_list <- list(IV.A, IV.L, IV.O, IV.M)
combined_results <- SEM.FitCombine(model_list, title = "Combined SEM Fit Results")
## Error in if (p.pos < nvars) x = cbind(x[1:p.pos], ` ` = sig, x[(p.pos + : the condition has length > 1
SEM.PathCombine <- function(model_list, title = "SEM results") {
# 第 1 步:计算每个模型的拟合值并转换为数据表格式
fit_tables <- lapply(model_list, function(model) {
table_results(model) %>% setDT()
})
# 第 2 步:在 `label` 列上合并所有模型的拟合结果
In.fit <- Reduce(function(x, y) merge(x, y, by = "label", all = TRUE), fit_tables)
# 打印合并结果
print_table(In.fit, title = title)
return(In.fit)
}
model_list=list(IV.A, IV.L, IV.O, IV.M)
combined_results <- SEM.PathCombine(list(IV.A, IV.L, IV.O, IV.M), title = "Combined SEM Fit Results")
## Error in if (p.pos < nvars) x = cbind(x[1:p.pos], ` ` = sig, x[(p.pos + : the condition has length > 1
table_results(IV.O)%>%setDT()#%>%print_table()