首页 > 解决方案 > 为模型汇总表提取模型协变量的问题

问题描述

我是一名研究生,使用线性回归(计数)模型来了解鱼类进出潮汐湿地的驱动因素。我目前正在尝试在 r 中生成一个值得发表的模型汇总表。我一直在使用 sel.table 函数,该函数为此目的运行良好。

但是,我无法生成包含各个模型公式的列。下面是我的代码,它基于使用 MuMIn 包的一些很好的说明。https://sites.google.com/site/rforfishandwildlifegrads/home/mumin_usage_examples

回顾一下,我的问题与下面的最后一行代码有关,

如何将模型公式插入模型选择表中。**

install.packages("MuMIn")
library(MuMIn)

data = mtcars

models = list(
  model1 <- lm(mpg ~ cyl, data = data),
  model2 <- lm(mpg ~ cyl + hp, data = data),
  model3 <- lm(mpg ~ cyl * hp, data = data)
)

#create an object “out.put” that contains all of the model selection information
out.put <- model.sel(models)

#coerce the object out.put into a data frame
sel.table <-as.data.frame(out.put)[6:10]

#add a column for model names
sel.table$Model <- rownames(sel.table)

#replace model name with formulas
for(i in 1:nrow(sel.table)) sel.table$Model[i]<- as.character(formula(paste(sel.table$Model[i])))[3]

#Any help on this topic would be greatly appreciated!

更新代码

我提取模型名称的方法非常笨拙,但除此之外,这段代码似乎生成了我想要的(完整的模型选择表)。另外,我不确定模型系数是否正确显示,但我希望对此进行跟进以获得最终答案。

data = mtcars

#write linear models
models = list(
  model1 <- lm(mpg ~ cyl, data = data),
  model2 <- lm(mpg ~ cyl + hp, data = data),
  model3 <- lm(mpg ~ cyl * hp + disp, data = data),
  model4 <- lm(mpg ~ cyl * hp + disp + wt + drat, data = data)
)

#create an object “out.put” that contains all of the model selection information
out.put <- model.sel(models)

#coerce the object out.put into a data frame
sel.table <-as.data.frame(out.put)

#slightly rename intercept column
names(sel.table)[1]="Intercept"

#select variables to display in model summary table
sel.table <- sel.table %>% 
 select(Intercept,cyl,hp,disp,wt,drat,df,logLik,AICc,delta)

#round numerical coumns
sel.table[,1:6]<- round(sel.table[,1:6],2)

sel.table[,8:10]<-round(sel.table[,8:10],2)

#add a column for model (row) names
sel.table$Model <- rownames(sel.table)

#extract model formulas
form <- data.frame(name = as.character(lapply(models, `[[`, c(10,2))))

#generate a column with model (row) numbers (beside associated model formulas)
form <- form %>% 
  mutate(Model=(1:4))

#merge model table and model formulas
sum_table <- merge (form,sel.table,by="Model")

#rename model equation column 
names(sum_table)[2]="Formula"

print <- flextable(head(sum_table))
print <- autofit(print)
print

20 年 6 月 1 日更新:

下面的图片描述了我在使用代码时遇到的两个问题。我找到了第一个问题的解决方法,但我仍在调查第二个问题。 在此处查看详细信息

  1. 模型最终被错误编号
  2. 正在为每个模型生成模型公式列

标签: rdatatablemodelsummarymumin

解决方案


我相信您遵循的示例中缺少部分代码,这就是您的代码不起作用的原因。

生成类似公式的字符串的最简单方法就是deparse在模型 s 的右侧formula(即第 3 个元素):

sapply(get.models(out.put, TRUE), function(mo) deparse(formula(mo)[[3]], width.cutoff = 500))

或者,如果您想A*B将 ' 扩展为A + B + A:B

sapply(get.models(out.put, TRUE), function(mo) deparse(terms(formula(mo), simplify = TRUE)[[3]], width.cutoff = 500))

更新:原始示例代码改进和简化:

library(MuMIn)

data <- mtcars

#! Feed the models directly to `model.sel`. No need to create a separate list of
#! models.
gm <- lm(mpg ~ cyl, data = data)
out.put <- model.sel(
  model1 = gm,
  model2 = update(gm, . ~. + hp),
  model3 = update(gm, . ~ . * hp + disp),
  model4 = update(gm, . ~ . * hp + disp + wt + drat)
  )

sel.table <- out.put
sel.table$family <- NULL
sel.table <- round(sel.table, 2)
#! Use `get.models` to get the list of models in the same order as in the
#! selection table
sel.table <- cbind(
    Model = 
#! Update (2): model number according to their original order, use:
       attr(out.put, "order"),
#!     otherwise: seq(nrow(sel.table)),
#!
#! Update (2): add a large `width.cutoff` to `deparse` so that the result is
#!         always a single string and `sapply` returns a character vector
#!         rather than a list.
#!         For oversize formulas, use `paste0(deparse(...), collapse = "")`  
    formula = sapply(get.models(out.put, TRUE),
        function(mo) deparse(formula(mo)[[3]], width.cutoff = 500)),
#!
    sel.table
    )

推荐阅读