简体   繁体   English

由线性回归中的每个变量解释的结果方差比例

[英]Proportion of variance of outcome explained by each variable in a linear regression

In the example data set found below I want to calculate the proportion of variance in science explained by each independent variable using linear regression model. 在下面找到的示例数据集中,我想使用线性回归模型计算由每个自变量解释的科学方差比例。 How could I achieve that in R? 我如何在R中实现呢?

hsb2 <- read.table('http://www.ats.ucla.edu/stat/r/modules/hsb2.csv', header=T, sep=",")

m1<-lm(science ~ math+female+ socst+ read, data =hsb2)

One of the ways is to use anova() function from stats package. 一种方法是使用stats包中的anova()函数。 It gives you the residual sum of squares explained by each variable and total sum of squares (ie variance) 它为您提供了每个变量说明的残差平方和和平方和(即方差)

anova(m1)
 Analysis of Variance Table

 Response: science
           Df Sum Sq Mean Sq  F value    Pr(>F)    
math        1 7760.6  7760.6 151.8810 < 2.2e-16 ***
female      1  233.0   233.0   4.5599  0.033977 *  
socst       1  465.6   465.6   9.1128  0.002878 ** 
read        1 1084.5  1084.5  21.2254 7.363e-06 ***
Residuals 195 9963.8    51.1                       
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

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

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