1 FUNCTIONS

2 DATA PREPARATION

2.1 Load all sheets of an Excel

Sheets <- rio::import_list("https://drive.google.com/uc?id=1-_SGvqQpl0bPTa2e9vVcr0SJEjFZMEsn&export=download")

#Sheets <- rio::import_list("https://drive.google.com/uc?id=1W9iSMJyKMf9dlay7YyCyRrY5E1GLceAQ&export=download")

#Sheets <- rio::import_list("https://drive.google.com/uc?id=1eWmXuL3uXAHMZesaCuiSQcuMD8djEKNc&export=download")
#Sheets <-rio::import_list("C:\\Users\\S\\Documents\\WXWork\\1688851821252719\\Cache\\File\\2024-09\\Matrix-overall.xlsx")
#as.data.frame(Sheets$Template)

# 使用 lapply 将 Sheets 中的每个数据框转换为 data.table,并替换缺失值
Sheets <- lapply(Sheets, function(df) {
  # 替换所有缺失值为 -999
  df[is.na(df)] <- -999
  # 返回修改后的 df
  return(df)
})

# 对 Sheets 列表中的每个数据表保留前 7 行和前 7 列
Sheets <- lapply(Sheets, function(df) {
  # 只保留前 7 行和前 7 列
  df[1:7, 1:7]
})

2.2 Convert all data in wide-format within one sheet

result_list <- lapply(Sheets, function(sheet) Matrix.toDT(sheet, title = ""))
#result_list <- result_list[!names(result_list) %in% "Sources"]
#result_list$Sources[[1]]
df <- bind_rows(lapply(result_list, function(x) x[[1]]))%>%
  setDT%>%
  .[, MatrixName := names(result_list)]#%>%
#  .[MatrixName != "Sources", ]%>%
#  .[MatrixName != "Template" , ]  #%>%  .[, lapply(.SD, as.numeric), .SDcols = setdiff(names(df), "MatrixName")]

# 将除了 "MatrixName" 之外的所有列转换为数值类型
cols_to_convert <- setdiff(names(df), "MatrixName")
#df=df[, (cols_to_convert) := lapply(.SD, as.numeric), .SDcols = cols_to_convert]

#MatrixName=df$MatrixName  

#df[] <- lapSources#df[] <- lapply(df, as.numeric)
#df=df[, names(df) := lapply(.SD, as.numeric)]

#names(df)
#unique(df$MatrixName)
#print_table(df)
#View(df)
# 查看合并后的数据框
#export(combined_df,"JPJSrow.xlsx")
#data <-read_sheet("https://docs.google.com/spreadsheets/d/1Q8OUSBQ9a1R0jJ14PtEH3w74aTmwyru3u3uIOst3DC4/edit?usp=sharing")
#View(data)

2.3 Convert all data in matrix format

cor_matrices <- Matrix.fromDT(as.data.frame(df),"A-ES", "Y-X", "MatrixName", text_rows = c("Sources", "Template"))
## 正在处理行:  Template 
## 正在处理行:  r 
## 正在处理行:  C-r 
## 正在处理行:  SD-r 
## 正在处理行:  SD-cr 
## 正在处理行:  Sources 
## 正在处理行:  Sample size 
## 正在处理行:  K Size 
## 正在处理行:  95%CI-L 
## 正在处理行:  95%CI-U
P <- cor_matrices$r
Nnum=Matrix.Stats(Sheets$`Sample size`)
## Statistics of matrix
## ────────────────────────────
##    harmonic_mean         sum
## ────────────────────────────
## 1      30130.709 1061439.000
## ────────────────────────────
## Descriptive Statistics:
## ───────────────────────────────────────────────────────────────────────
##     N     Mean       SD |   Median      Min       Max Skewness Kurtosis
## ───────────────────────────────────────────────────────────────────────
##    21 50544.71 32220.21 | 54471.00 11856.00 106149.00     0.20    -1.53
## ───────────────────────────────────────────────────────────────────────
Knum=Matrix.Stats(Sheets$`K Size`)
## Statistics of matrix
## ─────────────────────────
##    harmonic_mean      sum
## ─────────────────────────
## 1         81.468 2236.000
## ─────────────────────────
## Descriptive Statistics:
## ──────────────────────────────────────────────────────────
##     N   Mean    SD | Median   Min    Max Skewness Kurtosis
## ──────────────────────────────────────────────────────────
##    21 106.48 61.10 | 111.00 38.00 312.00     1.62     3.43
## ──────────────────────────────────────────────────────────
Nhar=round(Nnum$sum/Knum$sum,0)#round(Nnum$harmonic_mean,0)

3 TEMPLATE

IV.oceanS0  <- '
  # X 由 ES、A、C、EX 和 O 预测
  X ~  O + C + EX + A + ES

  # Y 由 X 预测
  Y ~ X
  
  # Residual correlation
  Y ~~ X
'

# 1.1 model specification 
IV.S <- '
  X ON O C EX A ES;
  Y ON X;
  Y WITH X;

  O WITH C EX A ES;
  C WITH EX A ES;
  EX WITH A ES;
  A WITH ES;
'
IV <- mplus2lavaan.modelSyntax(IV.S)
IVb <- mplus2lavaan.modelSyntax(IV.S)
cat(IV)
## X ~ O + C + EX + A + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES

3.1 Template: IVs are OCEAN

# 1. Model Specification
IV <- mplus2lavaan.modelSyntax(IV.S)

# 1.2 model fit
IV.F <- sem(IV,sample.cov = P,sample.nobs = Nhar)
lavaan_summary(IV.F)
## 
## Fit Measures (lavaan):
## χ²(4, N = 475) = 7.538, p = 0.110    
## χ²/df = 1.885
## AIC = 9218.823 (Akaike Information Criterion)
## BIC = 9318.742 (Bayesian Information Criterion)
## CFI = 0.986 (Comparative Fit Index)
## TLI = 0.924 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.972 (Normed Fit Index)
## IFI = 0.986 (Incremental Fit Index)
## GFI = 0.996 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.043, 90% CI [0.000, 0.090] (Root Mean Square Error of Approximation)
## SRMR = 0.020 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ────────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.      z     p       LLCI  ULCI   Beta
## ────────────────────────────────────────────────────────────────────────
## Regression Paths:                                                       
##   X <- O             -0.053 (0.040) -1.348  .178     -0.131 0.024 -0.053
##   X <- C              0.164 (0.042)  3.937 <.001 ***  0.083 0.246  0.164
##   X <- EX             0.140 (0.042)  3.343 <.001 ***  0.058 0.222  0.140
##   X <- A              0.060 (0.040)  1.512  .130     -0.018 0.139  0.060
##   X <- ES             0.116 (0.040)  2.884  .004 **   0.037 0.195  0.116
##   Y <- X              0.749 (0.170)  4.416 <.001 ***  0.416 1.081  0.749
## ────────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "A", "C", "EX", "O")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.F,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          49.624       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.09853
# 3. X->Y
lavaanPlot(model = IV.F, coefs = T, sig = 0.05,stars = TRUE)
#lavaanPlot(model = IV.F, stand = F, coefs = TRUE, sig = 0.05, covs = TRUE)

4 MAIN EFFECTS OF OCEAN ON X AND Y

# 1. Model Specification
IV <- gsub("Y ON X;", "Y ON O C EX A ES;", IV.S)
IV <- mplus2lavaan.modelSyntax(IV)
cat(IV)
## X ~ O + C + EX + A + ES
## Y ~ O + C + EX + A + ES
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.cenA <- sem(IV,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.cenA)
## 
## Fit Measures (lavaan):
## χ²(0, N = 475) = 0.000, p = 1.000    
## χ²/df = Inf (saturated model)
## AIC = 9219.285 (Akaike Information Criterion)
## BIC = 9335.857 (Bayesian Information Criterion)
## CFI = 1.000 (Comparative Fit Index)
## TLI = 1.000 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 1.000 (Normed Fit Index)
## IFI = 1.000 (Incremental Fit Index)
## GFI = 1.000 (Goodness-of-Fit Index)
## AGFI = 1.000 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.000, 90% CI [0.000, 0.000] (Root Mean Square Error of Approximation)
## SRMR = 0.000 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ────────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.      z     p       LLCI  ULCI   Beta
## ────────────────────────────────────────────────────────────────────────
## Regression Paths:                                                       
##   X <- O             -0.071 (0.046) -1.533  .125     -0.161 0.020 -0.071
##   X <- C              0.132 (0.047)  2.820  .005 **   0.040 0.224  0.132
##   X <- EX             0.130 (0.048)  2.742  .006 **   0.037 0.223  0.130
##   X <- A              0.052 (0.047)  1.108  .268     -0.040 0.143  0.052
##   X <- ES             0.172 (0.046)  3.742 <.001 ***  0.082 0.263  0.172
##   Y <- O             -0.013 (0.047) -0.283  .777     -0.106 0.079 -0.013
##   Y <- C              0.172 (0.048)  3.609 <.001 ***  0.079 0.266  0.172
##   Y <- EX             0.119 (0.048)  2.459  .014 *    0.024 0.214  0.119
##   Y <- A              0.059 (0.047)  1.235  .217     -0.034 0.152  0.059
##   Y <- ES             0.001 (0.047)  0.020  .984     -0.091 0.093  0.001
## ────────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# Plot
lavaanPlot(model = IV.cenA, coefs = T, sig = 0.05)

5 OVERALL RESULTS

5.1 IVs are OCEAN

# 1. Model Specification
IV.ocean <- IVb
cat(IV.ocean)
## X ~ O + C + EX + A + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.oceanA <- sem(IV.ocean,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.oceanA)
## 
## Fit Measures (lavaan):
## χ²(4, N = 475) = 7.538, p = 0.110    
## χ²/df = 1.885
## AIC = 9218.823 (Akaike Information Criterion)
## BIC = 9318.742 (Bayesian Information Criterion)
## CFI = 0.986 (Comparative Fit Index)
## TLI = 0.924 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.972 (Normed Fit Index)
## IFI = 0.986 (Incremental Fit Index)
## GFI = 0.996 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.043, 90% CI [0.000, 0.090] (Root Mean Square Error of Approximation)
## SRMR = 0.020 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ────────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.      z     p       LLCI  ULCI   Beta
## ────────────────────────────────────────────────────────────────────────
## Regression Paths:                                                       
##   X <- O             -0.053 (0.040) -1.348  .178     -0.131 0.024 -0.053
##   X <- C              0.164 (0.042)  3.937 <.001 ***  0.083 0.246  0.164
##   X <- EX             0.140 (0.042)  3.343 <.001 ***  0.058 0.222  0.140
##   X <- A              0.060 (0.040)  1.512  .130     -0.018 0.139  0.060
##   X <- ES             0.116 (0.040)  2.884  .004 **   0.037 0.195  0.116
##   Y <- X              0.749 (0.170)  4.416 <.001 ***  0.416 1.081  0.749
## ────────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "A", "C", "EX", "O")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.oceanA,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          49.624       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.09853
# 3. X->Y
lavaanPlot(model = IV.oceanA, coefs = T, sig = 0.05)

5.2 Test for excluding the unqualified IVs

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- mplus2lavaan.modelSyntax(IV)
cat(IV)
## X ~ C + EX + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.cenA <- sem(IV,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.cenA)
## 
## Fit Measures (lavaan):
## χ²(6, N = 475) = 11.237, p = 0.081 .  
## χ²/df = 1.873
## AIC = 9218.522 (Akaike Information Criterion)
## BIC = 9310.115 (Bayesian Information Criterion)
## CFI = 0.979 (Comparative Fit Index)
## TLI = 0.925 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.958 (Normed Fit Index)
## IFI = 0.980 (Incremental Fit Index)
## GFI = 0.993 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.043, 90% CI [0.000, 0.081] (Root Mean Square Error of Approximation)
## SRMR = 0.025 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ─────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.     z     p      LLCI  ULCI  Beta
## ─────────────────────────────────────────────────────────────────────
## Regression Paths:                                                    
##   X <- C              0.173 (0.041) 4.238 <.001 *** 0.093 0.253 0.173
##   X <- EX             0.133 (0.040) 3.308 <.001 *** 0.054 0.212 0.133
##   X <- ES             0.120 (0.040) 2.991  .003 **  0.042 0.199 0.120
##   Y <- X              0.757 (0.176) 4.312 <.001 *** 0.413 1.102 0.757
## ─────────────────────────────────────────────────────────────────────
## 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.cenA,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          46.275       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.cenA, coefs = T, sig = 0.05)
IV.Af <- SEM.FitCombine(list(IV.oceanA,IV.cenA), Mplus = F)#xxx
## SEM Model fit results
## ──────────────────────────────────────────────────────────────────────────────────────
##      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
## ──────────────────────────────────────────────────────────────────────────────────────
#result[[2]][, Disgin := "Longitudinal design"]

5.3 IVs are CEN

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- gsub("O WITH C EX A ES;", "", IV)
IV <- gsub("C WITH EX A ES;", "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 + ES
## EX ~~ ES
# 1.2 model fit
IV.A <- sem(IV,sample.cov = P,sample.nobs = Nhar)
lavaan_summary(IV.A)
## 
## Fit Measures (lavaan):
## χ²(2, N = 475) = 6.809, p = 0.033 *  
## χ²/df = 3.405
## AIC = 6616.660 (Akaike Information Criterion)
## BIC = 6670.783 (Bayesian Information Criterion)
## CFI = 0.966 (Comparative Fit Index)
## TLI = 0.830 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.955 (Normed Fit Index)
## IFI = 0.968 (Incremental Fit Index)
## GFI = 0.994 (Goodness-of-Fit Index)
## AGFI = 0.958 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.071, 90% CI [0.017, 0.133] (Root Mean Square Error of Approximation)
## SRMR = 0.024 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ─────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.     z     p      LLCI  ULCI  Beta
## ─────────────────────────────────────────────────────────────────────
## Regression Paths:                                                    
##   X <- C              0.173 (0.041) 4.238 <.001 *** 0.093 0.253 0.173
##   X <- EX             0.133 (0.040) 3.308 <.001 *** 0.054 0.212 0.133
##   X <- ES             0.120 (0.040) 2.991  .003 **  0.042 0.199 0.120
##   Y <- X              0.757 (0.176) 4.312 <.001 *** 0.413 1.102 0.757
## ─────────────────────────────────────────────────────────────────────
## 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.A,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          46.275       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.A, coefs = T, sig = 0.05)

5.4 IVs are EN with Y on C

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- gsub("Y ON X;", "Y ON X C;", IV)
IV <- gsub("O WITH C EX A ES;", "", IV)
IV <- gsub("C WITH EX A ES;", "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 + C
## Y ~~ X
## C ~~ EX + ES
## EX ~~ ES
# 1.2 model fit
IV.Ac <- sem(IV,sample.cov = P,sample.nobs = Nhar)
lavaan_summary(IV.Ac)
## 
## Fit Measures (lavaan):
## χ²(1, N = 475) = 3.598, p = 0.058 .  
## χ²/df = 3.598
## AIC = 6615.449 (Akaike Information Criterion)
## BIC = 6673.735 (Bayesian Information Criterion)
## CFI = 0.982 (Comparative Fit Index)
## TLI = 0.816 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.976 (Normed Fit Index)
## IFI = 0.983 (Incremental Fit Index)
## GFI = 0.997 (Goodness-of-Fit Index)
## AGFI = 0.955 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.074, 90% CI [0.000, 0.163] (Root Mean Square Error of Approximation)
## SRMR = 0.018 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ─────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.     z     p      LLCI  ULCI  Beta
## ─────────────────────────────────────────────────────────────────────
## Regression Paths:                                                    
##   X <- C              0.137 (0.045) 3.022  .003 **  0.048 0.226 0.137
##   X <- EX             0.137 (0.045) 3.074  .002 **  0.050 0.224 0.137
##   X <- ES             0.160 (0.045) 3.547 <.001 *** 0.072 0.249 0.160
##   Y <- X              0.415 (0.202) 2.053  .040 *   0.019 0.811 0.415
##   Y <- C              0.127 (0.061) 2.074  .038 *   0.007 0.247 0.127
## ─────────────────────────────────────────────────────────────────────
## 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.Ac,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          48.077       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.Ac, coefs = T, sig = 0.05)
IV.Acenf <- SEM.FitCombine(list(IV.Ac,IV.A), Mplus = F)#xxx
## SEM Model fit results
## ──────────────────────────────────────────────────────────────────────────────────────
##      Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue   CFI  SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────
## 1  Model 1        3.598     1.000         0.058 0.982 0.018                           
## 2  Model 2        6.809     2.000         0.033 0.966 0.024      3.211   1.000   0.073
## ──────────────────────────────────────────────────────────────────────────────────────

5.5 IVs are EN

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON EX ES;", IV.S)
IV <- gsub("O WITH C EX A ES;", "", IV)
IV <- gsub("C WITH EX A 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 ~ EX + ES
## Y ~ X
## Y ~~ X
## EX ~~ ES
# 1.2 model fit
IV.A2 <- sem(IV,sample.cov = P,sample.nobs = Nhar)
lavaan_summary(IV.A2)
## 
## Fit Measures (lavaan):
## χ²(1, N = 475) = 3.371, p = 0.066 .  
## χ²/df = 3.371
## AIC = 5315.205 (Akaike Information Criterion)
## BIC = 5352.675 (Bayesian Information Criterion)
## CFI = 0.973 (Comparative Fit Index)
## TLI = 0.839 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.964 (Normed Fit Index)
## IFI = 0.975 (Incremental Fit Index)
## GFI = 0.996 (Goodness-of-Fit Index)
## AGFI = 0.965 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.071, 90% CI [0.000, 0.160] (Root Mean Square Error of Approximation)
## SRMR = 0.021 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ─────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.     z     p      LLCI  ULCI  Beta
## ─────────────────────────────────────────────────────────────────────
## Regression Paths:                                                    
##   X <- EX             0.162 (0.044) 3.700 <.001 *** 0.076 0.247 0.162
##   X <- ES             0.181 (0.044) 4.125 <.001 *** 0.095 0.267 0.181
##   Y <- X              0.544 (0.177) 3.079  .002 **  0.198 0.890 0.544
## ─────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "EX")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.A2,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          38.076       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.07506
# 3. X->Y
lavaanPlot(model = IV.A2, coefs = T, sig = 0.05)
IV.A2f <- SEM.FitCombine(list(IV.A2,IV.A2), Mplus = F)#xxx
## SEM Model fit results
## ──────────────────────────────────────────────────────────────────────────────────────
##      Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue   CFI  SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────
## 1  Model 1        3.371     1.000         0.066 0.973 0.021                           
## 2  Model 2        3.371     1.000         0.066 0.973 0.021      0.000   0.000   1.000
## ──────────────────────────────────────────────────────────────────────────────────────
IV.A2f2=IV.A2f[[2]][Model!="Model 2",]

6 RESULTS SUMMARY

Summary.fit <- rbindlist(list(IV.Af[[2]], IV.Acenf[[2]],IV.A2f2), use.names = TRUE)
Summary.fit$Model[1:5] <- c(
  "M1. OCEAN as IVs",
  "M2. CEN as IVs with OA retained",
  "M4. EN as IVs with the effect of C on Y",
  "M3. CEN as IVs with OA removed",
  "M5. EN as IVs with OA removed"
)
print_table(Summary.fit[order(Summary.fit$Model), ])
## ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
##                                      Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue   CFI  SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## 1  M1. OCEAN as IVs                               7.538     4.000         0.110 0.986 0.020                           
## 2  M2. CEN as IVs with OA retained               11.237     6.000         0.081 0.979 0.025      3.699   2.000   0.157
## 3  M3. CEN as IVs with OA removed                 6.809     2.000         0.033 0.966 0.024      3.211   1.000   0.073
## 4  M4. EN as IVs with the effect of C on Y        3.598     1.000         0.058 0.982 0.018                           
## 5  M5. EN as IVs with OA removed                  3.371     1.000         0.066 0.973 0.021                           
## ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#Fit <- SEM.FitCombine(model_list, Mplus = F)#,title = "")
combined_results <- SEM.PathCombine(list(IV.oceanA,IV.cenA,IV.A,IV.Ac,IV.A2), title = "Combined SEM Path Results", ShortTable = T, EffectOnly = F)
## Combined SEM Path Results
## ─────────────────────────────────────────────────────────────────────────────────────────────────────
##            label M1.est_sig M1.se M2.est_sig M2.se M3.est_sig M3.se M4.est_sig M4.se M5.est_sig M5.se
## ─────────────────────────────────────────────────────────────────────────────────────────────────────
## 1   A.WITH.ES     0.160***  0.046  NA               NA                NA               NA            
## 2   C.WITH.A      0.289***  0.048  0.289***  0.048  NA                NA               NA            
## 3   C.WITH.ES     0.230***  0.047  0.230***  0.047  0.230***  0.047   0.230*** 0.047   NA            
## 4   C.WITH.EX     0.190***  0.047  0.190***  0.047  0.190***  0.047   0.190*** 0.047   NA            
## 5   C.WITH.O      NA               0.160***  0.046  NA                NA               NA            
## 6   ES.WITH.A     NA               0.160***  0.046  NA                NA               NA            
## 7   ES.WITH.O     NA               0.070     0.046  NA                NA               NA            
## 8   EX.WITH.A     0.200***  0.047  0.200***  0.047  NA                NA               NA            
## 9   EX.WITH.ES    0.259***  0.047  0.259***  0.047  0.259***  0.047   0.259*** 0.047   0.259*** 0.047
## 10  EX.WITH.O     NA               0.289***  0.048  NA                NA               NA            
## 11  O.WITH.A      0.190***  0.047  0.190***  0.047  NA                NA               NA            
## 12  O.WITH.C      0.160***  0.046  NA               NA                NA               NA            
## 13  O.WITH.ES     0.070     0.046  NA               NA                NA               NA            
## 14  O.WITH.EX     0.289***  0.048  NA               NA                NA               NA            
## 15  Variances.A   0.998***  0.065  0.998***  0.065  NA                NA               NA            
## 16  Variances.C   0.998***  0.065  0.998***  0.065  0.998***  0.065   0.998*** 0.065   NA            
## 17  Variances.ES  0.998***  0.065  0.998***  0.065  0.998***  0.065   0.998*** 0.065   0.998*** 0.065
## 18  Variances.EX  0.998***  0.065  0.998***  0.065  0.998***  0.065   0.998*** 0.065   0.998*** 0.065
## 19  Variances.O   0.998***  0.065  0.998***  0.065  NA                NA               NA            
## 20  Variances.X   0.904***  0.059  0.909***  0.059  0.909***  0.059   0.906*** 0.059   0.924*** 0.060
## 21  Variances.Y   1.288***  0.210  1.298***  0.219  1.298***  0.219   1.005*** 0.124   1.098*** 0.147
## 22  X.ON.A        0.060     0.040  NA               NA                NA               NA            
## 23  X.ON.C        0.164***  0.042  0.173***  0.041  0.173***  0.041   0.137**  0.045   NA            
## 24  X.ON.ES       0.116**   0.040  0.120**   0.040  0.120**   0.040   0.160*** 0.045   0.181*** 0.044
## 25  X.ON.EX       0.140***  0.042  0.133***  0.040  0.133***  0.040   0.137**  0.045   0.162*** 0.044
## 26  X.ON.O        -0.053    0.040  NA               NA                NA               NA            
## 27  X.WITH.Y      -0.567*** 0.163  -0.576*** 0.169  -0.576*** 0.169   -0.260   0.189   -0.363*  0.170
## 28  Y.ON.C        NA               NA               NA                0.127*   0.061   NA            
## 29  Y.ON.X        0.749***  0.170  0.757***  0.176  0.757***  0.176   0.415*   0.202   0.544**  0.177
## ─────────────────────────────────────────────────────────────────────────────────────────────────────
combined_results[!grepl("Variances\\.", label)]%>%
  .[15:nrow(.), ]%>%
  print_table()
## ────────────────────────────────────────────────────────────────────────────────────────────────
##       label M1.est_sig M1.se M2.est_sig M2.se M3.est_sig M3.se M4.est_sig M4.se M5.est_sig M5.se
## ────────────────────────────────────────────────────────────────────────────────────────────────
## 1  X.ON.A    0.060     0.040  NA               NA                NA               NA            
## 2  X.ON.C    0.164***  0.042  0.173***  0.041  0.173***  0.041   0.137**  0.045   NA            
## 3  X.ON.ES   0.116**   0.040  0.120**   0.040  0.120**   0.040   0.160*** 0.045   0.181*** 0.044
## 4  X.ON.EX   0.140***  0.042  0.133***  0.040  0.133***  0.040   0.137**  0.045   0.162*** 0.044
## 5  X.ON.O    -0.053    0.040  NA               NA                NA               NA            
## 6  X.WITH.Y  -0.567*** 0.163  -0.576*** 0.169  -0.576*** 0.169   -0.260   0.189   -0.363*  0.170
## 7  Y.ON.C    NA               NA               NA                0.127*   0.061   NA            
## 8  Y.ON.X    0.749***  0.170  0.757***  0.176  0.757***  0.176   0.415*   0.202   0.544**  0.177
## ────────────────────────────────────────────────────────────────────────────────────────────────

7 DIFFERENT RESEARCH DESIGN

8 LONGITUDINAL DESGIN

Change the data to CaseID=19

P <- cor_matrices$r
P["X", "Y"] <- 0.14
P["Y", "X"] <- 0.14

NP=cor_matrices$`Sample size`
NP["X", "Y"] <- 2987
NP["Y", "X"] <- 2987

KP=cor_matrices$`K Size`
KP["X", "Y"] <- 21
KP["Y", "X"] <- 21

Nnum=Matrix.Stats(NP)
## Statistics of matrix
## ────────────────────────────
##    harmonic_mean         sum
## ────────────────────────────
## 1      20722.545 1009955.000
## ────────────────────────────
## Descriptive Statistics:
## ──────────────────────────────────────────────────────────────────────
##     N     Mean       SD |   Median     Min       Max Skewness Kurtosis
## ──────────────────────────────────────────────────────────────────────
##    21 48093.10 33825.24 | 41939.00 2987.00 106149.00     0.25    -1.56
## ──────────────────────────────────────────────────────────────────────
Knum=Matrix.Stats(KP)
## Statistics of matrix
## ─────────────────────────
##    harmonic_mean      sum
## ─────────────────────────
## 1         69.494 1945.000
## ─────────────────────────
## Descriptive Statistics:
## ─────────────────────────────────────────────────────────
##     N  Mean    SD | Median   Min    Max Skewness Kurtosis
## ─────────────────────────────────────────────────────────
##    21 92.62 42.25 |  92.00 21.00 163.00     0.00    -1.38
## ─────────────────────────────────────────────────────────
Nhar=round(Nnum$sum/Knum$sum,0)#num$harmonic_mean

8.1 IVs are OCEAN

# 1. Model Specification
IV.ocean <- IVb
cat(IV.ocean)
## X ~ O + C + EX + A + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.oceanL <- sem(IV.ocean,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.oceanL)
## 
## Fit Measures (lavaan):
## χ²(4, N = 519) = 7.868, p = 0.097 .  
## χ²/df = 1.967
## AIC = 10073.340 (Akaike Information Criterion)
## BIC = 10175.386 (Bayesian Information Criterion)
## CFI = 0.985 (Comparative Fit Index)
## TLI = 0.923 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.972 (Normed Fit Index)
## IFI = 0.986 (Incremental Fit Index)
## GFI = 0.996 (Goodness-of-Fit Index)
## AGFI = 0.970 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.043, 90% CI [0.000, 0.088] (Root Mean Square Error of Approximation)
## SRMR = 0.020 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ────────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.      z     p       LLCI  ULCI   Beta
## ────────────────────────────────────────────────────────────────────────
## Regression Paths:                                                       
##   X <- O             -0.053 (0.037) -1.422  .155     -0.126 0.020 -0.053
##   X <- C              0.165 (0.039)  4.186 <.001 ***  0.088 0.242  0.165
##   X <- EX             0.140 (0.039)  3.550 <.001 ***  0.063 0.217  0.140
##   X <- A              0.060 (0.037)  1.615  .106     -0.013 0.134  0.060
##   X <- ES             0.114 (0.038)  3.024  .002 **   0.040 0.189  0.114
##   Y <- X              0.751 (0.166)  4.518 <.001 ***  0.425 1.077  0.751
## ────────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "A", "C", "EX", "O")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.oceanL,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          54.090       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.09853
# 3. X->Y
lavaanPlot(model = IV.oceanL, coefs = T, sig = 0.05)

8.2 Test for excluding the unqualified IVs

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- mplus2lavaan.modelSyntax(IV)
cat(IV)
## X ~ C + EX + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.cenL <- sem(IV,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.cenL)
## 
## Fit Measures (lavaan):
## χ²(6, N = 519) = 12.049, p = 0.061 .  
## χ²/df = 2.008
## AIC = 10073.521 (Akaike Information Criterion)
## BIC = 10167.063 (Bayesian Information Criterion)
## CFI = 0.977 (Comparative Fit Index)
## TLI = 0.920 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.958 (Normed Fit Index)
## IFI = 0.978 (Incremental Fit Index)
## GFI = 0.993 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.044, 90% CI [0.000, 0.080] (Root Mean Square Error of Approximation)
## SRMR = 0.025 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ─────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.     z     p      LLCI  ULCI  Beta
## ─────────────────────────────────────────────────────────────────────
## Regression Paths:                                                    
##   X <- C              0.174 (0.039) 4.498 <.001 *** 0.098 0.249 0.174
##   X <- EX             0.133 (0.038) 3.515 <.001 *** 0.059 0.207 0.133
##   X <- ES             0.119 (0.038) 3.137  .002 **  0.045 0.193 0.119
##   Y <- X              0.760 (0.172) 4.410 <.001 *** 0.422 1.097 0.760
## ─────────────────────────────────────────────────────────────────────
## 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.cenL,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          50.449       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.cenL, coefs = T, sig = 0.05)
IV.Lf <- SEM.FitCombine(list(IV.oceanL,IV.cenL), Mplus = F)#xxx
## SEM Model fit results
## ──────────────────────────────────────────────────────────────────────────────────────
##      Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue   CFI  SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────
## 1  Model 1        7.868     4.000         0.097 0.985 0.020                           
## 2  Model 2       12.049     6.000         0.061 0.977 0.025      4.181   2.000   0.124
## ──────────────────────────────────────────────────────────────────────────────────────
#result[[2]][, Disgin := "Longitudinal design"]

8.3 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.L <- sem(IV,sample.cov = P,sample.nobs = Nhar)
lavaan_summary(IV.L)
## 
## Fit Measures (lavaan):
## χ²(6, N = 519) = 37.454, p = 1e-06 ***
## χ²/df = 6.242
## AIC = 8675.500 (Akaike Information Criterion)
## BIC = 8739.278 (Bayesian Information Criterion)
## CFI = 0.849 (Comparative Fit Index)
## TLI = 0.622 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.832 (Normed Fit Index)
## IFI = 0.855 (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.132] (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.174 (0.039) 4.490 <.001 *** 0.098 0.250 0.172
##   X <- EX             0.133 (0.038) 3.523 <.001 *** 0.059 0.207 0.133
##   X <- ES             0.119 (0.038) 3.147  .002 **  0.045 0.193 0.119
##   Y <- X              0.760 (0.177) 4.295 <.001 *** 0.413 1.106 0.759
## ─────────────────────────────────────────────────────────────────────
## 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.L,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          47.859       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.L, coefs = T, sig = 0.05)

9 SUPERVISOR-RATING OF PERFORMANCE

Change the data to CaseID=40

P <- cor_matrices$r
P["X", "Y"] <- 0.16
P["Y", "X"] <- 0.16

NP=cor_matrices$`Sample size`
NP["X", "Y"] <- 8796
NP["Y", "X"] <- 8796

KP=cor_matrices$`K Size`
KP["X", "Y"] <- 50
KP["Y", "X"] <- 50

Nnum=Matrix.Stats(NP)
## Statistics of matrix
## ────────────────────────────
##    harmonic_mean         sum
## ────────────────────────────
## 1      26505.346 1015764.000
## ────────────────────────────
## Descriptive Statistics:
## ──────────────────────────────────────────────────────────────────────
##     N     Mean       SD |   Median     Min       Max Skewness Kurtosis
## ──────────────────────────────────────────────────────────────────────
##    21 48369.71 33459.70 | 41939.00 8796.00 106149.00     0.28    -1.57
## ──────────────────────────────────────────────────────────────────────
Knum=Matrix.Stats(KP)
## Statistics of matrix
## ─────────────────────────
##    harmonic_mean      sum
## ─────────────────────────
## 1         76.485 1974.000
## ─────────────────────────
## Descriptive Statistics:
## ─────────────────────────────────────────────────────────
##     N  Mean    SD | Median   Min    Max Skewness Kurtosis
## ─────────────────────────────────────────────────────────
##    21 94.00 40.22 |  92.00 38.00 163.00     0.11    -1.47
## ─────────────────────────────────────────────────────────
Nhar=round(Nnum$sum/Knum$sum,0)#num$harmonic_mean

9.1 IVs are OCEAN

# 1. Model Specification
IV.ocean <- IVb
cat(IV.ocean)
## X ~ O + C + EX + A + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.oceanS <- sem(IV.ocean,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.oceanS)
## 
## Fit Measures (lavaan):
## χ²(4, N = 515) = 7.986, p = 0.092 .  
## χ²/df = 1.997
## AIC = 9994.104 (Akaike Information Criterion)
## BIC = 10095.964 (Bayesian Information Criterion)
## CFI = 0.985 (Comparative Fit Index)
## TLI = 0.921 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.972 (Normed Fit Index)
## IFI = 0.986 (Incremental Fit Index)
## GFI = 0.996 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.044, 90% CI [0.000, 0.089] (Root Mean Square Error of Approximation)
## SRMR = 0.020 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ────────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.      z     p       LLCI  ULCI   Beta
## ────────────────────────────────────────────────────────────────────────
## Regression Paths:                                                       
##   X <- O             -0.053 (0.038) -1.410  .159     -0.127 0.021 -0.053
##   X <- C              0.165 (0.040)  4.135 <.001 ***  0.087 0.243  0.165
##   X <- EX             0.140 (0.040)  3.508 <.001 ***  0.062 0.218  0.140
##   X <- A              0.060 (0.038)  1.592  .111     -0.014 0.135  0.060
##   X <- ES             0.115 (0.038)  3.007  .003 **   0.040 0.190  0.115
##   Y <- X              0.750 (0.165)  4.549 <.001 ***  0.427 1.073  0.750
## ────────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "A", "C", "EX", "O")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.oceanS,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          53.737       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.09853
# 3. X->Y
lavaanPlot(model = IV.oceanS, coefs = T, sig = 0.05)

9.2 Test for excluding the unqualified IVs

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- mplus2lavaan.modelSyntax(IV)
cat(IV)
## X ~ C + EX + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.cenS <- sem(IV,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.cenS)
## 
## Fit Measures (lavaan):
## χ²(6, N = 515) = 12.064, p = 0.061 .  
## χ²/df = 2.011
## AIC = 9994.182 (Akaike Information Criterion)
## BIC = 10087.553 (Bayesian Information Criterion)
## CFI = 0.977 (Comparative Fit Index)
## TLI = 0.920 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.958 (Normed Fit Index)
## IFI = 0.978 (Incremental Fit Index)
## GFI = 0.993 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.044, 90% CI [0.000, 0.081] (Root Mean Square Error of Approximation)
## SRMR = 0.025 (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.447 <.001 *** 0.097 0.250 0.173
##   X <- EX             0.133 (0.038) 3.473 <.001 *** 0.058 0.208 0.133
##   X <- ES             0.120 (0.038) 3.119  .002 **  0.044 0.195 0.120
##   Y <- X              0.759 (0.171) 4.441 <.001 *** 0.424 1.093 0.759
## ─────────────────────────────────────────────────────────────────────
## 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.cenS,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          50.115       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.cenS, coefs = T, sig = 0.05)
IV.Mf <- SEM.FitCombine(list(IV.oceanS,IV.cenS), Mplus = F)#xxx
## SEM Model fit results
## ──────────────────────────────────────────────────────────────────────────────────────
##      Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue   CFI  SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────
## 1  Model 1        7.986     4.000         0.092 0.985 0.020                           
## 2  Model 2       12.064     6.000         0.061 0.977 0.025      4.078   2.000   0.130
## ──────────────────────────────────────────────────────────────────────────────────────
#result[[2]][, Disgin := "Longitudinal design"]

9.3 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.M <- sem(IV,sample.cov = P,sample.nobs = Nhar) #xxx
lavaan_summary(IV.M)
## 
## Fit Measures (lavaan):
## χ²(6, N = 515) = 37.283, p = 2e-06 ***
## χ²/df = 6.214
## AIC = 8606.835 (Akaike Information Criterion)
## BIC = 8670.498 (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.440 <.001 *** 0.097 0.250 0.172
##   X <- EX             0.133 (0.038) 3.482 <.001 *** 0.058 0.208 0.133
##   X <- ES             0.120 (0.038) 3.129  .002 **  0.045 0.195 0.120
##   Y <- X              0.759 (0.175) 4.325 <.001 *** 0.415 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.M,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          47.546       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.M, coefs = T, sig = 0.05)

10 OBJECTIVE SOURCES RESULTS

Change the data to CaseID=13

P <- cor_matrices$r
P["X", "Y"] <- 0.16
P["Y", "X"] <- 0.16

NP=cor_matrices$`Sample size`
NP["X", "Y"] <- 5216
NP["Y", "X"] <- 5216

KP=cor_matrices$`K Size`
KP["X", "Y"] <- 34
KP["Y", "X"] <- 34

Nnum=Matrix.Stats(NP)
## Statistics of matrix
## ────────────────────────────
##    harmonic_mean         sum
## ────────────────────────────
## 1      24128.980 1012184.000
## ────────────────────────────
## Descriptive Statistics:
## ──────────────────────────────────────────────────────────────────────
##     N     Mean       SD |   Median     Min       Max Skewness Kurtosis
## ──────────────────────────────────────────────────────────────────────
##    21 48199.24 33679.81 | 41939.00 5216.00 106149.00     0.26    -1.57
## ──────────────────────────────────────────────────────────────────────
Knum=Matrix.Stats(KP)
## Statistics of matrix
## ─────────────────────────
##    harmonic_mean      sum
## ─────────────────────────
## 1         73.950 1958.000
## ─────────────────────────
## Descriptive Statistics:
## ─────────────────────────────────────────────────────────
##     N  Mean    SD | Median   Min    Max Skewness Kurtosis
## ─────────────────────────────────────────────────────────
##    21 93.24 41.23 |  92.00 34.00 163.00     0.07    -1.46
## ─────────────────────────────────────────────────────────
Nhar=round(Nnum$sum/Knum$sum,0)#num$harmonic_mean

10.1 IVs are OCEAN

# 1. Model Specification
IV.ocean <- IVb
cat(IV.ocean)
## X ~ O + C + EX + A + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.oceanO <- sem(IV.ocean,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.oceanO)
## 
## Fit Measures (lavaan):
## χ²(4, N = 517) = 8.017, p = 0.091 .  
## χ²/df = 2.004
## AIC = 10032.757 (Akaike Information Criterion)
## BIC = 10134.710 (Bayesian Information Criterion)
## CFI = 0.985 (Comparative Fit Index)
## TLI = 0.921 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.972 (Normed Fit Index)
## IFI = 0.986 (Incremental Fit Index)
## GFI = 0.996 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.044, 90% CI [0.000, 0.089] (Root Mean Square Error of Approximation)
## SRMR = 0.020 (Standardized Root Mean Square Residual)
## 
## Model Estimates (lavaan):
## ────────────────────────────────────────────────────────────────────────
##                    Estimate    S.E.      z     p       LLCI  ULCI   Beta
## ────────────────────────────────────────────────────────────────────────
## Regression Paths:                                                       
##   X <- O             -0.053 (0.038) -1.413  .158     -0.127 0.021 -0.053
##   X <- C              0.165 (0.040)  4.143 <.001 ***  0.087 0.243  0.165
##   X <- EX             0.140 (0.040)  3.515 <.001 ***  0.062 0.218  0.140
##   X <- A              0.060 (0.038)  1.595  .111     -0.014 0.135  0.060
##   X <- ES             0.115 (0.038)  3.013  .003 **   0.040 0.190  0.115
##   Y <- X              0.750 (0.165)  4.558 <.001 ***  0.427 1.072  0.750
## ────────────────────────────────────────────────────────────────────────
## Note. Raw (Standard) Confidence Interval (CI) and SE.
# 2.1. Wald
variables <- c("ES", "A", "C", "EX", "O")
par.names <- paste0('X~',variables)
Wald.test(fit=IV.oceanO,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          53.946       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.09853
# 3. X->Y
lavaanPlot(model = IV.oceanO, coefs = T, sig = 0.05)

10.2 Test for excluding the unqualified IVs

# 1. Model Specification
IV <- gsub("X ON O C EX A ES;", "X ON C EX ES;", IV.S)
IV <- mplus2lavaan.modelSyntax(IV)
cat(IV)
## X ~ C + EX + ES
## Y ~ X
## Y ~~ X
## O ~~ C + EX + A + ES
## C ~~ EX + A + ES
## EX ~~ A + ES
## A ~~ ES
# 1.2 model fit
IV.cenO <- sem(IV,sample.cov = P,sample.nobs = Nhar)#xxx
lavaan_summary(IV.cenO)
## 
## Fit Measures (lavaan):
## χ²(6, N = 517) = 12.111, p = 0.060 .  
## χ²/df = 2.019
## AIC = 10032.850 (Akaike Information Criterion)
## BIC = 10126.307 (Bayesian Information Criterion)
## CFI = 0.977 (Comparative Fit Index)
## TLI = 0.919 (Tucker-Lewis Index; Non-Normed Fit Index, NNFI)
## NFI = 0.958 (Normed Fit Index)
## IFI = 0.978 (Incremental Fit Index)
## GFI = 0.993 (Goodness-of-Fit Index)
## AGFI = 0.969 (Adjusted Goodness-of-Fit Index)
## RMSEA = 0.044, 90% CI [0.000, 0.081] (Root Mean Square Error of Approximation)
## SRMR = 0.025 (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.456 <.001 *** 0.097 0.250 0.173
##   X <- EX             0.133 (0.038) 3.479 <.001 *** 0.058 0.208 0.133
##   X <- ES             0.120 (0.038) 3.125  .002 **  0.045 0.195 0.120
##   Y <- X              0.759 (0.170) 4.449 <.001 *** 0.424 1.093 0.759
## ─────────────────────────────────────────────────────────────────────
## 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.cenO,par.names,method='UIMASEM')%>%setDT()%>%print_table()
## ─────────────────────────────
##    Wald.Statistic Wald.pValue
## ─────────────────────────────
## 1          50.310       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.cenO, coefs = T, sig = 0.05)
IV.Of <- SEM.FitCombine(list(IV.oceanO,IV.cenO), Mplus = F)#xxx
## SEM Model fit results
## ──────────────────────────────────────────────────────────────────────────────────────
##      Model ChiSqM_Value ChiSqM_DF ChiSqM_PValue   CFI  SRMR ChiSq_Diff DF_Diff ChiSq_p
## ──────────────────────────────────────────────────────────────────────────────────────
## 1  Model 1        8.017     4.000         0.091 0.985 0.020                           
## 2  Model 2       12.111     6.000         0.060 0.977 0.025      4.094   2.000   0.129
## ──────────────────────────────────────────────────────────────────────────────────────
#result[[2]][, Disgin := "Longitudinal design"]

10.3 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()