R의 glm()에서 회귀 계수를 추출하는 방법


다음 방법을 사용하여 R의 glm() 함수에서 회귀 계수를 추출할 수 있습니다.

방법 1: 모든 회귀계수 추출

 model$coefficients

방법 2: 특정 변수에 대한 회귀계수 추출

 model$coefficients[' my_variable ']

방법 3: 표준오차, Z값, P값을 포함한 모든 회귀계수 추출

 summary(model)$coefficients

다음 예에서는 이러한 방법을 실제로 사용하는 방법을 보여줍니다.

예: R의 glm()에서 회귀 계수 추출

ISLR 패키지의 기본 데이터 세트를 사용하여 로지스틱 회귀 모델을 적합하다고 가정합니다.

 #load dataset
data <- ISLR::Default

#view first six rows of data
head(data)

  default student balance income
1 No No 729.5265 44361.625
2 No Yes 817.1804 12106.135
3 No No 1073.5492 31767.139
4 No No 529.2506 35704.494
5 No No 785.6559 38463.496
6 No Yes 919.5885 7491.559

#fit logistic regression model
model <- glm(default~student+balance+income, family=' binomial ', data=data)

#view summary of logistic regression model
summary(model)

Call:
glm(formula = default ~ student + balance + income, family = "binomial", 
    data = data)

Deviance Residuals: 
    Min 1Q Median 3Q Max  
-2.4691 -0.1418 -0.0557 -0.0203 3.7383  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept) -1.087e+01 4.923e-01 -22.080 < 2e-16 ***
studentYes -6.468e-01 2.363e-01 -2.738 0.00619 ** 
balance 5.737e-03 2.319e-04 24.738 < 2e-16 ***
income 3.033e-06 8.203e-06 0.370 0.71152    
---
Significant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2920.6 on 9999 degrees of freedom
Residual deviance: 1571.5 on 9996 degrees of freedom
AIC: 1579.5

Number of Fisher Scoring iterations: 8

model$coefficients를 입력하여 모델에서 모든 회귀 계수를 추출할 수 있습니다.

 #extract all regression coefficients
model$coefficients

  (Intercept) studentYes balance income 
-1.086905e+01 -6.467758e-01 5.736505e-03 3.033450e-06

model$coefficients[‘balance’]를 입력하여 균형 변수에 대한 회귀 계수만 추출할 수도 있습니다.

 #extract coefficient for 'balance'
model$coefficients[' balance ']

balance 
0.005736505

표준 오류, z-값 및 p-값 과 함께 회귀 계수를 표시하려면 다음과 같이 summary(model)$ 계수를 사용할 수 있습니다.

 #view regression coefficients with standard errors, z values and p-values
summary(model)$coefficients

                 Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.086905e+01 4.922555e-01 -22.080088 4.911280e-108
studentYes -6.467758e-01 2.362525e-01 -2.737646 6.188063e-03
balance 5.736505e-03 2.318945e-04 24.737563 4.219578e-135
income 3.033450e-06 8.202615e-06 0.369815 7.115203e-01

이 출력에서 특정 값에 액세스할 수도 있습니다.

예를 들어, 다음 코드를 사용하여 잔액 변수의 p 값에 액세스할 수 있습니다.

 #view p-value for balance variable
summary(model)$coefficients[' balance ', ' Pr(>|z|) ']

[1] 4.219578e-135

또는 다음 코드를 사용하여 각 회귀 계수에 대한 p-값에 액세스할 수 있습니다.

 #view p-value for all variables
summary(model)$coefficients[, ' Pr(>|z|) ']

  (Intercept) studentYes balance income 
4.911280e-108 6.188063e-03 4.219578e-135 7.115203e-01 

모델의 각 회귀계수에 대한 P값이 표시됩니다.

유사한 구문을 사용하여 출력의 모든 값에 액세스할 수 있습니다.

추가 리소스

다음 튜토리얼에서는 R에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

R에서 단순 선형 회귀를 수행하는 방법
R에서 다중 선형 회귀를 수행하는 방법
R에서 로지스틱 회귀를 수행하는 방법
R에서 2차 회귀를 수행하는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다