在模型列表上使用lapply

时间:2013-03-02 09:10:07

标签: r lapply

我已经生成了一个模型列表,并希望创建一个汇总表。

例如,这里有两个模型:

x <- seq(1:10)
y <- sin(x)^2
model1 <- lm(y ~ x)
model2 <- lm(y ~ x + I(x^2) + I(x^3))

和两个公式,第一个从公式

的组件生成方程
get.model.equation <- function(x) {
  x <- as.character((x$call)$formula)
  x <- paste(x[2],x[1],x[3])
}

和第二个生成模型名称为字符串

get.model.name <- function(x) {
  x <- deparse(substitute(x))
}

通过这些,我创建了一个汇总表

model.list <- list(model1, model2)
AIC.data <- lapply(X = model.list, FUN = AIC)
AIC.data <- as.numeric(AIC.data)
model.models <- lapply(X = model.list, FUN = get.model)
model.summary <- cbind(model.models, AIC.data)
model.summary <- as.data.frame(model.summary)
names(model.summary) <- c("Model", "AIC")
model.summary$AIC <- unlist(model.summary$AIC)
rm(AIC.data)
model.summary[order(model.summary$AIC),]

一切正常。 我想使用get.model.name

将模型名称添加到表中
x <- get.model.name(model1)

我给了我“model1”。

所以现在我将该函数应用于模型列表

model.names <- lapply(X = model.list, FUN = get.model.name)

但现在取代 model1 我得到 X [[1L]]

如何获取 model1 而不是 X [[1L]]

我正在看一张看起来像这样的表:

 Model                Formula       AIC
model1                  y ~ x  11.89136
model2 y ~ x + I(x^2) + I(x^3) 15.03888

3 个答案:

答案 0 :(得分:8)

你想要这样的东西吗?

model.list <- list(model1 = lm(y ~ x), 
                   model2 = lm(y ~ x + I(x^2) + I(x^3)))
sapply(X = model.list, FUN = AIC)

答案 1 :(得分:2)

我做这样的事情:

model.list <- list(model1 = lm(y ~ x),
                   model2 = lm(y ~ x + I(x^2) + I(x^3)))
# changed Reduce('rbind', ...) to do.call(rbind, ...) (Hadley's comment)
do.call(rbind, 
        lapply(names(model.list), function(x) 
          data.frame(model = x, 
          formula = get.model.equation(model.list[[x]]), 
          AIC = AIC(model.list[[x]])
          )
        )
      )

#    model                 formula      AIC
# 1 model1                   y ~ x 11.89136
# 2 model2 y ~ x + I(x^2) + I(x^3) 15.03888

答案 2 :(得分:2)

另一个选项,ldply,但请参阅下面的hadley评论,以便更有效地使用ldply

 # prepare data
    x <- seq(1:10)
    y <- sin(x)^2
    dat <- data.frame(x,y)

# create list of named models obviously these are not suited to the data here, just to make the workflow work...
models <- list(model1=lm(y~x, data = dat), 
               model2=lm(y~I(1/x), data=dat),
               model3=lm(y ~ log(x), data = dat),
               model4=nls(y ~ I(1/x*a) + b*x, data = dat, start = list(a = 1, b = 1)), 
               model5=nls(y ~ (a + b*log(x)), data=dat, start = setNames(coef(lm(y ~ log(x), data=dat)), c("a", "b"))),
               model6=nls(y ~ I(exp(1)^(a + b * x)), data=dat, start = list(a=0,b=0)),
               model7=nls(y ~ I(1/x*a)+b, data=dat, start = list(a=1,b=1))
)

library(plyr)
library(AICcmodavg) # for small sample sizes
# build table with model names, function, AIC and AICc
data.frame(cbind(ldply(models, function(x) cbind(AICc = AICc(x), AIC = AIC(x))), 
                 model = sapply(1:length(models), function(x) deparse(formula(models[[x]])))
                      ))

     .id     AICc      AIC                     model
1 model1 15.89136 11.89136                     y ~ x
2 model2 15.78480 11.78480                y ~ I(1/x)
3 model3 15.80406 11.80406                y ~ log(x)
4 model4 16.62157 12.62157    y ~ I(1/x * a) + b * x
5 model5 15.80406 11.80406      y ~ (a + b * log(x))
6 model6 15.88937 11.88937 y ~ I(exp(1)^(a + b * x))
7 model7 15.78480 11.78480        y ~ I(1/x * a) + b

对于我来说,如何用.id函数中的列名替换ldply,任何提示都不是很明显吗?