
library(survival)
data<-colon
View(data)
library(descr)
CrossTable(datastatus)
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
======================================
dataobstruct 0 1 Total
0 775 723 1498
0.465 0.474
0.517 0.483 0.806
0.826 0.786
0.417 0.389
1 163 197 360
1.933 1.971
0.453 0.547 0.194
0.174 0.214
0.088 0.106
Total 938 920 1858
0.505 0.495
💡775197/(723163)
[1] 1.295514
| status = 0 (생존) | status = 1 (사망) | 합계 | |
|---|---|---|---|
| obstruct = 0 | 775 | 723 | 1498 |
| obstruct = 1 | 163 | 197 | 360 |
| 합계 | 938 | 920 | 1858 |
교차표 해석
행 비율과 열 비율
OR의 정의
OR=(775⋅197)(723⋅163)=152675117849=1.295OR = \frac{(775 \cdot 197)}{(723 \cdot 163)} = \frac{152675}{117849} = 1.295
OR=(723⋅163)(775⋅197)=117849152675=1.295
OR 해석
💡exp(0.25891)
[1] 1.295517
coef(m)
(Intercept) obstruct
-0.06945381 0.25890733
exp(coef(m))
(Intercept) obstruct
0.9329032 1.2955138
round(exp(cbind(coef(m),confint(m))),3)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) 0.933 0.843 1.032
obstruct 1.296 1.029 1.633
log(p1−p)=β0+β1⋅obstruct\log\left(\frac{p}{1-p}\right) = \beta_0 + \beta_1 \cdot \text{obstruct}
log(1−pp)=β0+β1⋅obstruct
회귀분석 결과
(Intercept) obstruct
-0.06945381 0.25890733
exp(coef(m))
(Intercept) obstruct
0.9329032 1.2955138
round(exp(cbind(coef(m), confint(m))), 3)
2.5 % 97.5 %
(Intercept) 0.933 0.843 1.032
obstruct 1.296 1.029 1.633
신뢰구간 해석
(Intercept): 신뢰구간 [0.843, 1.032]은 1을 포함하기 때문에, 유의하지 않음. 즉, 장폐색이 없는 사람의 사망 오즈(odds)는 유의미하지 않음.
obstruct: 신뢰구간 [1.029, 1.633]은 1을 포함하지 않음, 따라서 유의미한 결과임.
- obstruct = 1일 때, 장폐색이 있는 사람은 장폐색이 없는 사람에 비해 사망 가능성이 1.029배에서 1.633배 더 높다고 볼 수 있습니다.
장폐색이 없는 경우에 비해 장폐색이 있는 경우 사망 위험이 1.295배 높다.
*신뢰구간 [1.029, 1.633]이 1을 포함하지 않으므로 유의미**한 결과로 볼 수 있습니다.
OR 해석과 로지스틱 회귀 해석은 일치합니다.
교차표와 로지스틱 회귀분석 모두 장폐색이 사망에 영향을 미친다는 증거를 제공합니다.
data<-read.csv("HR_comma_sep.csv")
head(data)
satisfaction_level last_evaluation number_project average_montly_hours
1 0.38 0.53 2 157
2 0.80 0.86 5 262
3 0.11 0.88 7 272
4 0.72 0.87 5 223
5 0.37 0.52 2 159
6 0.41 0.50 2 153
time_spend_company Work_accident left promotion_last_5years department salary
1 3 0 1 0 sales low
2 6 0 1 0 sales medium
3 4 0 1 0 sales medium
4 5 0 1 0 sales low
5 3 0 1 0 sales low
6 3 0 1 0 sales low
View(data)
💡summary(data)
satisfaction_level last_evaluation number_project average_montly_hours
Min. :0.0900 Min. :0.3600 Min. :2.000 Min. : 96.01st Qu.:0.4400 1st Qu.:0.5600 1st Qu.:3.000 1st Qu.:156.0
Median :0.6400 Median :0.7200 Median :4.000 Median :200.0
Mean :0.6128 Mean :0.7161 Mean :3.803 Mean :201.1
3rd Qu.:0.8200 3rd Qu.:0.8700 3rd Qu.:5.000 3rd Qu.:245.0
Max. :1.0000 Max. :1.0000 Max. :7.000 Max. :310.0
time_spend_company Work_accident left promotion_last_5years
Min. : 2.000 Min. :0.0000 Min. :0.0000 Min. :0.000001st Qu.: 3.000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000
Median : 3.000 Median :0.0000 Median :0.0000 Median :0.00000
Mean : 3.498 Mean :0.1446 Mean :0.2381 Mean :0.02127
3rd Qu.: 4.000 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:0.00000
Max. :10.000 Max. :1.0000 Max. :1.0000 Max. :1.00000
department salary
Length:14999 Length:14999
Class :character Class :character
Mode :character Mode :character
table(data$department)
accounting hr IT management marketing product_mng
767 739 1227 630 858 902
RandD sales support technical
787 4140 2229 2720table(data$salary)
high low medium
1237 7316 6446
read.csv("HR_comma_sep.csv")head(data)로 첫 6개의 행 확인, summary(data)로 각 변수의 기초 통계량을 확인.table(data$department) 및 table(data$salary)로 범주형 변수를 확인.해석
salary 변수의 범주는 low, medium, high로 구분됨.department의 범주는 10개의 부서로 구성됨.left 변수는 이직 여부로 0 또는 1로 구성되어 있음.m<-glm(left~salary, family="binomial",data=data)
summary(m)
Call:
glm(formula = left ~ salary, family = "binomial", data = data)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.6451 0.1143 -23.14 <2e-16
salarylow 1.7830 0.1171 15.22 <2e-16
salarymedium 1.2856 0.1184 10.86 <2e-16 ***
Signif. codes: 0 ‘*’ 0.001 ‘’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 16465 on 14998 degrees of freedom
Residual deviance: 16030 on 14996 degrees of freedom
AIC: 16036
Number of Fisher Scoring iterations: 5
round(exp(cbind(coef(m),confint(m))),3)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) 0.071 0.056 0.088
salarylow 5.947 4.759 7.536
salarymedium 3.617 2.886 4.594
HR<-data
HRsalary), 2)
m<-glm(left~salary2, family="binomial",data=data)
m<-glm(left~salary2, family="binomial",data=HR)
summary(m)
Call:
glm(formula = left ~ salary2, family = "binomial", data = HR)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.86218 0.02559 -33.69 <2e-16
salary2high -1.78295 0.11711 -15.22 <2e-16
salary2medium -0.49737 0.04011 -12.40 <2e-16 ***
Signif. codes: 0 ‘*’ 0.001 ‘’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 16465 on 14998 degrees of freedom
Residual deviance: 16030 on 14996 degrees of freedom
AIC: 16036
Number of Fisher Scoring iterations: 5
💡round(exp(cbind(coef(m),confint(m))),3)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) 0.422 0.402 0.444
salary2high 0.168 0.133 0.210
salary2medium 0.608 0.562 0.658
relevel() 함수로 Reference Group을 변경할 수 있음.HR$salary2 <- relevel(factor(data$salary), 2) # 'medium'을 Reference Group으로 설정
해석
모델 생성
m <- glm(left ~ salary, family = "binomial", data = data)
summary(m)
회귀 결과 해석
salarylow = 1.7830salarymedium = 1.2856해석
salary = high일 때, 이직할 오즈(odds)는 exp(-2.6451) ≈ 0.071. 즉, 이직할 확률이 낮음.salary = low일 때, 이직할 오즈가 exp(1.7830) ≈ 5.947배 증가.salary = medium일 때, 이직할 오즈가 exp(1.2856) ≈ 3.617배 증가.Reference 변경
R
코드 복사
HR$salary2 <- relevel(factor(data$salary), 2) # Reference를 'medium'으로 설정
m <- glm(left ~ salary2, family = "binomial", data = HR)
summary(m)
회귀 결과 해석
- 절편(Intercept) = -0.86218
- salary2high = -1.78295
- salary2medium = -0.49737
**해석**
- **Intercept = -0.86218**: `salary = medium`일 때, **이직할 오즈(odds)는 exp(-0.86218) ≈ 0.422**.
- **salary2high = -1.78295**: `salary = high`일 때, 이직할 오즈가 **exp(-1.78295) ≈ 0.168**배로 감소.
- **salary2medium = -0.49737**: `salary = medium`과 비교할 때, 이 값은 기준(reference) 그룹이므로 0으로 처리됨.
HRsatisfaction_level
m<-glm(left~dissatisfaction + salary + time_spend_company, family="binomial",data=HR)
step(m)
Start: AIC=13607.36
left ~ dissatisfaction + salary + time_spend_company
Df Deviance AIC
13597 13607
time_spend_company 1 13811 13819
salary 2 14039 14045
dissatisfaction 1 15676 15684
Call: glm(formula = left ~ dissatisfaction + salary + time_spend_company,
family = "binomial", data = HR)
Coefficients:
(Intercept) dissatisfaction salaryhigh salarymedium
-3.2432 3.7239 -1.9859 -0.5343
time_spend_company
0.2116
Degrees of Freedom: 14998 Total (i.e. Null); 14994 Residual
Null Deviance: 16460
Residual Deviance: 13600 AIC: 13610
summary(m)
Call:
glm(formula = left ~ dissatisfaction + salary + time_spend_company,
family = "binomial", data = HR)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.24316 0.07029 -46.14 <2e-16
dissatisfaction 3.72386 0.08852 42.07 <2e-16
salaryhigh -1.98592 0.12461 -15.94 <2e-16
salarymedium -0.53427 0.04436 -12.04 <2e-16
time_spend_company 0.21159 0.01418 14.92 <2e-16 ***
Signif. codes: 0 ‘*’ 0.001 ‘’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 16465 on 14998 degrees of freedom
Residual deviance: 13597 on 14994 degrees of freedom
AIC: 13607
Number of Fisher Scoring iterations: 5
💡round(exp(cbind(coef(m),confint(m))),3)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) 0.039 0.034 0.045
dissatisfaction 41.424 34.855 49.315
salaryhigh 0.137 0.107 0.174
salarymedium 0.586 0.537 0.639
time_spend_company 1.236 1.202 1.270
모형 생성
R
코드 복사
m1 <- glm(left ~ dissatisfaction + salary + time_spend_company, family = "binomial", data = HR)
summary(m1)
회귀 결과 해석
해석
salary = high일 때, 이직할 오즈가 exp(-1.9859) ≈ 0.137배 감소.salary = medium일 때, 이직할 오즈가 exp(-0.5343) ≈ 0.586배 감소.step() 함수 사용
step(m1)
해석
- step 함수는 AIC(정보기준, Akaike Information Criterion)를 기준으로 최적의 모형을 선택합니다.
- AIC가 낮을수록 좋은 모델로 평가합니다.
- dissatisfaction, salary, time_spend_company가 최종 모델로 선택되었습니다.
모수절약의 원칙을 따르려면 모델을 여러개 세우고 비교해본다! → 해당 데이터에서는 이직에 영향을 미치는 데이터들로만 이루어져있기 때문에 모든 데이터가 left에 영향을 준다. → 최적 모델과 단순 모델의 비교
m1<-glm(left~dissatisfaction + salary + time_spend_company, family="binomial",data=HR)
m2<-glm(left~dissatisfaction, family="binomial",data=HR)
library(lmtest)
필요한 패키지를 로딩중입니다: zoo
다음의 패키지를 부착합니다: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
lrtest(m1,m2)
Likelihood ratio test
Model 1: left ~ dissatisfaction + salary + time_spend_company
Model 2: left ~ dissatisfaction
#Df LogLik Df Chisq Pr(>Chisq)
1 5 -6798.7
2 2 -7098.9 -3 600.43 < 2.2e-16 ***
Signif. codes: 0 ‘*’ 0.001 ‘’ 0.01 ‘*’ 0.05 ‘.’ 0.1
summary(m2)
Call:
glm(formula = left ~ dissatisfaction, family = "binomial", data = HR)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.85860 0.04735 -60.38 <2e-16
dissatisfaction 3.83248 0.08720 43.95 <2e-16
Signif. codes: 0 ‘*’ 0.001 ‘’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 16465 on 14998 degrees of freedom
Residual deviance: 14198 on 14997 degrees of freedom
AIC: 14202
💡Number of Fisher Scoring iterations: 4
모형 생성
m1 <- glm(left ~ dissatisfaction + salary + time_spend_company, family = "binomial", data = HR)
m2 <- glm(left ~ dissatisfaction, family = "binomial", data = HR)
LR Test 수행
library(lmtest)
lrtest(m1, m2)
LR Test 결과
Likelihood ratio test
Model 1: left ~ dissatisfaction + salary + time_spend_company
Model 2: left ~ dissatisfaction
#Df LogLik Df Chisq Pr(>Chisq)
1 5 -6798.7
2 2 -7098.9 -3 600.43 < 2.2e-16 ***
해석
- LR Test는 두 모델의 유의한 차이가 있는지를 검정합니다.
- Chisq = 600.43, p < 2.2e-16로, 모형 1이 모형 2에 비해 유의미하게 더 적합하다고 판단할 수 있습니다.