如何基于分组的行进行回归并遍历列? [英] How to make regression based on grouped rows and loop over columns?

查看:68
本文介绍了如何基于分组的行进行回归并遍历列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是执行一个回归循环,该循环始终具有相同的预测变量,但会循环响应(此处为y1,y2和y3).问题是我还希望对分组变量的每个类别都执行此操作.在下面的示例数据中,我想对所有三个y变量进行回归y_i = x,这将导致三个回归.但是我希望针对group = a,group = b和group = c分别完成此操作,从而导致9种不同的回归(最好以列表形式存储).无法弄清楚该怎么做!任何对如何执行此操作有想法的人吗?

What I want to do is to perform a regression loop that always has the same predictor but loops over responses (here: y1, y2 and y3). The problem is that I want it also to be done for each category of a grouping variable. In the example data below, I want to make the regression y_i=x for all three y variables, which would result in three regressions. But I want this to be done separately for group=a, group=b and group=c, resulting in 9 different regressions (preferably stored as lists). Cant figure out how to do it! Anyone who has an idea on how to do this?

到目前为止,我的想法是与dplyr :: group_by一起进行for循环或套用,但无法使其正常工作.

My idea so far was to maybe do a for-loop or lapply combined with dplyr::group_by, but can't get it to work.

示例数据(我有一个更大的数据集用于实际分析).

Example data (I have a much larger data set for the actual analysis).

set.seed(123)
dat <- data.frame(group=c(rep("a",10), rep("b",10), rep("c",10)), 
                   x=rnorm(30), y1=rnorm(30), y2=rnorm(30), y3=rnorm(30))

推荐答案

1)在nlme(R随附)中使用 lmList 它).

1) Use lmList in nlme (which comes with R so you don't have to install it).

library(nlme)
regs <- lmList(cbind(y1, y2, y3) ~ x | group, dat)

给出一个 lmList 对象,该对象具有每个组的组件.我们显示组 a 的组件,其他组类似.

giving an lmList object having a component for each group. We show the component for group a and the other groups are similar.

> regs$a

Call:
lm(formula = object, data = dat, na.action = na.action)

Coefficients:
             y1       y2       y3     
(Intercept)   0.2943   0.1395   0.4539
x             0.3721  -0.2206  -0.2255

2)另一种方法是执行一个整体 lm ,从而给出一个具有与上述相同系数的 lm 对象.

2) Another approach is to perform one overall lm giving an lm object having the same coefficients as above.

lm(cbind(y1, y2, y3) ~ group + x:group + 0, dat)

3)我们也可以使用几种列表理解程序包之一.这给出了9个组件的列表.组件名称标识每个主组件中的调用组件(如输出的Call:行中所示)所使用的组合.请注意,当前的CRAN版本是0.1.0,但是下面的代码依赖于listcompr 0.1.1,可以从github获得该代码,直到将其放到CRAN上.

3) We could also use one of several list comprehension packages. This gives a list of 9 components. The names of the components identify the combination used as does the call component (shown in the Call: line of the output) within each main component. Note t hat the current CRAN version is 0.1.0 but the code below relies on listcompr 0.1.1 which can be obtained from github until it is put on CRAN.

# install.github("patrickroocks/listcompr")
library(listcompr)
packageVersion("listcompr") # need version 0.1.1 or later

regs <- gen.named.list("{y}.{g}",
  do.call("lm", 
    list(reformulate("x", y), quote(dat), subset = bquote(dat$group == .(g)))
  ), y = c("y1", "y2", "y3"), g = unique(dat$group)
)

如果您不介意输出中的Call:行的描述性较差,则可以将其简化为:

If you don't mind that the Call: line in the output is less descriptive then it can be simplified to:

gen.named.list("{y}.{g}", lm(reformulate("x", y), dat, subset = group == g),
  y = c("y1", "y2", "y3"), g = unique(dat$group))

注意

从问题中纠正的输入具有两个y2.

Note

The input corrected from question which had two y2's.

set.seed(123)
dat <- data.frame(group=c(rep("a",10), rep("b",10), rep("c",10)), 
                   x=rnorm(30), y1=rnorm(30), y2=rnorm(30), y3=rnorm(30))

这篇关于如何基于分组的行进行回归并遍历列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆