獨斷論

R과 python: 단순회귀분석 본문

과학과 기술/R 통계

R과 python: 단순회귀분석

부르칸 2021. 3. 22. 08:18

R의 lm( )과 python의 statsmodel을 이용하여 단순회귀분석을 비교해보도록 한다.

R의 lm( )

독립변수 x와 종속변수 y를 console에서 직접입력한다. 총 50개의 sample size이다.

x = c(2.655,3.721,5.729,9.082,2.017,8.984,9.447,6.608,6.291,0.618,
      2.06,1.766,6.87,3.841,7.698,4.977,7.176,9.919,3.8,7.774,
      9.347,2.121,6.517,1.256,2.672,3.861,0.134,3.824,8.697,3.403,
      4.821,5.996,4.935,1.862,8.274,6.685,7.942,1.079,7.237,4.113,
      8.209,6.471,7.829,5.53,5.297,7.894,0.233,4.772,7.323,6.927,
      4.776)
y = c(27.544,20.7,23.422,44.979,20.042,41.499,44.606,29.852,35.507,5.874,
      26.234,18.788,40.352,23.029,46.569,30.235,33.185,45.356,18.5,32.805,
      38.391,27.133,36.901,19.336,23.473,22.589,32.212,20.41,42.416,24.726,
      30.063,29.921,16.188,14.722,42.33,33.35,34.221,19.77,31.638,26.286,
      33.093,34.39,39.632,29.502,28.514,39.979,16.771,26.237,39.409,39.901,
      27.562)

x와 y를 데이터프레임으로 저장한다.

df_xy = data.frame(x, y)
plot(y ~ x, data = df_xy)

lm( )을 이용하여 단순회귀분석을 실행하고 회귀분석결과를 그래프로 그려본다.

xy_lmfit = lm(y ~ x, data = df_xy)
abline(xy_lmfit)
summary(xy_lmfit)

> summary(xy_lmfit)

Call:
lm(formula = y ~ x, data = df_xy)

Residuals:
     Min       1Q   Median       3Q      Max 
-12.7109  -3.2562   0.2766   2.5969  17.2568 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  14.5660     1.5794   9.222 2.76e-12 ***
x             2.9043     0.2655  10.937 9.47e-15 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 5.063 on 49 degrees of freedom
Multiple R-squared:  0.7094,    Adjusted R-squared:  0.7035 
F-statistic: 119.6 on 1 and 49 DF,  p-value: 9.469e-15

Python의 statsmodels

필요한 라이브러리를 불러들인다.

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt

x와 y 변수를 벡터로 입력한다.

x = np.array([2.655,3.721,5.729,9.082,2.017,8.984,9.447,6.608,6.291,0.618,
      2.06,1.766,6.87,3.841,7.698,4.977,7.176,9.919,3.8,7.774,
      9.347,2.121,6.517,1.256,2.672,3.861,0.134,3.824,8.697,3.403,
      4.821,5.996,4.935,1.862,8.274,6.685,7.942,1.079,7.237,4.113,
      8.209,6.471,7.829,5.53,5.297,7.894,0.233,4.772,7.323,6.927,
      4.776])
y = np.array([27.544,20.7,23.422,44.979,20.042,41.499,44.606,29.852,35.507,5.874,
      26.234,18.788,40.352,23.029,46.569,30.235,33.185,45.356,18.5,32.805,
      38.391,27.133,36.901,19.336,23.473,22.589,32.212,20.41,42.416,24.726,
      30.063,29.921,16.188,14.722,42.33,33.35,34.221,19.77,31.638,26.286,
      33.093,34.39,39.632,29.502,28.514,39.979,16.771,26.237,39.409,39.901,
      27.562])

xy_df라는 데이터프레임을 만들고 위 x와 y벡터를 각 열로 넣는다.

xy_df = pd.DataFrame({'x': x, 'y': y})

statsmodels의 ols로 단순회귀분석을 실행한다.

model_ols = smf.ols('y ~ x', data = xy_df)
fit_ols = model_ols.fit()

결과를 출력한다.

print(fit_ols.summary())

결과가 좀 길어서 coefficients 부분만 보여준다.

==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     14.5660      1.579      9.222      0.000      11.392      17.740
x              2.9043      0.266     10.937      0.000       2.371       3.438
==============================================================================

그래프로도 한번 그려보자.

fig, ax = plt.subplots()
ax.plot(xy_df['x'], xy_df['y'], 'o')
ax.plot(xy_df['x'], fit_ols.fittedvalues)

 

Comments