简体   繁体   English

从线性回归输出获取每组的平均得分

[英]Getting mean score for each group from linear regression output

I run the linear regression predicting life satisfaction by sex, race and its interaction. 我进行了线性回归,根据性别,种族及其相互作用来预测生活满意度。

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

Here is an output: 这是输出:

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

I'm required to provide the mean score of life satisfaction for (1) each sex group as well as for (2) each race group (4 in total). 我需要提供(1)每个性别组以及(2)每个种族组(总共4个)的生活满意度平均得分。

So, how can I do it using R? 那么,如何使用R做到这一点? I know that I can just aggregate the data but there is a hint that I can use some coefficients to figure out the mean of satisfaction level for both sex and race groups. 我知道我可以汇总数据,但暗示我可以使用一些系数来计算出性别和种族群体的满意度的平均值。

Thank you very much in advance. 提前非常感谢您。

One quick way of doing it: 一种快速的方法:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM