R: 회귀 분석

R에서 회귀분석은 lm()함수를 이용한다. women 샘플 데이터를 이용해서 단순 회귀 분석(simple regression)을 해보자.

> data(women)
> model = lm(weight ~ height, women)

lm(weight ~ height,women)은 종속변수를 weight, 독립변수를 height로 설정한 것이다. 그래프를 그려보자. plot함수는 산점도를 그리고 abline함수는 회귀분석 결과를 가지고 직선을 그려준다.

> plot(weight ~ height, women)
> abline(model)

분석 결과를 보고 싶다면 summary함수를 사용한다.

> summary(model)

Call:
lm(formula = weight ~ height, data = women)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.7333 -1.1333 -0.3833  0.7417  3.1167 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) -87.51667    5.93694  -14.74 1.71e-09 ***
height        3.45000    0.09114   37.85 1.09e-14 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 1.525 on 13 degrees of freedom
Multiple R-Squared: 0.991,      Adjusted R-squared: 0.9903 
F-statistic:  1433 on 1 and 13 DF,  p-value: 1.091e-14 

분산분석표는 anova() 함수를 사용한다. 분산분석을 수행하는 aov 함수와 다르다는 데 유의.

> anova(model)
Analysis of Variance Table

Response: weight
          Df Sum Sq Mean Sq F value    Pr(>F)    
height     1 3332.7  3332.7  1433.0 1.091e-14 ***
Residuals 13   30.2     2.3                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1