【问题标题】:Getting mean score for each group from linear regression output从线性回归输出中获取每组的平均分数
【发布时间】:2015-12-12 16:10:01
【问题描述】:

我运行线性回归,按性别、种族及其相互作用预测生活满意度。

lm2 <-lm(nids$satisfaction~nids$male+nids$race+nids$male:nids$race)

这是一个输出:

Call:
lm(formula = nids$satisfaction ~ nids$male + nids$race + nids$male:nids$race)

Residuals:
    Min      1Q  Median      3Q     Max 
-6.6613 -1.3366 -0.0485  1.7378  4.9515 

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)    
(Intercept)          4.17751    0.05467  76.410  < 2e-16 ***
nids$male            0.39318    0.08564   4.591 4.45e-06 ***
nids$race            0.87095    0.03421  25.459  < 2e-16 ***
nids$male:nids$race -0.17947    0.05261  -3.411 0.000649 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.358 on 12016 degrees of freedom
Multiple R-squared:  0.07414,   Adjusted R-squared:  0.07391 
F-statistic: 320.7 on 3 and 12016 DF,  p-value: < 2.2e-16

我需要提供 (1) 每个性别组以及 (2) 每个种族组(总共 4 个)的生活满意度平均分。

那么,我如何使用 R 来做到这一点?我知道我可以汇总数据,但有一个提示,我可以使用一些系数来计算性别和种族群体的满意度平均值。

非常感谢您。

【问题讨论】:

  • 请提供一个可重现的例子。在 R 中实现了许多数据集。
  • 你的意思是上传数据集吗?
  • 欢迎来到 StackOverflow。 reproducible example 会很有帮助
  • 最简单的方法是从您的数据中提取并使用内置集合:help(pack="datasets")。
  • 更新了我的帖子,其中包含指向数据和 .r 文件的链接。

标签: r statistics linear-regression


【解决方案1】:

一种快速的方法:

malenids <- nids[nids$male == 1, ]
femalenids <- nids[nids$male == 0, ]
lapply(split(malenids, malenids$race), function(x) mean(x$satisfaction))
lapply(split(femalenids, femalenids$race), function(x) mean(x$satisfaction))

【讨论】:

  • 是的,但问题是如何只使用这个线性回归输出:(,它背后有什么原则吗?
  • 好吧,误解了。这背后的原因是什么?你为什么不直接生成你的预测,然后像上面那样在样本中聚合或计算它?
  • 但是“生成预测”是什么意思?
  • 使用predict(lm2) 或fitted(lm2),因为您没有提供任何新数据。
  • @dmg 查看fit &lt;- lm(weight ~ group, PlantGrowth); c(coefficients(fit)[1], coefficients(fit)[1] + coefficients(fit)[-1]); aggregate(weight ~ group, PlantGrowth, mean) 或fit &lt;- lm(mpg ~ as.factor(cyl), mtcars); c(coefficients(fit)[1], coefficients(fit)[1] + coefficients(fit)[-1]); aggregate(mpg ~ as.factor(cyl), mtcars, mean)。
猜你喜欢
  • 2020-07-07
  • 2019-01-14
  • 1970-01-01
  • 2018-12-11
  • 2021-02-20
  • 1970-01-01
  • 2016-03-04
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多