Fitting Statistical Models to Data with Python - WEEK 2 - Part 1
WEEK 2 - FITTING MODELS TO INDEPENDENT DATA - PART 1
- 2์ฃผ์ฐจ์๋ linear regression๊ณผ logistic regression์ ๋ฐฐ์ด๋ค. ๊ทธ๋ฆฌ๊ณ , ์ด ๋ชจ๋ธ์ ์ ํ๋๋ฅผ ์ด๋ป๊ฒ ์ธก์ ํ ์ ์๋์ง์ ๋ํด์ ํ๊ฐํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด์ํ๊ธฐ๋ ํ๋ค.
Linear regression modelling: one ind var.
- ์์ฃผ ๊ฐ๋จํ ๋ชจํ์ธ, linear regression์ ๋ชจ๋ธ๋งํฉ๋๋ค. ๊ฐ ๋ณ์์ ๋ํ ์ค๋ช
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- Y:
BPXSY1: ํผ์คํ์์ ๋ํด์ 1๋ฒ์งธ๋ก ์ธก์ ๋ Bloods pressure - X:
RIDAGEYR: ํผ์คํ์์ ๋์ด.
- Y:
- ์ฆ, ์ฌ๊ธฐ์๋, โํผ์คํ์์ ๋์ดโ๋ผ๋ ๋ณ์๊ฐ, โํ์โ์ ์ํฅ์ ๋ฏธ์น ๊ฒ์ด๋ค, ๋ผ๋ ๊ฒ์ ๊ฐ์ ํ๊ณ ๋ชจ๋ธ์ ์ธ์ด ๊ฒ์ด์ฃ . (๋ํ, ์ง๊ธ์ ์ค์ํ์ง ์์ง๋ง, BP๊ฐ โ1๋ฒ์งธ ์ธก์ ๋โ ์ด๋ผ๋ ๋ง์ ์ ์ํ์ธ์).
- ์๋์ ๊ฐ์ด, ๊ฐ๋จํ ๋ชจ๋ธ์ ์ธ์ฐ๊ณ dataframe๊ณผ formula๋ฅผ ํจ๊ป ๋๊ฒจ์ค๋๋ค.
# Read and Process data
# file link
# https://github.com/kshedden/statswpy-nhanes/blob/master/merged/nhanes_2015_2016.csv
nhanes_df = pd.read_csv("nhanes_2015_2016.csv")
print(nhanes_df.columns)
# ํ์ํ ์นผ๋ผ๋ง ์ ํํ๊ณ , ํ์์๋ ๊ฒ๋ค ์ญ์
vars = ["BPXSY1", "RIDAGEYR", "RIAGENDR", "RIDRETH1", "DMDEDUC2", "BMXBMI", "SMQ020"]
nhanes_df = nhanes_df[vars].dropna()
print("== csv file processed")
OLS = True
if OLS==True:
print("== OLS summary")
model = sm.OLS.from_formula("BPXSY1 ~ RIDAGEYR", data=nhanes_df)
result = model.fit()
print(result.summary())
print("== OLS done")
- ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
== csv file processed
== OLS summary
OLS Regression Results
==============================================================================
Dep. Variable: BPXSY1 R-squared: 0.207
Model: OLS Adj. R-squared: 0.207
Method: Least Squares F-statistic: 1333.
Date: Thu, 16 Jan 2020 Prob (F-statistic): 2.09e-259
Time: 16:32:25 Log-Likelihood: -21530.
No. Observations: 5102 AIC: 4.306e+04
Df Residuals: 5100 BIC: 4.308e+04
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 102.0935 0.685 149.120 0.000 100.751 103.436
RIDAGEYR 0.4759 0.013 36.504 0.000 0.450 0.501
==============================================================================
Omnibus: 690.261 Durbin-Watson: 2.039
Prob(Omnibus): 0.000 Jarque-Bera (JB): 1505.999
Skew: 0.810 Prob(JB): 0.00
Kurtosis: 5.112 Cond. No. 156.
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
== OLS done
coefficient
coef: X๋ผ๋ ๋ณ์๊ฐ Y๋ผ๋ ๋ณ์์ ์ผ๋ง๋ ๋ฏผ๊ฐํ๊ฒ ์ํฅ์ ์ฃผ๋๊ฐ?์ ๋ํ ๊ฒ์ ๋งํ์ฃ . ์์ ๊ธฐ์ธ๊ธฐ, ์ ๊ฐ์ ๊ฒ์ด๊ณ , ๋๋ต์ ์ธ ๊ตฌ๊ฐ๋ ์ ์ ์์ต๋๋ค.RIDAGEYR๋ 0.4759์ coef๋ฅผ ๊ฐ์ง์ฃ . ์ฆ, 1๋ ์BPXSY1์ 0.48๋งํผ์ ๋ณํ๋ฅผ ๊ฐ์ ธ์จ๋ค, ๋ผ๊ณ ํ ์ ์์ฃ . ๊ทธ๋ฆฌ๊ณ p-value ๋ํ, ์ถฉ๋ถํ ์๊ธฐ ๋๋ฌธ์, ๋งค์ฐ ์ ํจํ๋ค๊ณ ํ ์ ์์ต๋๋ค.
R-squared and correlation
- ํํ, ๋
๋ฆฝ๋ณ์(independent var)๊ฐ ์ข
์๋ณ์(dependent var)์ ๋ถ์ฐ์ ์ผ๋ง๋ ์ ์ค๋ช
ํ๋์ง, ์ด๋ฅผ ์ค๋ช
ํ๊ธฐ ์ํ ๊ฐ์ผ๋ก,
R-squared๊ฐ ์ฌ์ฉ๋ฉ๋๋ค(๋ฌผ๋ก , ํ์ฌ ๋ฐฉ์ ์์์๋ 1๊ฐ์ ๋ ๋ฆฝ๋ณ์์ 1๊ฐ์ ์ข ์ ๋ณ์๊ฐ ์ฌ์ฉ๋์ด, correlation๊ณผ ๋ณ ์ฐจ์ด๊ฐ ์์ต๋๋ค๋ง) - ์ ํ์์ ๋ณด๋ ๊ฐ์ 0.207์ด๋ฉฐ, ์ด๋, ์ข ์ ๋ณ์์ ๋ณํ์ 21%๊ฐ ๋ ๋ฆฝ๋ณ์์ ์ํด์ ๊ฒฐ์ ๋๋ค๋ ์ด์ผ๊ธฐ์ฃ .
Linear regression modelling: two ind var.
- ์ฌ์ค, 1๊ฐ์ ๋ ๋ฆฝ๋ณ์๋ก ์ข ์ ๋ณ์๋ฅผ ์์ธกํ๋ ๊ฒฝ์ฐ๋ ์ ์์ฃ . ๊ทธ๋์, ์ฌ๊ธฐ์๋ ํ๋์ ์ข ์ ๋ณ์๋ฅผ ๋ ์ถ๊ฐํด๋ด ๋๋ค. ์๋ง๋, 2๊ฐ์ ์ข ์ ๋ณ์๋ฅผ ์ฌ์ฉํด์ ์์ธกํ๋ค๋ฉด, ๋ณด๋ค ์ ํํ๊ฒ ๋์ค์ง ์์๊น(์ฆ R-Square ๊ฐ์ด ์ปค์ง์ง ์์๊น?)๋ผ๊ณ ์์ํด๋ด ๋๋ค.
- ๋ณธ ์ฝ์ค์์๋
RIAGENDR๋ผ๋, ์ฑ๋ณ์ ์๋ฏธํ๋ categorical value๋ฅผ ์ง์ด๋ฃ์ต๋๋ค. ๋ณดํต, ๋ค๋ฅธ ๋จธ์ ๋ฌ๋ ํจํค์ง์์๋ ์ด ๊ฐ์, numerical value๋ก ๋ฐ๊พธ๋๋ฐ, ์ฌ๊ธฐ์๋ ๊ทธ๋ฅ โMaleโ, โFemaleโ์ธ ๋ฌธ์์ด๋ก ๋ฐ๊พธ์ด ์ง์ด๋ฃ์ต๋๋ค. ์์์ ์ ํด์ํ๋๋ด ๋๋ค.
nhanes_df = pd.read_csv("nhanes_2015_2016.csv")
#print(nhanes_df.columns)
# ํ์ํ ์นผ๋ผ๋ง ์ ํํ๊ณ , ํ์์๋ ๊ฒ๋ค ์ญ์
vars = ["BPXSY1", "RIDAGEYR", "RIAGENDR", "RIDRETH1", "DMDEDUC2", "BMXBMI", "SMQ020"]
nhanes_df = nhanes_df[vars].dropna()
nhanes_df["RIAGENDRx"] = nhanes_df.RIAGENDR.replace({1: "Male", 2: "Female"})
print("== csv file processed")
print("== OLS summary")
model = sm.OLS.from_formula("BPXSY1 ~ RIDAGEYR + RIAGENDRx", data=nhanes_df)
result = model.fit()
print(result.summary())
print("== OLS done")
== csv file processed
== OLS summary
OLS Regression Results
==============================================================================
Dep. Variable: BPXSY1 R-squared: 0.215
Model: OLS Adj. R-squared: 0.214
Method: Least Squares F-statistic: 697.4
Date: Thu, 16 Jan 2020 Prob (F-statistic): 1.87e-268
Time: 16:48:59 Log-Likelihood: -21505.
No. Observations: 5102 AIC: 4.302e+04
Df Residuals: 5099 BIC: 4.304e+04
Df Model: 2
Covariance Type: nonrobust
=====================================================================================
coef std err t P>|t| [0.025 0.975]
-------------------------------------------------------------------------------------
Intercept 100.6305 0.712 141.257 0.000 99.234 102.027
RIAGENDRx[T.Male] 3.2322 0.459 7.040 0.000 2.332 4.132
RIDAGEYR 0.4739 0.013 36.518 0.000 0.448 0.499
==============================================================================
Omnibus: 706.732 Durbin-Watson: 2.036
Prob(Omnibus): 0.000 Jarque-Bera (JB): 1582.730
Skew: 0.818 Prob(JB): 0.00
Kurtosis: 5.184 Cond. No. 168.
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
== OLS done
-
์ด์ ์ด ๋ชจ๋ธ์ โ๋์ดโ์ โ์ฑ๋ณโ์ ์ฌ์ฉํ์ฌ BP์ ๋ณํ ์ ๋๋ฅผ ์ค๋ช ํ ์ ์์ต๋๋ค.
coef์ ๋ฐ๋ฅด๋ฉด, 1๋ ์ด ์ถ๊ฐ๋ ์๋ก 0.47์ ํ์์ด ๋์์ง๊ณ , ์ฑ๋ณ์ Male์ ๊ฒฝ์ฐ 3.23์ด ๋ ๋๋ค๊ณ ํ์๋ฉ๋๋ค. ๋ํ, ๋์ด์ ๊ฒฝ์ฐ๋ ์๋ก์ด ๋ณ์์ธ โ์ฑ๋ณโ์ด ์ถ๊ฐ๋๊ธฐ ์ ๊ณผ ์ดํ์coef์ ๋ณํ๊ฐ ๊ฑฐ์ ์์ต๋๋ค. ์ด๋ ๋ณดํต, ์ด ๋๊ฐ์ ์๊ด๊ด๊ณ๊ฐ ์์์ ์๋ฏธํ์ฃ .
-
categorical variable์ ๋ฆฌ๊ทธ๋ ์ ๋ชจ๋ธ์์ covariate(๊ณต๋ณ๋)์ผ๋ก ์ฌ์ฉ๋๋ฉฐ,
reference level์ ๊ทธ category๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ ํ๋ค๋ ๋ง์ด๋ค. ๋ง์น, ์ด์ ์ โMale์ ๊ฒฝ์ฐ 3.23์ด ๋ ๋์์ง๋๋ก ์ค๊ณ๋ ๊ฒ์ Female์ด reference level์ด๊ธฐ ๋๋ฌธโ์ด๋ค. ๋น์ฐํ์ง๋ง, ๋ง์ฝ โmale์ reference level๋ก ์ก๋๋ค๋ฉด, ํด๋น coefficient๋ -3.23์ด ๋ ๊ฒ์ด๋คโ.
- categorical variable์ ๋ณดํต dummy variable๋ก ๋ณํ๋๋ค. ์ฆ, category๊ฐ 3๊ฐ๋ผ๋ฉด, 3๊ฐ์ ์นผ๋ผ์ ๋ง๋ค์ด์ ๊ฐ ์นผ๋ผ์ True/Fale๋ฅผ ๋งํด์ฃผ๋ ํํ๋ฅผ ๋งํ๋ค. ์ฆ,
statsmodels์์๋ ๋์ผํ๋ฉฐ, ์ฌ์ฑ์ reference level๋ก ์ก๊ณ , ๋จ์ฑ์ด๋ผ๋ ์นผ๋ผ์ ๋ง๋ค์ด์, 0 or 1๋ก ์ธํ ํด์ฃผ๋ ๊ฒ์ ๋งํ๋ค.
Linear regression modelling: Three ind var.
- ์ด๋ฒ์๋
BMXBMI, BMI๋ผ๋ ์นผ๋ผ์ ์ถ๊ฐํ์์ผ๋ฉฐ, ๋น์ฐํ๋R_square๊ฐ์ด ์ํญ ์ํฅ๋์๊ณ ,coef๋ ์ ํจํ๋ค.
# Read and Process data
# file link
# https://github.com/kshedden/statswpy-nhanes/blob/master/merged/nhanes_2015_2016.csv
nhanes_df = pd.read_csv("nhanes_2015_2016.csv")
#print(nhanes_df.columns)
# ํ์ํ ์นผ๋ผ๋ง ์ ํํ๊ณ , ํ์์๋ ๊ฒ๋ค ์ญ์
vars = ["BPXSY1", "RIDAGEYR", "RIAGENDR", "RIDRETH1", "DMDEDUC2", "BMXBMI", "SMQ020"]
nhanes_df = nhanes_df[vars].dropna()
nhanes_df["RIAGENDRx"] = nhanes_df.RIAGENDR.replace({1: "Male", 2: "Female"})
print("== csv file processed")
print("== OLS summary")
model = sm.OLS.from_formula("BPXSY1 ~ RIDAGEYR + BMXBMI + RIAGENDRx", data=nhanes_df)
result = model.fit()
print(result.summary())
print("== OLS done")
== csv file processed
== OLS summary
OLS Regression Results
==============================================================================
Dep. Variable: BPXSY1 R-squared: 0.228
Model: OLS Adj. R-squared: 0.228
Method: Least Squares F-statistic: 502.0
Date: Thu, 16 Jan 2020 Prob (F-statistic): 8.54e-286
Time: 17:03:52 Log-Likelihood: -21461.
No. Observations: 5102 AIC: 4.293e+04
Df Residuals: 5098 BIC: 4.296e+04
Df Model: 3
Covariance Type: nonrobust
=====================================================================================
coef std err t P>|t| [0.025 0.975]
-------------------------------------------------------------------------------------
Intercept 91.5840 1.198 76.456 0.000 89.236 93.932
RIAGENDRx[T.Male] 3.5783 0.457 7.833 0.000 2.683 4.474
RIDAGEYR 0.4709 0.013 36.582 0.000 0.446 0.496
BMXBMI 0.3060 0.033 9.351 0.000 0.242 0.370
==============================================================================
Omnibus: 752.325 Durbin-Watson: 2.040
Prob(Omnibus): 0.000 Jarque-Bera (JB): 1776.087
Skew: 0.847 Prob(JB): 0.00
Kurtosis: 5.343 Cond. No. 316.
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
== OLS done
wrap-up
statsmodels์์ ๋น๊ต์ ๊ฐ๋จํ linear regression model์ ๊ตฌํํ์๋ค. ๊ธฐ๋ณธ์ ์ธ Ordinary Least Square method๋ฅผ ์ฌ์ฉํ์ฌ, ์์ธก์น์ ์ค์ ์น์ ์ฐจ์ด๋ฅผ ์ต์ํํ๋ parameter๋ฅผ ์ถ์ ํ๋ ์์ผ๋ก ์งํ๋์๋ค.- ํนํ, ๋ ๋ฆฝ๋ณ์๊ฐ 1๊ฐ์ผ ๋์, 2๊ฐ, 3๊ฐ ์ผ๋๋ก ๋์ด๋๋ ๊ฒ์ ํตํด, R-squared ๊ฐ์ด ์ด๋ป๊ฒ ๋ฌ๋ผ์ง๋์ง, ๊ทธ๋ฆฌ๊ณ coef๊ฐ์ด ๋ฌด์จ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ฉฐ, ๋ณ์๊ฐ ์ถ๊ฐ๋๋ฉด์ coef๊ฐ ์ค์ด๋ค๊ฑฐ๋ ํ ๊ฒฝ์ฐ, ๊ฐ ์นผ๋ผ๊ฐ์ ์๊ด๊ด๊ณ(correlation)์ ํตํด ์๋ก ์ด๋ค ๊ด๋ จ์ฑ์ด ์๋์ง ๋ฑ์ ํ์ ํ๋ ๊ฒ์ด ์ค์ํ๋ค๋ ๊ฒ. ๊ทธ๋ฆฌ๊ณ , coef ๋ํ, p-value๋ฅผ ๊ฐ์ง๋ฉฐ, ๋๋ฌด ์์ ๊ฐ์ ๋ํด์๋ ์ ํจํ์ง ์๋ค๋ ๊ฒ ์ ๋๋ฅผ ๋ฐฐ์ ๋ค.
- ๊ทธ ์ธ๋ก, ํด๋น ๋ชจ๋ธ์ ์๊ฐํํ๋ ๋ฐฉ๋ฒ๋ค์ ๋ํด์๋ ๋ฐฐ์ ์ผ๋, ์ด ๋ถ๋ถ์ ๊ทธ์ ์ ์ธํ์๋ค.
GitHub์์ ์ด ๊ธ์ ๋งํฌ๋ค์ด ์๋ณธ ๋ณด๊ธฐ & ์์ ์ ์src/content/posts/2020-01-18-fitting_stat_model_week_2_part1_lin_reg
๐ ์ฐ๊ด ์ง์ ํ๋ธ:๐ python-libs ์นดํ
๊ณ ๋ฆฌ๐ Python ์ปค๋ฆฌํ๋ผ#python#python-libs#coursera
๐กRELATED ARTICLES ยท ํจ๊ป ์ฝ์ผ๋ฉด ์ข์ ์ฐ๊ด ํฌ์คํธ
python-libs
Fitting Statistical Models to Data with Python - WEEK 1 - Part 1
์ฝ์ด๋ณด๊ธฐ→
python-libs
Fitting Statistical Models to Data with Python - WEEK 1 - Part 2
์ฝ์ด๋ณด๊ธฐ→
python-libs
Fitting Statistical Models to Data with Python - WEEK 2 - Part 2
์ฝ์ด๋ณด๊ธฐ→
python-libs
Inferential Statistical Analysis with Python - WEEK 2
์ฝ์ด๋ณด๊ธฐ→