partial_and_semipartial_correlation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
partial_and_semipartial_correlation [2024/10/15 13:19] – [e.g. Using ppcor.test with 4 var] hkimscil | partial_and_semipartial_correlation [2024/10/17 10:28] (current) – [e.g. Using ppcor.test with 4 var] hkimscil | ||
---|---|---|---|
Line 414: | Line 414: | ||
====== e.g. Using ppcor.test with 4 var ====== | ====== e.g. Using ppcor.test with 4 var ====== | ||
- | |||
< | < | ||
rm(list=ls()) | rm(list=ls()) | ||
- | options(digits = 4) | ||
- | HSGPA <- c(3.0, 3.2, 2.8, 2.5, 3.2, 3.8, 3.9, 3.8, 3.5, 3.1) | + | library(ggplot2) |
- | FGPA <- c(2.8, 3.0, 2.8, 2.2, 3.3, 3.3, 3.5, 3.7, 3.4, 2.9) | + | library(dplyr) |
- | SATV <- c(500, 550, 450, 400, 600, 650, 700, 550, 650, 550) | + | library(tidyr) |
- | GREV <- c(600, 670, 540, 800, 750, 820, 830, 670, 690, 600) | + | library(faux) |
- | ##GREV <- c(510, 670, 440, 800, 750, 420, 830, 470, 690, 600) | + | |
+ | set.seed(101) | ||
+ | scholar <- rnorm_multi(n = 50, | ||
+ | mu = c(3.12, 3.3, 540, 650), | ||
+ | sd = c(.25, .34, 12, 13), | ||
+ | r = c(0.15, 0.44, 0.47, 0.55, 0.45, 0.88), | ||
+ | | ||
+ | | ||
+ | attach(scholar) | ||
- | scholar <- data.frame(HSGPA, | + | # library(psych) |
- | # install.packages(" | + | |
- | library(psych) | + | |
describe(scholar) # provides descrptive information about each variable | describe(scholar) # provides descrptive information about each variable | ||
Line 436: | Line 439: | ||
pairs(scholar) | pairs(scholar) | ||
+ | # install.packages(" | ||
+ | library(ppcor) | ||
+ | |||
+ | reg.g.sh <- lm(GREV ~ SATV + HSGPA) | ||
+ | res.g.sh <- resid(reg.g.sh) | ||
+ | |||
+ | reg.g.fh <- lm(GREV ~ FGPA + HSGPA) | ||
+ | res.g.fh <- resid(reg.g.fh) | ||
+ | |||
+ | reg.g.sf <- lm(GREV ~ SATV + FGPA) | ||
+ | res.g.sf <- resid(reg.g.sf) | ||
+ | |||
+ | reg.f.sh <- lm(FGPA ~ SATV + HSGPA) | ||
+ | res.f <- resid(reg.f.sh) | ||
+ | |||
+ | reg.s.fh <- lm(SATV ~ FGPA + HSGPA) | ||
+ | res.s <- resid(reg.s.fh) | ||
+ | |||
+ | reg.h.sf <- lm(HSGPA ~ FGPA + SATV) | ||
+ | res.h <- resid(reg.h.sf) | ||
+ | |||
+ | reg.all <- lm(GREV ~ HSGPA + FGPA + SATV) | ||
+ | reg.1 <- lm(GREV ~ res.f) | ||
+ | reg.2 <- lm(GREV ~ res.s) | ||
+ | reg.3 <- lm(GREV ~ res.h) | ||
+ | |||
+ | summary(reg.all) | ||
+ | summary(reg.1) | ||
+ | summary(reg.2) | ||
+ | summary(reg.3) | ||
+ | |||
+ | reg.1a <- lm(res.g.sh~res.f) | ||
+ | reg.2a <- lm(res.g.fh~res.s) | ||
+ | reg.3a <- lm(res.g.sf~res.h) | ||
+ | |||
+ | reg.1$coefficient[2] | ||
+ | reg.2$coefficient[2] | ||
+ | reg.3$coefficient[2] | ||
+ | |||
+ | reg.1a$coefficient[2] | ||
+ | reg.2a$coefficient[2] | ||
+ | reg.3a$coefficient[2] | ||
+ | |||
+ | spr.y.f <- spcor.test(GREV, | ||
+ | spr.y.s <- spcor.test(GREV, | ||
+ | spr.y.h <- spcor.test(GREV, | ||
+ | |||
+ | spr.y.f$estimate | ||
+ | spr.y.s$estimate | ||
+ | spr.y.h$estimate | ||
+ | |||
+ | spr.y.f$estimate^2 | ||
+ | spr.y.s$estimate^2 | ||
+ | spr.y.h$estimate^2 | ||
+ | |||
+ | summary(reg.1)$r.square | ||
+ | summary(reg.2)$r.square | ||
+ | summary(reg.3)$r.square | ||
+ | |||
+ | ca <- summary(reg.1)$r.square + | ||
+ | summary(reg.2)$r.square + | ||
+ | summary(reg.3)$r.square | ||
+ | # so common explanation area should be | ||
+ | summary(reg.all)$r.square - carm(list=ls()) | ||
+ | |||
+ | library(ggplot2) | ||
+ | library(dplyr) | ||
+ | library(tidyr) | ||
+ | library(faux) | ||
+ | |||
+ | set.seed(101) | ||
+ | scholar <- rnorm_multi(n = 50, | ||
+ | mu = c(3.12, 3.3, 540, 650), | ||
+ | sd = c(.25, .34, 12, 13), | ||
+ | r = c(0.15, 0.44, 0.47, 0.55, 0.45, 0.88), | ||
+ | | ||
+ | | ||
attach(scholar) | attach(scholar) | ||
+ | |||
+ | # library(psych) | ||
+ | describe(scholar) # provides descrptive information about each variable | ||
+ | |||
+ | corrs <- cor(scholar) # find the correlations and set them into an object called ' | ||
+ | corrs # print corrs | ||
+ | |||
+ | pairs(scholar) | ||
+ | |||
# install.packages(" | # install.packages(" | ||
library(ppcor) | library(ppcor) | ||
- | pcor.test(GREV, | ||
reg.f.sh <- lm(FGPA ~ SATV + HSGPA) | reg.f.sh <- lm(FGPA ~ SATV + HSGPA) | ||
Line 447: | Line 535: | ||
res.s <- resid(reg.s.fh) | res.s <- resid(reg.s.fh) | ||
- | reg.h.sf <- lm(SATV ~ FGPA + HSGPA) | + | reg.h.sf <- lm(HSGPA ~ FGPA + SATV) |
res.h <- resid(reg.h.sf) | res.h <- resid(reg.h.sf) | ||
Line 460: | Line 548: | ||
summary(reg.3) | summary(reg.3) | ||
+ | reg.1$coefficient[2] | ||
+ | reg.2$coefficient[2] | ||
+ | reg.3$coefficient[2] | ||
+ | |||
+ | spr.y.f <- spcor.test(GREV, | ||
+ | spr.y.s <- spcor.test(GREV, | ||
+ | spr.y.h <- spcor.test(GREV, | ||
+ | |||
+ | spr.y.f$estimate | ||
+ | spr.y.s$estimate | ||
+ | spr.y.h$estimate | ||
+ | |||
+ | spr.y.f$estimate^2 | ||
+ | spr.y.s$estimate^2 | ||
+ | spr.y.h$estimate^2 | ||
+ | |||
+ | summary(reg.1)$r.square | ||
+ | summary(reg.2)$r.square | ||
+ | summary(reg.3)$r.square | ||
+ | |||
+ | ca <- summary(reg.1)$r.square + | ||
+ | summary(reg.2)$r.square + | ||
+ | summary(reg.3)$r.square | ||
+ | # so common explanation area should be | ||
+ | summary(reg.all)$r.square - ca | ||
</ | </ | ||
Line 465: | Line 578: | ||
> | > | ||
> rm(list=ls()) | > rm(list=ls()) | ||
- | > options(digits = 4) | ||
> | > | ||
- | > HSGPA <- c(3.0, 3.2, 2.8, 2.5, 3.2, 3.8, 3.9, 3.8, 3.5, 3.1) | + | > library(ggplot2) |
- | > FGPA <- c(2.8, 3.0, 2.8, 2.2, 3.3, 3.3, 3.5, 3.7, 3.4, 2.9) | + | > library(dplyr) |
- | > SATV <- c(500, 550, 450, 400, 600, 650, 700, 550, 650, 550) | + | > library(tidyr) |
- | > GREV <- c(600, 670, 540, 800, 750, 820, 830, 670, 690, 600) | + | > library(faux) |
- | > ##GREV <- c(510, 670, 440, 800, 750, 420, 830, 470, 690, 600) | + | |
> | > | ||
+ | > set.seed(101) | ||
+ | > scholar <- rnorm_multi(n = 50, | ||
+ | + mu = c(3.12, 3.3, 540, 650), | ||
+ | + sd = c(.25, .34, 12, 13), | ||
+ | + r = c(0.15, 0.44, 0.47, 0.55, 0.45, 0.88), | ||
+ | + varnames = c(" | ||
+ | + empirical = FALSE) | ||
+ | > attach(scholar) | ||
+ | The following objects are masked from scholar (pos = 3): | ||
+ | |||
+ | FGPA, GREV, HSGPA, SATV | ||
+ | |||
> | > | ||
- | > scholar <- data.frame(HSGPA, | + | > # library(psych) |
- | > # install.packages(" | + | |
- | > library(psych) | + | |
> describe(scholar) # provides descrptive information about each variable | > describe(scholar) # provides descrptive information about each variable | ||
- | vars n | + | vars n |
- | HSGPA 1 10 3.28 0.46 3.20 3.30 0.52 2.5 3.9 1.4 -0.08 | + | HSGPA 1 50 3.13 |
- | FGPA | + | FGPA |
- | SATV | + | SATV |
- | GREV | + | GREV |
- | kurtosis | + | kurtosis |
- | HSGPA -1.43 | + | HSGPA |
- | FGPA -0.77 | + | FGPA -0.01 0.05 |
- | SATV -1.27 29.63 | + | SATV -0.60 1.62 |
- | GREV -1.59 31.83 | + | GREV -0.54 1.68 |
> | > | ||
> corrs <- cor(scholar) # find the correlations and set them into an object called ' | > corrs <- cor(scholar) # find the correlations and set them into an object called ' | ||
> corrs # print corrs | > corrs # print corrs | ||
| | ||
- | HSGPA 1.0000 0.9226 0.8745 0.4007 | + | HSGPA 1.0000 0.3404 0.4627 0.5406 |
- | FGPA 0.9226 1.0000 0.8144 0.1906 | + | FGPA 0.3404 1.0000 0.5266 0.5096 |
- | SATV 0.8745 0.8144 1.0000 0.4630 | + | SATV 0.4627 0.5266 1.0000 0.8802 |
- | GREV 0.4007 0.1906 0.4630 1.0000 | + | GREV 0.5406 0.5096 0.8802 1.0000 |
> | > | ||
> pairs(scholar) | > pairs(scholar) | ||
> | > | ||
- | > attach(scholar) | ||
- | The following objects are masked _by_ .GlobalEnv: | ||
- | |||
- | FGPA, GREV, HSGPA, SATV | ||
- | |||
- | The following objects are masked from scholar (pos = 3): | ||
- | |||
- | FGPA, GREV, HSGPA, SATV | ||
- | |||
> # install.packages(" | > # install.packages(" | ||
> library(ppcor) | > library(ppcor) | ||
- | > pcor.test(GREV, | ||
- | estimate p.value statistic | ||
- | 1 | ||
> | > | ||
> reg.f.sh <- lm(FGPA ~ SATV + HSGPA) | > reg.f.sh <- lm(FGPA ~ SATV + HSGPA) | ||
Line 520: | Line 629: | ||
> res.s <- resid(reg.s.fh) | > res.s <- resid(reg.s.fh) | ||
> | > | ||
- | > reg.h.sf <- lm(SATV ~ FGPA + HSGPA) | + | > reg.h.sf <- lm(HSGPA ~ FGPA + SATV) |
> res.h <- resid(reg.h.sf) | > res.h <- resid(reg.h.sf) | ||
> | > | ||
Line 535: | Line 644: | ||
Residuals: | Residuals: | ||
Min 1Q Median | Min 1Q Median | ||
- | -102.82 -55.69 | + | -13.541 -3.441 |
Coefficients: | Coefficients: | ||
- | Estimate Std. Error t value Pr(> | + | Estimate Std. Error t value Pr(> |
- | (Intercept) | + | (Intercept) |
- | HSGPA 235.039 205.978 1.14 0.297 | + | HSGPA 8.3214 3.8050 2.19 0.034 * |
- | FGPA -282.595 182.188 | + | FGPA |
- | SATV | + | SATV 0.8143 0.0867 9.40 2.8e-12 *** |
--- | --- | ||
Signif. codes: | Signif. codes: | ||
- | Residual standard error: | + | Residual standard error: |
- | Multiple R-squared: | + | Multiple R-squared: |
- | F-statistic: | + | F-statistic: |
> summary(reg.1) | > summary(reg.1) | ||
Line 557: | Line 666: | ||
Residuals: | Residuals: | ||
| | ||
- | -119.6 -88.7 22.9 | + | -21.76 -8.65 -2.08 |
Coefficients: | Coefficients: | ||
Estimate Std. Error t value Pr(> | Estimate Std. Error t value Pr(> | ||
- | (Intercept) | + | (Intercept) |
- | res.f -282.6 185.5 -1.52 0.17 | + | res.f 1.40 5.74 0.24 0.81 |
--- | --- | ||
Signif. codes: | Signif. codes: | ||
- | Residual standard error: | + | Residual standard error: |
- | Multiple R-squared: | + | Multiple R-squared: |
- | F-statistic: | + | F-statistic: |
> summary(reg.2) | > summary(reg.2) | ||
Line 576: | Line 685: | ||
Residuals: | Residuals: | ||
- | | + | |
- | -142.19 -77.25 -2.43 | + | -22.54 -4.94 |
Coefficients: | Coefficients: | ||
Estimate Std. Error t value Pr(> | Estimate Std. Error t value Pr(> | ||
- | (Intercept) | + | (Intercept) |
- | res.s 0.557 0.759 0.73 | + | res.s 0.814 0.148 |
--- | --- | ||
Signif. codes: | Signif. codes: | ||
- | Residual standard error: | + | Residual standard error: |
- | Multiple R-squared: | + | Multiple R-squared: |
- | F-statistic: | + | F-statistic: |
> summary(reg.3) | > summary(reg.3) | ||
Line 596: | Line 705: | ||
Residuals: | Residuals: | ||
- | | + | Min |
- | -142.19 -77.25 -2.43 | + | -22.71 -9.32 |
Coefficients: | Coefficients: | ||
Estimate Std. Error t value Pr(> | Estimate Std. Error t value Pr(> | ||
- | (Intercept) | + | (Intercept) |
- | res.h 0.557 0.759 0.73 0.48 | + | res.h 8.32 8.21 1.01 0.32 |
--- | --- | ||
Signif. codes: | Signif. codes: | ||
- | Residual standard error: | + | Residual standard error: |
- | Multiple R-squared: | + | Multiple R-squared: |
- | F-statistic: | + | F-statistic: |
> | > | ||
- | > | + | > reg.1$coefficient[2] |
- | </ | + | res.f |
- | ---- | + | 1.399 |
- | ---- | + | > reg.2$coefficient[2] |
- | 학자인 A는 GRE점수는 (GREV) 학점에 신경을 쓰는 활동보다는 지능지수와 관련된다고 믿는 SATV의 영향력이 더 클것으로 생각된다. 그래서 SATV만의 영향력을 다른 변인을 콘트롤하여 살펴보고 싶다. | + | res.s |
- | <code> | + | 0.8143 |
- | pcor.test(scholar$GREV, scholar$SATV, | + | > reg.3$coefficient[2] |
- | + | res.h | |
- | reg7 <- lm(GREV ~ HSGPA + FGPA) # run linear regression | + | 8.321 |
- | resid7 <- resid(reg7) | + | |
- | + | ||
- | reg8 <- lm(SATV ~ HSGPA+ FGPA) # second regression | + | |
- | resid8 <- resid(reg8) | + | |
- | + | ||
- | cor(resid7, resid8) | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | < | + | |
- | > pcor.test(scholar$GREV, scholar$SATV, | + | |
- | | + | |
- | 1 0.3179 0.4429 | + | |
> | > | ||
- | > reg7 <- lm(GREV ~ HSGPA + FGPA) # run linear regression | + | > spr.y.f |
- | > resid7 | + | > spr.y.s |
+ | > spr.y.h <- spcor.test(GREV, | ||
> | > | ||
- | > reg8 <- lm(SATV ~ HSGPA+ FGPA) # second regression | + | > spr.y.f$estimate |
- | > resid8 <- resid(reg8) | + | [1] 0.03519 |
+ | > spr.y.s$estimate | ||
+ | [1] 0.6217 | ||
+ | > spr.y.h$estimate | ||
+ | [1] 0.1447 | ||
> | > | ||
- | > cor(resid7, resid8) | + | > spr.y.f$estimate^2 |
- | [1] 0.3179 | + | [1] 0.001238 |
+ | > spr.y.s$estimate^2 | ||
+ | [1] 0.3865 | ||
+ | > spr.y.h$estimate^2 | ||
+ | [1] 0.02094 | ||
> | > | ||
+ | > summary(reg.1)$r.square | ||
+ | [1] 0.001238 | ||
+ | > summary(reg.2)$r.square | ||
+ | [1] 0.3865 | ||
+ | > summary(reg.3)$r.square | ||
+ | [1] 0.02094 | ||
+ | > | ||
+ | > ca <- summary(reg.1)$r.square + | ||
+ | + | ||
+ | + | ||
+ | > # so common explanation area should be | ||
+ | > summary(reg.all)$r.square - ca | ||
+ | [1] 0.39 | ||
> | > | ||
</ | </ | ||
+ | {{: | ||
+ | |||
+ | multiple regression 분석을 보면 독립변인의 coefficient 값은 각각 | ||
+ | * HSGPA | ||
+ | * FGPA 1.3994 | ||
+ | * SATV 0.8143 | ||
+ | 이 기울기에 대해서 t-test를 각각 하여 HSGPA와 FGPA의 설명력이 significant 한지를 확인하였다. 그리고 이 때의 R< | ||
+ | * 0.799 이었다. | ||
+ | 그런데 이 coefficient값은 독립변인 각각의 고유의 설명력을 가지고 (spcor.test(GREV, | ||
+ | 또한 세 독립변인이 공통적으로 설명하는 부분은 | ||
+ | * 0.39 | ||
+ | 임을 알 수 있다. | ||
====== e.g., 독립변인 들이 서로 독립적일 때의 각각의 설명력 ====== | ====== e.g., 독립변인 들이 서로 독립적일 때의 각각의 설명력 ====== | ||
In this example, the two IVs are orthogonal to each other (not correlated with each other). Hence, regress res.y.x2 against x1 would not result in any problem. | In this example, the two IVs are orthogonal to each other (not correlated with each other). Hence, regress res.y.x2 against x1 would not result in any problem. |
partial_and_semipartial_correlation.1728965999.txt.gz · Last modified: 2024/10/15 13:19 by hkimscil