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

如何使用R包modelsummary生成factanal因子分析汇总表(两种尝试失败)

解决factanal因子分析结果用modelsummary生成LaTeX汇总表的问题

方法一:直接用modelsummary适配factanal对象

先确保modelsummary是最新版本(旧版本对factanal的支持可能不完善):

install.packages("modelsummary")

拟合模型并生成LaTeX格式的汇总表:

library(modelsummary)
library(tidyverse)

# 拟合单因子分析模型
fa_model <- iris %>% 
  select(-Species) %>% 
  factanal(factors = 1, rotation = "varimax") # 可选添加因子旋转方法

# 生成定制化LaTeX表格
modelsummary(fa_model,
             statistic = NULL, # 因子分析核心展示载荷,无需默认统计量
             gof_map = list(
               list(raw = "statistic", clean = "卡方值", fmt = 2),
               list(raw = "df", clean = "自由度", fmt = 0),
               list(raw = "p.value", clean = "p值", fmt = 4),
               list(raw = "gfi", clean = "GFI", fmt = 4)
             ), # 自定义要展示的拟合优度指标
             title = "iris数据集单因子分析结果",
             output = "latex") # 直接输出LaTeX代码,也可指定路径保存为.tex文件

方法二:用broom提取结果后手动构建表格

如果直接调用modelsummary失败,可通过broom包拆分提取载荷和拟合指标,再生成表格:

library(modelsummary)
library(tidyverse)
library(broom)

fa_model <- iris %>% 
  select(-Species) %>% 
  factanal(factors = 1)

# 提取因子载荷数据
loadings_table <- tidy(fa_model) %>% 
  select(variable, estimate) %>% 
  rename("观测变量" = variable, "因子载荷" = estimate)

# 提取拟合优度指标
gof_stats <- glance(fa_model) %>% 
  mutate(
    卡方值 = round(statistic, 2),
    自由度 = df,
    p值 = round(p.value, 4),
    GFI = round(gfi, 4)
  ) %>% 
  select(卡方值, 自由度, p值, GFI)

# 生成LaTeX表格,合并载荷与拟合指标
modelsummary(loadings_table,
             output = "latex",
             title = "因子分析载荷表",
             notes = paste0("拟合指标: 卡方值=", gof_stats$卡方值, 
                           ", 自由度=", gof_stats$自由度, 
                           ", p值=", gof_stats$p值, 
                           ", GFI=", gof_stats$GFI))

注意事项

  • factanal要求输入数据无缺失值,需提前处理缺失;
  • 若需要因子旋转,在factanal中添加rotation参数(如rotation = "varimax");
  • 导出LaTeX时,可通过output = "xxx.tex"直接将结果保存到本地文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:05:11