티스토리 뷰
INTRO
1. 회귀분석이란?
2. 단순회귀분석
3. 다중회귀분석(중회귀분석)
4. 다항회귀분석
회귀분석 방법 : summary
- Residuals(잔차)
- Coefficients(회귀계수)
- 모델 적합도 : Multiple R-squared, Adjusted R-squared, F-statistic, p-value
단순회귀분석(Simple Regression Analysis)
test_regression_1.R
rm(list=ls())
setwd = "~/Rcoding"
## simple linear regression
## create vector (x1,y1) ,..., (xn,yn)
set.seed(2)
x = runif(10,0,11)
y = 2 + 3*x + rnorm(10,0,0.2)
dfrm_1 = data.frame(x,y)
dfrm_1
model_1 = lm(y~x, data=dfrm_1)
model_1
summary(model_1)
plot(x,y,col = c("blue"))
title("Simple Linear Regression Data & Fitting Curve")
legend('topleft',c('x'), col = c("blue"), pch = c(1))
abline(model_1$coef[1], model_1$coef[2], col = c("red"))
text(x[3]-2,y[3]+5,paste("y=",model_1$coef[1],"x + ",model_1$coef[2]))
출력결과
> source("~/Rcoding/test_regression_1.R", echo=TRUE)
> rm(list=ls())
> setwd = "~/Rcoding"
> ## simple linear regression
> ## create vector (x1,y1) ,..., (xn,yn)
> set.seed(2)
> x = runif(10,0,11)
> y = 2 + 3*x + rnorm(10,0,0.2)
> dfrm_1 = data.frame(x,y)
> dfrm_1
x y
1 2.033705 8.127599
2 7.726114 25.319934
3 6.306590 20.871829
4 1.848571 7.942608
5 10.382233 33.118941
6 10.378225 33.218204
7 1.420749 6.458597
8 9.167937 29.425272
9 5.148204 17.236677
10 6.049821 20.505909
> model_1 = lm(y~x, data=dfrm_1)
> model_1
Call:
lm(formula = y ~ x, data = dfrm_1)
Coefficients:
(Intercept) x
2.213 2.979
> summary(model_1)
Call:
lm(formula = y ~ x, data = dfrm_1)
Residuals:
Min 1Q Median 3Q Max
-0.311062 -0.118624 -0.002713 0.093296 0.272610
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.2133 0.1257 17.61 1.11e-07 ***
x 2.9786 0.0183 162.76 2.27e-15 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1885 on 8 degrees of freedom
Multiple R-squared: 0.9997, Adjusted R-squared: 0.9997
F-statistic: 2.649e+04 on 1 and 8 DF, p-value: 2.272e-15
>
> plot(x,y,col = c("blue"))
> title("Simple Linear Regression Data & Fitting Curve")
> legend('topleft',c('x'), col = c("blue"), pch = c(1))
> abline(model_1$coef[1], model_1$coef[2], col = c("red"))
> text(x[3]-2,y[3]+5,paste("y=",model_1$coef[1],"x + ",model_1$coef[2]))
> summary(model_1)
Call:
lm(formula = y ~ x, data = dfrm_1)
Residuals:
Min 1Q Median 3Q Max
-0.311062 -0.118624 -0.002713 0.093296 0.272610
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.2133 0.1257 17.61 1.11e-07 ***
x 2.9786 0.0183 162.76 2.27e-15 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1885 on 8 degrees of freedom
Multiple R-squared: 0.9997, Adjusted R-squared: 0.9997
F-statistic: 2.649e+04 on 1 and 8 DF, p-value: 2.272e-15
분석방법
- Residuals(잔차)
Residuals:
Min 1Q Median 3Q Max
-0.311062 -0.118624 -0.002713 0.093296 0.272610
- 잔차 : 예측값과 실제값 차이
- 잔차의 최솟값(Min), 사분위수(1Q, Median, 3Q), 최댓값(Max)을 보여준다.
- Coefficients(회귀계수)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.2133 0.1257 17.61 1.11e-07 ***
x 2.9786 0.0183 162.76 2.27e-15 ***
- Estimate(기대값, 추정치)는 데이터로부터 얻은 계수의 추정값
- Intercept(절편)의 추정치는 2.2133로, x가 0일 때 y의 값
- 데이터 분석 결과 1차 선형(lienear)이며 함수로 표현하면 다음과 같다
- Pr(>|t|)는 모집단에서 계수가 0일 때, 현재와 같은 크기의 표본에서 이러한 계수가 추정될 확률 p-value
- p-value(=2.27e-15)가 보통 5%와 같은 유의수준을 정하여 그보다 작으면(p < 0.05), "통계적으로 유의미하다"라고 한다.
- 즉, x가 증가할 때 기대되는 y의 변화는 유의수준 99.9%에서 통계적으로 유의미하다.
# 참고
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
구분 | p-value | 유의수준 |
* | < 0.05 | 95% |
** | < 0.01 | 99% |
*** | < 0.001 | 99.9% |
아무것도 없거나 점(.)으로 표시 | 추정 계수가 통계적으로 유의하지 않음 |
- 모델 적합도 : Multiple R-squared, Adjusted R-squared, F-statistic, p-value
Residual standard error: 0.1885 on 8 degrees of freedom
Multiple R-squared: 0.9997, Adjusted R-squared: 0.9997
F-statistic: 2.649e+04 on 1 and 8 DF, p-value: 2.272e-15
- Multiple R-squared: 0.9997
- 결정계수 R2(R-squared)
- 모형 적합도(설명력) : 추정된 회귀선이 관측값을 설명하는 정도
- 총 변동 y의 분산을 x가 99.97%를 설명한다.
- Adjusted R-squared: 0.9997
- 수정된 결정계수
- 독립변수가 여러 개인 다중회귀분석에서 사용
- 독립변수의 개수와 표본의 크기를 고려하여 R-squared를 보정
- 모델이 대상변수의 몇 %를 설명하는 지 확인하며, 서로 다른 모형을 비교할 때는 이 지표가 높은 쪽은 선택
- 결정계수(R2, R-squared)와 차이가 크면 회귀 모형을 재검토 해야함
- F-statistic: 2.649e+04 on 1 and 8 DF, p-value: 2.272e-15
- F 통계량 MSR(Mean Square Regression)/MSE(Mean Square Error)의 비율을 F 분포를 통해 검정한 것으로
- 회귀 모형에 대한 통계적 유의미성 검증, 계수 중 하나라도 0이 아닌 값이 있으면 유의미하다라고 판단
- 즉, 이 모형은 주어진 표본 뿐 아니라 모집단에서도 의미있는 모형이라 할 수 있음
'R' 카테고리의 다른 글
(macOS)[R] 기초 통계 분석 : 회귀 분석(Regression Analysis) - 4 (0) | 2022.04.13 |
---|---|
(macOS)[R] 기초 통계 분석 : 회귀 분석(Regression Analysis) - 3 (0) | 2022.04.11 |
(macOS)[R] 기초 통계 분석 : 회귀 분석(Regression Analysis) - 1 (0) | 2022.04.05 |
(macOS)[R] 기초 통계 분석 : 기술 통계 (0) | 2022.04.05 |
(macOS)[R] 결측값 처리 / 이상치 탐색 (0) | 2022.04.04 |
- Total
- Today
- Yesterday
- Regression
- 확진
- ERP
- 자가격리
- pyserial
- 라즈베리파이
- raspberrypi
- Pandas
- Django
- COVID-19
- template
- github
- MacOS
- git
- analysis
- DS18B20
- vscode
- r
- arduino
- 코로나19
- sublime text
- 코로나
- Raspberry Pi
- Python
- DAQ
- SSH
- server
- Model
- Templates
- CSV
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |