library(tidyverse)
library(easystats)
library(GGally)
library(skimr)
library(broom)
library(ggdag)
library(dagitty)
library(broom)
#data("msleep")
partial
먼저
partial
set.seed(1)
dagify(Y ~ X1,
~ eX1,
X1 ~~ eY,
eX1 ~ eY,
Y ~ X2,
Y ~ X2) %>%
X1 ggdag() +
theme_minimal()
그림을 통해 보면 X2는 confounder로 X1과 Y에 동시에 영향을 주는 변수입니다. 이 때, confounder의 효과를 제거하고 Y ~ X1의 효과를 보려고 합니다. 이 경우 partial
partial
Example
data description
포유류의 수면과 관련된 데이터
sleep_total : 총 수면 시간 (
)brainwt : 뇌의 무게
bodywt : 체중
brainwt(
<- read_csv("./msleep.csv") dat
<- dat %>%
corr cor()
corr
sleep_total brainwt bodywt
sleep_total 1.0000000 -0.6806058 -0.6810127
brainwt -0.6806058 1.0000000 0.9448724
bodywt -0.6810127 0.9448724 1.0000000
먼저 correlation(zero-order-correlation)를 보면 sleep_total과 brainwt의 correlation는 correlation
패키지에 partial correlation을 계산하는 함수가 이미 있으므로 이용해보겠습니다.
cor_to_pcor(corr)
sleep_total brainwt bodywt
sleep_total 1.0000000 -0.1548778 -0.1580967
brainwt -0.1548778 1.0000000 0.8972459
bodywt -0.1580967 0.8972459 1.0000000
sleep_total과 brainwt의 partial correlation는
cor_to_pcor(corr)^2
sleep_total brainwt bodywt
sleep_total 1.00000000 0.02398713 0.02499458
brainwt 0.02398713 1.00000000 0.80505014
bodywt 0.02499458 0.80505014 1.00000000
partial
ANOVA table을 이용해서 다시 계산해보겠습니다.
<- lm(sleep_total ~ ., dat)
fit_f <- lm(sleep_total ~ bodywt, dat)
fit_b <- anova(fit_f) %>% tidy()
fit_f_anov <- anova(fit_b) %>% tidy()
fit_b_anov
$sumsq[2] - fit_f_anov$sumsq[3])/fit_b_anov$sumsq[2] (fit_b_anov
[1] 0.02398713
결과는 동일한 것을 확인할 수 있습니다.
<- lm(sleep_total ~ bodywt, dat)
fit1 <- lm(brainwt ~ bodywt, dat)
fit2
<- augment(fit1)$.resid
r1 <- augment(fit2)$.resid
r2
cor(r1, r2)^2
[1] 0.02398713
semi partial(or part)
set.seed(12)
dagify(X1 ~ X2,
~ eX1,
X1 ~ X1,
Y ~ eX1) %>%
Y ggdag() +
theme_minimal()
semi partial(or part)
Example
sleep_total(
cor_to_spcor(corr, cov = sapply(dat, sd))
sleep_total brainwt bodywt
sleep_total 1.00000000 -0.1134126 -0.1158295
brainwt -0.05071300 1.0000000 0.6573670
bodywt -0.05176701 0.6570276 1.0000000
semi-partial correlation을 구해보면 partial correlation과 비슷하게
cor_to_spcor(corr, cov = sapply(dat, sd))^2
sleep_total brainwt bodywt
sleep_total 1.000000000 0.01286242 0.01341648
brainwt 0.002571808 1.00000000 0.43213138
bodywt 0.002679823 0.43168533 1.00000000
semi-partial
<- lm(sleep_total ~ bodywt + brainwt, dat)
fit1 <- lm(sleep_total ~ bodywt, dat)
fit2
<- broom::glance(fit1)$r.squared
r2_y12 <- broom::glance(fit2)$r.squared
r2_y2
- r2_y2 r2_y12
[1] 0.01286242
이는 단순하게 full model의
<- lm(brainwt ~ bodywt, dat)
fit3
cor(augment(fit3)$.resid, dat$sleep_total)^2
[1] 0.01286242
또는
참고자료
https://brendanhcullen.github.io/psy612/lectures/7-partial.html#25
https://rpubs.com/KwonPublishing/249631
https://easystats.github.io/correlation/reference/cor_to_pcor.html
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=soowon0109&logNo=30173631827
Citation
@online{don2022,
author = {Don, Don and Don, Don},
title = {Partial {R} Squared},
date = {2022-08-31},
url = {https://dondonkim.netlify.app/posts/2022-08-31-partial-correlation/partial_corr.html},
langid = {en}
}