按因子值将数据帧分成子集,发送到返回 glm 类的函数,如何重新组合?

发布于 2024-09-04 07:28:31 字数 444 浏览 3 评论 0原文

感谢 Hadley 的 plyr 包 ddply 函数,我们可以获取一个数据帧,按因子将其分解为子数据帧,将每个子数据帧发送到一个函数,然后将每个子数据帧的函数结果组合成一个新的数据帧。

但是,如果该函数返回像 glm 这样的类的对象,或者在我的例子中,ac(“glm”,“lm”)。那么,这些不能组合成一个数据框,不是吗?我收到此错误是否

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class 'c("glm", "lm")' into a data.frame

有一些更灵活的数据结构可以容纳我的函数调用的所有复杂的 glm 类结果,保留有关数据帧子集的信息?

或者应该以完全不同的方式来完成?

Thanks to Hadley's plyr package ddply function we can take a dataframe, break it down into subdataframes by factors, send each to a function, and then combine the function results for each subdataframe into a new dataframe.

But what if the function returns an object of a class like glm or in my case, a c("glm", "lm"). Then, these can't be combined into a dataframe can they? I get this error instead

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class 'c("glm", "lm")' into a data.frame

Is there some more flexible data structure that will accommodate all the complex glm class results of my function calls, preserving the information regarding the dataframe subsets?

Or should this be done in an entirely different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2024-09-11 07:28:31

只是为了扩展我的评论:plyr 有一组组合输入和输出类型的函数。因此,当您的函数返回无法转换为 data.frame 的内容时,您应该使用 list 作为输出。因此,不要使用 ddply,而是使用 dlply

当您想要对每个模型执行某些操作并将结果转换为 data.frame 时,ldply 就是关键。

让我们使用 dlply 创建一些模型,

list_of_models <- dlply(warpbreaks, .(tension), function(X) lm(breaks~wool, data=X))
str(list_of_models, 1)
# List of 3
#  $ L:List of 13
#   ..- attr(*, "class")= chr "lm"
#  $ M:List of 13
#   ..- attr(*, "class")= chr "lm"
#  $ H:List of 13
#   ..- attr(*, "class")= chr "lm"
#  - attr(*, "split_type")= chr "data.frame"
#  - attr(*, "split_labels")='data.frame':        3 obs. of  1 variable:

它提供了三个 lm 模型的列表。

使用ldply,您可以创建一个data.frame,例如

  • 包含每个模型的预测:

    ldply(list_of_models, 函数(模型) {
        data.frame(fit=predict(模型,warpbreaks))
    })
    # 张力贴合
    # 1 升 44.5556
    # 2 升 44.5556
    # 3 升 44.5556
    
  • 每个模型的统计数据:

    ldply(list_of_models, 函数(模型) {
      c(
        aic = extractAIC(模型),
        偏差 = 偏差(模型),
        logLik = logLik(模型),
        限制 = 限制(模型),
        系数 = 系数(模型)
      )
    })
    # 张力 aic1 aic2 偏差 logLik confint1 confint2 confint3 confint4 coef.(截距) coef.woolB
    # 1 升 2 98.3291 3397.78 -72.7054 34.2580 -30.89623 54.8531 -1.77044 44.5556 -16.33333
    # 2米2 81.1948 1311.56 -64.1383 17.6022 -4.27003 30.3978 13.82559 24.0000 4.77778
    # 3 H 2 76.9457 1035.78 -62.0137 18.8701 -13.81829 30.2411 2.26273 24.5556 -5.77778
    

Just to expand my comment: plyr has set of functions to combine input and output type. So when you function returns something inconvertible to data.frame you should use list as output. So instead of using ddply use dlply.

When you want to do something on each model and convert results to data.frame then ldply is the key.

Lets create some models using dlply

list_of_models <- dlply(warpbreaks, .(tension), function(X) lm(breaks~wool, data=X))
str(list_of_models, 1)
# List of 3
#  $ L:List of 13
#   ..- attr(*, "class")= chr "lm"
#  $ M:List of 13
#   ..- attr(*, "class")= chr "lm"
#  $ H:List of 13
#   ..- attr(*, "class")= chr "lm"
#  - attr(*, "split_type")= chr "data.frame"
#  - attr(*, "split_labels")='data.frame':        3 obs. of  1 variable:

It gives list of three lm models.

Using ldply you could create a data.frame, e.g.

  • with predictions of each model:

    ldply(list_of_models, function(model) {
        data.frame(fit=predict(model, warpbreaks))
    })
    #     tension     fit
    # 1         L 44.5556
    # 2         L 44.5556
    # 3         L 44.5556
    
  • with statistics to each model:

    ldply(list_of_models, function(model) {
      c(
        aic = extractAIC(model),
        deviance = deviance(model),
        logLik = logLik(model),
        confint = confint(model),
        coef = coef(model)
      )
    })
    # tension aic1    aic2 deviance   logLik confint1  confint2 confint3 confint4 coef.(Intercept) coef.woolB
    # 1       L    2 98.3291  3397.78 -72.7054  34.2580 -30.89623  54.8531 -1.77044          44.5556  -16.33333
    # 2       M    2 81.1948  1311.56 -64.1383  17.6022  -4.27003  30.3978 13.82559          24.0000    4.77778
    # 3       H    2 76.9457  1035.78 -62.0137  18.8701 -13.81829  30.2411  2.26273          24.5556   -5.77778
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文