(통계 어렵다..)
연속적인 데이터(종속변수)일 때는 회귀 분석을 쓰지만, 범위형 데이터일 때는 로지스틱 회귀 분석을 사용한다.
<로지스틱 회귀 분석>
https://www.youtube.com/watch?v=bNt4xH_GxFk
일반적으로 종속변수가 이분형일 경우 종속변수와 독립변수의 관계는 S-shape이 형태를 띄게 된다. s-shape의 관계에서 종속변수에 logit 변환을 취하게 되면 종속변수와 독립변수의 관계가 선형으로 바뀌게 되는데 이 모형을 로지스틱회귀모형이라 한다
http://www.iexceller.com/MyXls/External_lectures/OnRainbow/OnRainbow_91.asp
http://blog.naver.com/libido1014/120122772781
http://wolfpack.hnu.ac.kr/2012_Spring/LM/LM_Ch12%20Logistic.pdf
<ODDS 개념>
http://wolfpack.hnu.ac.kr/2012_Spring/LM/LM_Ch12%20Logistic.pdf
ODDs
p/1 -p로 정의되며
p
임의의 사건이 발생할(성공) 확률로 이것은 도박의 기준이 된다.
한국이 2002 년 16 강에 들어갈 확률 0.1 이면 1/9 이 Odds 이다. 즉 한국 승리에 1$을 걸은
사람은 한국이 이길 경우 9$을 상금으로 받게 된다. 브라질이 2002 년 16 강에 들어갈 확률
0.8 이면 4 가 Odds 이다. 그러므로 4$을 걸면 1$을 상금으로 받게 된다.
http://tip.daum.net/openknow/65887671
http://dohwan.tistory.com/entry/Odds%EC%99%80-Odds-Ratios-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0
많은 의사들에게 있어 승산(Odds)과 승산비(Odds Ratios, 이하 OR)는 이해하기 어렵다. Odds는 "어떤 사건이 일어날 확률을 그 사건이 일어나지 않을 확률로 나눈 것"이다. Odds Ratio는 "한 그룹에서의 odds를 다른 그룹에서의 odds로 나눠준 것이다" (예를 들어 약을 복용한 그룹의 odds/약을 복용하지 않은 그룹의 Odds
확률(Probability)이 낮을 때(예를 들어 10% 이하일 때)는 OR은 실제의 relative risk(상대위험도, RR)를 반영한다. 그러나 사건의 확률이 높아질수록 OR은 점점 더 RR보다 과장되어 OR이 더 이상 RR을 대체하지 못한다. OR은 association(연관성)을 측정하는 데에는 항상 좋지만, RR을 대체하기에 항항 좋은 것은 아니다. OR을 이해하는 것이 어렵기 때문에, 그 활용은 OR이 연관성의 측정 용도로 적절한 Case-Control study와 logistic regression에 제한되어 사용되는 것이 좋다.
로지스틱 회귀 분석은 log(odds)를 모형화한 것..
<R>
glm은 generalized linear model의 약자이다.
family의 값은 binomial(이항 분포, 0아니면 1)로 정해야 (family = binomial) 로직스틱 회귀분석을 진행할 수 있다.
출처: http://www.cookbook-r.com/Statistical_analysis/Logistic_regression/
> data(mtcars)
> dat <- subset(mtcars, select=c(mpg, am, vs))
> dat
mpg am vs
Mazda RX4 21.0 1 0
Mazda RX4 Wag 21.0 1 0
Datsun 710 22.8 1 1
Hornet 4 Drive 21.4 0 1
Hornet Sportabout 18.7 0 0
Valiant 18.1 0 1
Duster 360 14.3 0 0
Merc 240D 24.4 0 1
Merc 230 22.8 0 1
Merc 280 19.2 0 1
Merc 280C 17.8 0 1
Merc 450SE 16.4 0 0
Merc 450SL 17.3 0 0
Merc 450SLC 15.2 0 0
Cadillac Fleetwood 10.4 0 0
Lincoln Continental 10.4 0 0
Chrysler Imperial 14.7 0 0
Fiat 128 32.4 1 1
Honda Civic 30.4 1 1
Toyota Corolla 33.9 1 1
Toyota Corona 21.5 0 1
Dodge Challenger 15.5 0 0
AMC Javelin 15.2 0 0
Camaro Z28 13.3 0 0
Pontiac Firebird 19.2 0 0
Fiat X1-9 27.3 1 1
Porsche 914-2 26.0 1 0
Lotus Europa 30.4 1 1
Ford Pantera L 15.8 1 0
Ferrari Dino 19.7 1 0
Maserati Bora 15.0 1 0
Volvo 142E 21.4 1 1
>
>
> logr_vm <- glm(vs ~ mpg, data=dat, family=binomial)
> logr_vm
Call: glm(formula = vs ~ mpg, family = binomial, data = dat)
Coefficients:
(Intercept) mpg
-8.8331 0.4304
Degrees of Freedom: 31 Total (i.e. Null); 30 Residual
Null Deviance: 43.86
Residual Deviance: 25.53 AIC: 29.53
>
> logr_vm <- glm(vs ~ mpg, data=dat, family=binomial(link="logit"))
> logr_vm
Call: glm(formula = vs ~ mpg, family = binomial(link = "logit"), data = dat)
Coefficients:
(Intercept) mpg
-8.8331 0.4304
Degrees of Freedom: 31 Total (i.e. Null); 30 Residual
Null Deviance: 43.86
Residual Deviance: 25.53 AIC: 29.53
>
>
> library(ggplot2)
> ggplot(dat, aes(x=mpg, y=vs)) + geom_point() +
+ stat_smooth(method="glm", family="binomial", se=FALSE)
>
> plot(dat$mpg, dat$vs)
> curve(predict(logr_vm, data.frame(mpg=x), type="response"), add=TRUE)
>
>
> logr_va <- glm(vs ~ am, data=dat, family=binomial)
> logr_va
Call: glm(formula = vs ~ am, family = binomial, data = dat)
Coefficients:
(Intercept) am
-0.5390 0.6931
Degrees of Freedom: 31 Total (i.e. Null); 30 Residual
Null Deviance: 43.86
Residual Deviance: 42.95 AIC: 46.95
> summary(logr_va)
Call:
glm(formula = vs ~ am, family = binomial, data = dat)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.2435 -0.9587 -0.9587 1.1127 1.4132
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.5390 0.4756 -1.133 0.257
am 0.6931 0.7319 0.947 0.344
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 43.860 on 31 degrees of freedom
Residual deviance: 42.953 on 30 degrees of freedom
AIC: 46.953
Number of Fisher Scoring iterations: 4
>
> library(ggplot2)
> ggplot(dat, aes(x=am, y=vs)) +
+ geom_point(shape=1, position=position_jitter(width=.05,height=.05)) +
+ stat_smooth(method="glm", family="binomial", se=FALSE)
>
>
> plot(jitter(dat$am, .2), jitter(dat$vs, .2))
> curve(predict(logr_va, data.frame(am=x), type="response"), add=TRUE)
>
>
> logr_vma <- glm(vs ~ mpg + am, data=dat, family=binomial)
> logr_vma
Call: glm(formula = vs ~ mpg + am, family = binomial, data = dat)
Coefficients:
(Intercept) mpg am
-12.7051 0.6809 -3.0073
Degrees of Freedom: 31 Total (i.e. Null); 29 Residual
Null Deviance: 43.86
Residual Deviance: 20.65 AIC: 26.65
>
> summary(logr_vma)
Call:
glm(formula = vs ~ mpg + am, family = binomial, data = dat)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.05888 -0.44544 -0.08765 0.33335 1.68405
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -12.7051 4.6252 -2.747 0.00602 **
mpg 0.6809 0.2524 2.698 0.00697 **
am -3.0073 1.5995 -1.880 0.06009 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 43.860 on 31 degrees of freedom
Residual deviance: 20.646 on 29 degrees of freedom
AIC: 26.646
Number of Fisher Scoring iterations: 6
참조할만한 좋은 R 소스
'R' 카테고리의 다른 글
[R] Error in plot.new() : figure margins too large 해결하기 (0) | 2016.01.14 |
---|---|
[R] k-means 알고리즘 (0) | 2016.01.12 |
[R] 문자열을 토큰으로 나누기 (strsplit) (0) | 2016.01.06 |
[R] 숫자로 된 문자열을 숫자로 변환하기 (0) | 2016.01.05 |
[R] 카이제곱 분포(Chi-square distribution)와 카이제곱 검정 (Chi-squared test) (0) | 2015.12.30 |