-1

我是一名研究生,使用线性回归(计数)模型来了解鱼类进出潮汐湿地的驱动因素。我目前正在尝试在 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. 正在为每个模型生成模型公式列
4

3 回答 3

1

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

生成类似公式的字符串的最简单方法就是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
    )
于 2020-05-31T10:12:33.410 回答
0
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
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
)

#slightly rename intercept column  
colnames(sel.table)[3] <- 'Intercept' 

# #select summary columns for model selection table
# sel.table <- sel.table %>%
#   select(Model,formula,Intercept,df,logLik,AICc,delta,weight)

print <- flextable(head(sel.table))
print <- autofit(print)
print
于 2020-06-08T13:12:58.627 回答
-1

由于您的问题不可重现,我会尝试其他方法,也许这就是您正在寻找的:

data = mtcars

models = list(
  model1 = lm(mpg ~ cyl, data = data),
  model2 = lm(mpg ~ cyl + hp, data = data)
  )

data.frame(name = as.character(lapply(models, `[[`, c(10,2))),
           other.column = NA)
#>             name other.column
#> 1      mpg ~ cyl           NA
#> 2 mpg ~ cyl + hp           NA

reprex 包(v0.3.0)于 2020 年 5 月 28 日创建

lm 对象的公式(调用)位于列表的第 10 位。您实际上可以在键入时数数model1$。您可以使用rownames()而不是列,但不建议这样做。

可重现示例后编辑

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 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]

# formulas as names
sel.table$name = as.character(lapply(models, `[[`, c(10,2)))

# reordering
sel.table = sel.table[, c(6,1,2,3,4,5)]

sel.table
#>             name df    logLik     AICc    delta    weight
#> 3      mpg ~ cyl  5 -78.14329 168.5943 0.000000 0.5713716
#> 1 mpg ~ cyl + hp  3 -81.65321 170.1636 1.569298 0.2607054
#> 2 mpg ~ cyl * hp  4 -80.78092 171.0433 2.449068 0.1679230

reprex 包于 2020-05-31 创建(v0.3.0)

于 2020-05-28T23:55:30.840 回答