r滤波器和汇总结果来自lapply模型摘要

发布于 2025-01-29 16:43:30 字数 481 浏览 4 评论 0原文

我正在尝试使用dlply在数据集的子集上执行的多个回归模型过滤和汇总结果。

这就是我的模型的方式:

library(plyr)

data("mtcars")

models = dlply(mtcars, .(cyl), function(df) lm(mpg ~ hp,data=df))
lapply(models, summary)

现在,我将类似不同模型的结果(圆柱体4、6、8)结合在一起:

rbind(
  c("Cylinder 4", coef(lapply(models, summary)$`4`)[2,]),
  c("Cylinder 6", coef(lapply(models, summary)$`6`)[2,]),
  c("Cylinder 8", coef(lapply(models, summary)$`8`)[2,])
)

是否有一种方法可以更有效地总结这一点?

I am trying to filter and aggregate results from multiple regression models executed on a subset of dataset using dlply.

This is how I ran my models:

library(plyr)

data("mtcars")

models = dlply(mtcars, .(cyl), function(df) lm(mpg ~ hp,data=df))
lapply(models, summary)

Right now I am combining the results from different models(cylinder 4, 6, 8) like this:

rbind(
  c("Cylinder 4", coef(lapply(models, summary)

I am trying to filter and aggregate results from multiple regression models executed on a subset of dataset using dlply.

This is how I ran my models:

library(plyr)

data("mtcars")

models = dlply(mtcars, .(cyl), function(df) lm(mpg ~ hp,data=df))
lapply(models, summary)

Right now I am combining the results from different models(cylinder 4, 6, 8) like this:

4`)[2,]), c("Cylinder 6", coef(lapply(models, summary)

I am trying to filter and aggregate results from multiple regression models executed on a subset of dataset using dlply.

This is how I ran my models:

library(plyr)

data("mtcars")

models = dlply(mtcars, .(cyl), function(df) lm(mpg ~ hp,data=df))
lapply(models, summary)

Right now I am combining the results from different models(cylinder 4, 6, 8) like this:

6`)[2,]), c("Cylinder 8", coef(lapply(models, summary)

I am trying to filter and aggregate results from multiple regression models executed on a subset of dataset using dlply.

This is how I ran my models:

library(plyr)

data("mtcars")

models = dlply(mtcars, .(cyl), function(df) lm(mpg ~ hp,data=df))
lapply(models, summary)

Right now I am combining the results from different models(cylinder 4, 6, 8) like this:

8`)[2,]) )

Is there a way to summarize this more efficiently?

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

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

发布评论

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

评论(1

抱猫软卧 2025-02-05 16:43:30

我们可以从broom中使用整理,而不是使用摘要coef。我们还可以将模型数据直接输送到map2_df

library(tidyverse)

dlply(mtcars, .(cyl), function(df)
  lm(mpg ~ hp, data = df)) %>%
  map2_df(
    .,
    names(.),
    ~ tidy(.x)[2,] %>% mutate(Cylinder = paste0("Cylinder ", .y)) %>% tibble::column_to_rownames("Cylinder")
  )

输出

  term  estimate std.error statistic p.value Cylinder
  <chr>    <dbl>     <dbl>     <dbl>   <dbl> <chr>   
1 hp    -0.113      0.0612    -1.84   0.0984 4       
2 hp    -0.00761    0.0266    -0.286  0.786  6       
3 hp    -0.0142     0.0139    -1.02   0.326  8   

We can use tidy from broom, rather than using summary and coef. We can also just pipe the model data straight into map2_df.

library(tidyverse)

dlply(mtcars, .(cyl), function(df)
  lm(mpg ~ hp, data = df)) %>%
  map2_df(
    .,
    names(.),
    ~ tidy(.x)[2,] %>% mutate(Cylinder = paste0("Cylinder ", .y)) %>% tibble::column_to_rownames("Cylinder")
  )

Output

  term  estimate std.error statistic p.value Cylinder
  <chr>    <dbl>     <dbl>     <dbl>   <dbl> <chr>   
1 hp    -0.113      0.0612    -1.84   0.0984 4       
2 hp    -0.00761    0.0266    -0.286  0.786  6       
3 hp    -0.0142     0.0139    -1.02   0.326  8   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文