R通过lapply命令从乘法回归中提取回归系数

发布于 2024-12-29 05:01:40 字数 1156 浏览 0 评论 0原文

我有一个包含多个变量的大型数据集,其中一个是状态变量,每个状态编码为 1-50。我想对数据集的其余 27 个变量(总共 55 个变量)运行 28 个变量的回归,并且针对每个州。

换句话说,对 covariate1、covariate2、...、covariate27 运行变量 1 的回归,以获取状态 == 1 的观察结果。然后,我想对变量 1 的状态 2-50 重复此操作,并对变量 2、变量 3、...、变量 28 重复整个过程。

我认为我已经编写了正确的 R 代码来执行此操作,但接下来我想做的是提取系数,最好提取到系数矩阵中。有人可以帮我解决这个问题吗?这是我到目前为止编写的代码:

for (num in 1:50) {

    #PUF is the data set I'm using

    #Subset the data by states
    PUFnum <- subset(PUF, state==num)

    #Attach data set with state specific data
    attach(PUFnum)

    #Run our prediction regression
    #the variables class1 through e19700 are the 27 covariates I want to use
    regression <- lapply(PUFnum,  function(z) lm(z ~ class1+class2+class3+class4+class5+class6+class7+
                                                     xtot+e00200+e00300+e00600+e00900+e01000+p04470+e04800+
                                                     e09600+e07180+e07220+e07260+e06500+e10300+
                                                     e59720+e11900+e18425+e18450+e18500+e19700))

    Beta <- lapply(regression, function(d) d<- coef(regression$d))


    detach(PUFnum)
}

I have a large dataset with several variables, one of which is a state variable, coded 1-50 for each state. I'd like to run a regression of 28 variables on the remaining 27 variables of the dataset (there are 55 variables total), and specific for each state.

In other words, run a regression of variable1 on covariate1, covariate2, ..., covariate27 for observations where state==1. I'd then like to repeat this for variable1 for states 2-50, and the repeat the whole process for variable2, variable3,..., variable28.

I think I've written the correct R code to do this, but the next thing I'd like to do is extract the coefficients, ideally into a coefficient matrix. Could someone please help me with this? Here's the code I've written so far:

for (num in 1:50) {

    #PUF is the data set I'm using

    #Subset the data by states
    PUFnum <- subset(PUF, state==num)

    #Attach data set with state specific data
    attach(PUFnum)

    #Run our prediction regression
    #the variables class1 through e19700 are the 27 covariates I want to use
    regression <- lapply(PUFnum,  function(z) lm(z ~ class1+class2+class3+class4+class5+class6+class7+
                                                     xtot+e00200+e00300+e00600+e00900+e01000+p04470+e04800+
                                                     e09600+e07180+e07220+e07260+e06500+e10300+
                                                     e59720+e11900+e18425+e18450+e18500+e19700))

    Beta <- lapply(regression, function(d) d<- coef(regression$d))


    detach(PUFnum)
}

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

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

发布评论

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

评论(2

以可爱出名 2025-01-05 05:01:40

这是经典 Split-Apply-Combine 问题的另一个示例,可以使用 @hadley 的 plyr 包来解决。在您的问题中,您希望

  1. 按状态拆分数据框
  2. 对每个子集应用回归
  3. 将系数组合到数据框中。

我将使用 MASS 库中提供的 Cars93 数据集来说明它。我们有兴趣根据国家/地区的原产地找出马力引擎大小之间的关系。

# LOAD LIBRARIES
require(MASS); require(plyr)

# SPLIT-APPLY-COMBINE
regressions <- dlply(Cars93, .(Origin), lm, formula = Horsepower ~ EngineSize)
coefs <- ldply(regressions, coef)

   Origin (Intercept) EngineSize
1     USA    33.13666   37.29919
2 non-USA    15.68747   55.39211

编辑。对于您的示例,将 PUF 替换为 Cars93,将 state 替换为 Origin,将 fm 替换为公式

This is another example of the classic Split-Apply-Combine problem, which can be addressed using the plyr package by @hadley. In your problem, you want to

  1. Split data frame by state
  2. Apply regressions for each subset
  3. Combine coefficients into data frame.

I will illustrate it with the Cars93 dataset available in MASS library. We are interested in figuring out the relationship between horsepower and enginesize based on origin of country.

# LOAD LIBRARIES
require(MASS); require(plyr)

# SPLIT-APPLY-COMBINE
regressions <- dlply(Cars93, .(Origin), lm, formula = Horsepower ~ EngineSize)
coefs <- ldply(regressions, coef)

   Origin (Intercept) EngineSize
1     USA    33.13666   37.29919
2 non-USA    15.68747   55.39211

EDIT. For your example, substitute PUF for Cars93, state for Origin and fm for the formula

美煞众生 2025-01-05 05:01:40

我已经稍微清理了你的代码:

fm <- z ~ class1+class2+class3+class4+class5+class6+class7+
          xtot+e00200+e00300+e00600+e00900+e01000+p04470+e04800+
          e09600+e07180+e07220+e07260+e06500+e10300+
          e59720+e11900+e18425+e18450+e18500+e19700

PUFsplit <- split(PUF, PUF$state)
mod <- lapply(PUFsplit, function(z) lm(fm, data=z))

Beta <- sapply(mod, coef)

如果你愿意,你甚至可以将这一切放在一行中:

Beta <- sapply(lapply(split(PUF, PUF$state), function(z) lm(fm, data=z)), coef)

I've cleaned up your code slightly:

fm <- z ~ class1+class2+class3+class4+class5+class6+class7+
          xtot+e00200+e00300+e00600+e00900+e01000+p04470+e04800+
          e09600+e07180+e07220+e07260+e06500+e10300+
          e59720+e11900+e18425+e18450+e18500+e19700

PUFsplit <- split(PUF, PUF$state)
mod <- lapply(PUFsplit, function(z) lm(fm, data=z))

Beta <- sapply(mod, coef)

If you wanted, you could even put this all in one line:

Beta <- sapply(lapply(split(PUF, PUF$state), function(z) lm(fm, data=z)), coef)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文