使用stargazer合并4个LM模型时出现条件长度大于1错误求助
Stargazer多LM模型合并报错排查与解决
问题详情
为论文开展回归分析,4个国家的数据库编码完全一致,单个LM模型可通过stargazer正常输出结果,但合并4个模型时出现错误:
Error in if (is.na(s)) { : the condition has length > 1
可正常运行的单个模型代码
modelouruguayedad <- lm(age ~ religiosity + ideology, data = Uruguay1) stargazer::stargazer( modelouruguayedad, type = 'text', title = "" , no.space = T, digits = 4, star.cutoffs = c(.05, .01, .001), dep.var.labels = 'Apoyo al matrimonio igualitario', model.numbers = T, model.names = F, object.names = F, column.labels = '', omit.stat = c("f", "ser"))
报错的多模型代码
stargazer::stargazer( modelocolombiaedad, modelouruguayedad, modelobrasiledad, modelocolombiaedad, type = 'text', title = "" , no.space = T, digits = 4, star.cutoffs = c(.05, .01, .001), dep.var.labels = 'Edad', model.numbers = T, model.names = F, object.names = F, column.labels = c("Argentina", "Brasil", "Colombia", "Uruguay"), covariate.labels = c("Religiosidad", "Ideologia"), omit.stat = c("f", "ser"))
解决步骤
- 修正模型传入错误:多模型代码中重复传入了
modelocolombiaedad,但column.labels是4个国家的标签,模型数量与标签数量不匹配。需将重复的模型替换为对应阿根廷的modeloargentinaedad,保证4个模型对应4个标签。 - 验证变量一致性:确认所有模型的公式、变量名称和类型完全一致:
- 每个
lm模型的公式均为age ~ religiosity + ideology - 所有数据集(如Uruguay1、Colombia1)中,
age、religiosity、ideology的名称、数据类型(数值/因子)无差异
- 每个
- 简化代码排查:先运行最简版多模型代码测试:
若正常输出,再逐步添加stargazer::stargazer(modeloargentinaedad, modelobrasiledad, modelocolombiaedad, modelouruguayedad, type = 'text')digits、covariate.labels等参数,定位触发错误的具体参数。 - 更新包版本:旧版stargazer存在多模型处理bug,运行
update.packages("stargazer")更新至最新版本后重试。
内容的提问来源于stack exchange,提问作者Clara Scalise
相关产品推荐
相关产品推荐

