R의 lm() 함수에서 p 값을 추출하는 방법
R의 lm() 함수 에서 p-값을 추출하려면 다음 방법을 사용할 수 있습니다.
방법 1: 회귀 모델에서 전체 P 값 추출
#define function to extract overall p-value of model overall_p <- function (my_model) { f <- summary(my_model)$fstatistic p <- pf(f[1],f[2],f[3],lower. tail =F) attributes(p) <- NULL return (p) } #extract overall p-value of model overall_p(model)
방법 2: 회귀계수에 대한 개별 P값 추출
summary(model)$coefficients[,4]
다음 예에서는 이러한 방법을 실제로 사용하는 방법을 보여줍니다.
예: R의 lm()에서 P 값 추출
R에 다음과 같은 다중 선형 회귀 모델을 적합하다고 가정합니다.
#create data frame df <- data. frame (rating=c(67, 75, 79, 85, 90, 96, 97), points=c(8, 12, 16, 15, 22, 28, 24), assists=c(4, 6, 6, 5, 3, 8, 7), rebounds=c(1, 4, 3, 3, 2, 6, 7)) #fit multiple linear regression model model <- lm(rating ~ points + assists + rebounds, data=df)
summary() 함수를 사용하여 회귀 모델의 전체 요약을 표시할 수 있습니다.
#view model summary
summary(model)
Call:
lm(formula = rating ~ points + assists + rebounds, data = df)
Residuals:
1 2 3 4 5 6 7
-1.5902 -1.7181 0.2413 4.8597 -1.0201 -0.6082 -0.1644
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.4355 6.6932 9.926 0.00218 **
points 1.2152 0.2788 4.359 0.02232 *
assists -2.5968 1.6263 -1.597 0.20860
rebounds 2.8202 1.6118 1.750 0.17847
---
Significant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.193 on 3 degrees of freedom
Multiple R-squared: 0.9589, Adjusted R-squared: 0.9179
F-statistic: 23.35 on 3 and 3 DF, p-value: 0.01396
결과 맨 아래에서 회귀 모델의 전체 p-값이 0.01396 임을 확인할 수 있습니다.
모델에서 이 p-값만 추출하려면 사용자 정의 함수를 정의하면 됩니다.
#define function to extract overall p-value of model overall_p <- function (my_model) { f <- summary(my_model)$fstatistic p <- pf(f[1],f[2],f[3],lower. tail =F) attributes(p) <- NULL return (p) } #extract overall p-value of model overall_p(model) [1] 0.01395572
이 함수는 위의 모델 출력과 동일한 p-값을 반환합니다.
모델에서 개별 회귀 계수에 대한 p-값을 추출하려면 다음 구문을 사용할 수 있습니다.
#extract p-values for individual regression coefficients in model
summary(model)$coefficients[,4]
(Intercept) points assists rebounds
0.002175313 0.022315418 0.208600183 0.178471275
여기에 표시된 p-값은 위 회귀 출력의 Pr(> |t|) 열에 있는 값과 일치합니다.
관련 항목: R의 lm() 함수에서 R-제곱을 추출하는 방법
추가 리소스
다음 튜토리얼에서는 R에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
R에서 단순 선형 회귀를 수행하는 방법
R에서 다중 선형 회귀를 수행하는 방법
R에서 잔차 플롯을 만드는 방법