You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为bootstrapLavaan生成的CFA模型绘制路径图?

问题:bootstrapLavaan结果无法生成CFA路径图

首次使用bootstrapLavaan做验证性因子分析(CFA),模型运行正常且能完成结果汇总,但无法生成路径图。问题出在bootstrapLavaan的输出是向量集合,无法被semPaths直接识别。


初始CFA模型构建代码

install(lavaan)

# Fit initial CFA model using lavaan
initial_cfa_model <- lavaan::cfa(initial_cfa_model_syntax, data = cfa_data_rmv, missing = "fiml")

# Create summary
initial_cfa <- summary(initial_cfa_model, fit.measures=TRUE, standardized=TRUE, rsquare=TRUE)

# Custom function to extract measures from bootstrapLavaan
custom_extract <- function(lavaan_obj) {
  
  # Extract fit measures
  fit_measures <- lavaan::fitMeasures(lavaan_obj, fit.measures = c("chisq", "cfi", "tli", "rmsea", "srmr", "aic", "bic"))
   
  # Extract coefficients
  coefs <- lavaan::coef(lavaan_obj)
  
  # Combine fit measures and coefficients
  combined_results <- c(fit_measures, coefs)
  
  # Return combined results
  return(combined_results)
}

# Bootstrap the initial CFA model
boot_cfa_model <- lavaan::bootstrapLavaan(initial_cfa_model, 
                                          R = boot_iterations, 
                                          iseed = seed, 
                                          FUN = custom_extract
                                          )

成功绘制初始CFA路径图的代码

install(semPaths)

# Plot the initial CFA model
initial_cfa_plot <- semPaths(initial_cfa_model, 
                             whatLabels = "est", 
                             edge.label.cex = 0.7, 
                             layout = "tree", 
                             intercepts = FALSE, 
                             residuals = FALSE, 
                             style = "lisrel", 
                             curveAdjacent = TRUE, 
                             rotation = 2)

initial_cfa_plot

失败的bootstrap路径图代码

# Plot the boot CFA model
boot_cfa_plot <- semPaths(boot_cfa_model, 
                             whatLabels = "est", 
                             edge.label.cex = 0.7, 
                             layout = "tree", 
                             intercepts = FALSE, 
                             residuals = FALSE, 
                             style = "lisrel", 
                             curveAdjacent = TRUE, 
                             rotation = 2)

boot_cfa_plot

解决方法

核心原因

bootstrapLavaan搭配自定义FUN时,返回的是每次bootstrap迭代提取的向量集合(最终为矩阵/数组),而semPaths仅支持输入lavaan拟合对象(如cfa()返回的lavaan类对象),无法直接处理汇总后的bootstrap结果。

可行方案

方案1:绘制bootstrap平均参数路径图

提取bootstrap迭代的平均系数,替换到初始模型的参数中,再用semPaths绘制:

# 提取bootstrap后的平均系数(仅保留初始模型的参数部分)
boot_avg_coefs <- colMeans(boot_cfa_model)[names(lavaan::coef(initial_cfa_model))]

# 复制初始模型对象,替换参数为bootstrap平均值
boot_avg_model <- initial_cfa_model
boot_avg_model@Fit@est <- boot_avg_coefs

# 绘制路径图
boot_cfa_plot <- semPaths(boot_avg_model, 
                          whatLabels = "est", 
                          edge.label.cex = 0.7, 
                          layout = "tree", 
                          intercepts = FALSE, 
                          residuals = FALSE, 
                          style = "lisrel", 
                          curveAdjacent = TRUE, 
                          rotation = 2)

boot_cfa_plot

方案2:查看单个bootstrap迭代的路径图

修改bootstrapLavaan的FUN,返回完整的拟合对象(注意:R值较大时会占用大量内存):

# 修改自定义函数,返回完整lavaan拟合对象
custom_extract_full <- function(lavaan_obj) {
  return(lavaan_obj)
}

# 重新运行bootstrap
boot_cfa_models <- lavaan::bootstrapLavaan(initial_cfa_model, 
                                           R = boot_iterations, 
                                           iseed = seed, 
                                           FUN = custom_extract_full
                                           )

# 绘制第1次迭代的模型路径图(可替换为任意迭代序号)
boot_cfa_plot <- semPaths(boot_cfa_models[[1]], 
                          whatLabels = "est", 
                          edge.label.cex = 0.7, 
                          layout = "tree", 
                          intercepts = FALSE, 
                          residuals = FALSE, 
                          style = "lisrel", 
                          curveAdjacent = TRUE, 
                          rotation = 2)

boot_cfa_plot

方案3:在初始路径图上标注bootstrap置信区间

计算bootstrap参数的95%置信区间,手动添加到路径图标签中:

# 计算bootstrap参数的95%置信区间
boot_ci <- apply(boot_cfa_model, 2, quantile, probs = c(0.025, 0.975))

# 提取初始模型的标准化参数及对应关系
std_coefs <- lavaan::standardizedSolution(initial_cfa_model)[, c("lhs", "op", "rhs", "est.std")]

# 匹配置信区间到对应参数
coef_names <- names(lavaan::coef(initial_cfa_model))
std_coefs$ci_lower <- boot_ci["2.5%", coef_names]
std_coefs$ci_upper <- boot_ci["97.5%", coef_names]

# 自定义标签格式:估计值\n[95%CI]
std_coefs$label <- paste0(round(std_coefs$est.std, 2), "\n[", round(std_coefs$ci_lower, 2), ",", round(std_coefs$ci_upper, 2), "]")

# 绘制带置信区间的路径图
boot_cfa_plot <- semPaths(initial_cfa_model, 
                          whatLabels = "custom", 
                          customLabels = std_coefs$label,
                          edge.label.cex = 0.7, 
                          layout = "tree", 
                          intercepts = FALSE, 
                          residuals = FALSE, 
                          style = "lisrel", 
                          curveAdjacent = TRUE, 
                          rotation = 2)

boot_cfa_plot

内容的提问来源于stack exchange,提问作者davwillev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 08:13:17