mediation_analysis
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
mediation_analysis [2023/06/03 01:41] – [e.gs] hkimscil | mediation_analysis [2023/06/18 07:42] (current) – hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Mediation Analysis ====== | ====== Mediation Analysis ====== | ||
- | Planned behavior 이론에 따라서 연구자는 아래와 같은 모델을 만들고 데이터를 얻은 후 테스트하려고 한다. 특히 이 단계에서 연구자는 Attitudes가 Behavior 미치는 영향력을 Attitudes 고유의 것과 Intention을 거쳐가는 것으로 구분하여 확인해보려고 한다. | + | Planned behavior 이론에 따라서 연구자는 아래와 같은 모델을 만들고 데이터를 얻은 후 테스트하려고 한다. 특히 이 단계에서 연구자는 Attitudes가 Behavior 미치는 영향력을 Attitudes 고유의 것과 Intention을 거쳐가는 것으로 구분하여 확인해보려고 한다. |
- | 보통 lavaan package를 활용하여 (path analysis를 위해서 개발) | + | |
+ | * 우선 attitudes와 (x 변인) behavior (y 변인) 간의 significant한 관계가 있다. 즉, lm(behavior ~ attitudes) 의 f statistics 값이 significant하다. 이 coefficient값을 c 라고 하자. | ||
+ | < | ||
+ | Attitudes | ||
+ | </ | ||
+ | * 이 attitudes는 intention에도 통계적으로 유의미한 설명력을 갖는다. 이 관계는 a 라는 coefficient값을 갖는다. | ||
+ | < | ||
+ | Attitudes | ||
+ | </ | ||
+ | * 그리고 beahvior에 대해서 attitudes와 intention 두 변인을 이용한 regression의 R square값은 significant한 설명력을 갖는다. 그러나, 각 변인 중 애초에 독립적으로 사용되었을 때 significant했던 attitudes는 설명력을 잃거나 혹은 significant한 설명력을 갖지만 그 크기가 c보다 작다. 즉, 아래에서 | ||
+ | * b = significant | ||
+ | * c' = not significant 혹은 c' < c 즉, attitudes의 설명력이 significant한 상태를 유지하지만 영향력은 감소 | ||
+ | < | ||
+ | |||
+ | | ||
+ | \ | ||
+ | \ b | ||
+ | \ | ||
+ | Attitudes | ||
+ | |||
+ | b | ||
+ | c' < c | ||
+ | </ | ||
+ | |||
+ | 이 때 연구자는 attitudes의 설명력이 Intentin을 통해서 behavior에 가게 되는게 아닌가 가정을 하고 이를 확인하게 되는데, 이것이 attitudes의 intention을 통한 moderating 혹은 mediated effect이다. | ||
+ | |||
+ | attitudes가 intention을 통해서 어떤 크기의 영향력을 갖는가? 즉, attitudes의 매개효과의 크기는 어느정도인가가 궁금할 수 있는데 크게는 두 가지가 있다. 첫번째는 c - c' 의 크기를 indirect effect로 보는 경우이다. c는 attitudes가 온전히 (다른 변인 고려 없이) behavior에 미치는 영향력인데, | ||
+ | |||
+ | 다른 하나는 세 변인을 동시에 고려하여 attitudes의 단위변화가 최종적으로 behavior에 어떻게 영향을 미치는가를 본다. 만약에 a의 beta 크기가 2라고 하고, b의 beta 크기가 3이라고 하면, 우리는 attitudes의 단위가 한단위 변하면 intention의 단위가 2단위 변한다는 것을 알고, 이를 두번째 단계에 (b의 크기) 적용하면 2단위가 변하였으므로 behavior는 3x2단위 (6단위) 변하게 될 것이라고 추측할 수 있다. 즉, attitudes의 한단위는 intention의 매개효과를 통하여 behavior가 6단위 변하도록 한다 (a beta coefficient * b beta coefficient). | ||
+ | |||
+ | size of mediated effect | ||
+ | '' | ||
+ | |||
+ | |||
+ | 이런 것들은 일반 regression을 이용하여 알아낼 수도 있지만, | ||
< | < | ||
Line 17: | Line 51: | ||
또한 mediation analysis에서 독립변인들의 효과를 (설명력을) 직접효과와 간접효과로 나눌 수 있는데 직접효과는 a, b, 그리고 c'를 직접효과라고 (direct effects) 하고 a와 b를 거쳐서 가는 효과를 간접효과라고 (indirect effects) 한다. Indirect effects 의 크기를 어떻게 측정하는가에는 여러가지 방법이 있을 수 있지만, 가장 많이 쓰이는 방법으로는 | 또한 mediation analysis에서 독립변인들의 효과를 (설명력을) 직접효과와 간접효과로 나눌 수 있는데 직접효과는 a, b, 그리고 c'를 직접효과라고 (direct effects) 하고 a와 b를 거쳐서 가는 효과를 간접효과라고 (indirect effects) 한다. Indirect effects 의 크기를 어떻게 측정하는가에는 여러가지 방법이 있을 수 있지만, 가장 많이 쓰이는 방법으로는 | ||
* a path와 b path의 coefficient값을 곱한 값을 취하는 방법이다 | * a path와 b path의 coefficient값을 곱한 값을 취하는 방법이다 | ||
- | * 다른 방법으로는 | + | * 다른 방법으로는 |
위에서 a b 를 곱해서 간접효과를 측정할 때에 그 값이 (효과가) significant한지 알아보기 위한 테스트에는 두 가지 방법이 있을 수 있는데 | 위에서 a b 를 곱해서 간접효과를 측정할 때에 그 값이 (효과가) significant한지 알아보기 위한 테스트에는 두 가지 방법이 있을 수 있는데 | ||
Line 966: | Line 1000: | ||
====== e.g. 2 ====== | ====== e.g. 2 ====== | ||
+ | https:// | ||
+ | < | ||
+ | #################################### | ||
+ | nlsy <- read.csv(" | ||
+ | attach(nlsy) | ||
+ | # install.packages(" | ||
+ | library(bmem) | ||
+ | library(sem) | ||
+ | # install.packages(" | ||
+ | library(cfa) | ||
+ | </ | ||
+ | < | ||
+ | ########################## | ||
+ | nlsy.model< | ||
+ | math =b*HE + cp*ME | ||
+ | HE = a*ME | ||
+ | < | ||
+ | effects< | ||
+ | nlsy.res< | ||
+ | ########################## | ||
+ | </ | ||
+ | < | ||
+ | m.me.he <- lm(ME~HE) | ||
+ | m.math.me <- lm(math~ME) | ||
+ | m.math.he <- lm(math~HE) | ||
+ | m.math.mehe <- lm(math~ME+HE) | ||
+ | m.he.me <- lm(HE~ME) | ||
+ | summary(m.he.me) | ||
+ | res.m.he.me <- resid(m.he.me) | ||
+ | m.temp <- lm(math~res.m.he.me) | ||
+ | summary(m.temp) | ||
+ | res.m.me.he <- resid(m.me.he) | ||
+ | m.temp2 <- lm(math~res.m.me.he) | ||
+ | summary(m.temp2) | ||
+ | |||
+ | library(lavaan) | ||
+ | specmod <- " | ||
+ | # path c' (direct effect) | ||
+ | math ~ c*ME | ||
+ | |||
+ | # path a | ||
+ | HE ~ a*ME | ||
+ | |||
+ | # path b | ||
+ | math ~ b*HE | ||
+ | |||
+ | # indirect effect (a*b) | ||
+ | # sobel test (Delta method) | ||
+ | ab := a*b | ||
+ | " | ||
+ | |||
+ | # fit/ | ||
+ | fitmod <- sem(specmod, | ||
+ | |||
+ | # summarize/ | ||
+ | summary(fitmod, | ||
+ | |||
+ | # for a | ||
+ | summary(m.he.me) | ||
+ | # for b | ||
+ | summary(m.temp) | ||
+ | # for cprime | ||
+ | summary(m.temp2) | ||
+ | |||
+ | a <- summary(m.he.me)$coefficient[2] # a | ||
+ | b <- summary(m.temp)$coefficient[2] | ||
+ | c <- summary(m.temp2)$coefficient[2] # c | ||
+ | a | ||
+ | b | ||
+ | c | ||
+ | a*b | ||
+ | |||
+ | c2 <- summary(fitmod)$pe$est[1] | ||
+ | a2 <- summary(fitmod)$pe$est[2] | ||
+ | b2 <- summary(fitmod)$pe$est[3] | ||
+ | ab2 <- summary(fitmod)$pe$est[7] | ||
+ | a2 | ||
+ | b2 | ||
+ | c2 | ||
+ | ab2 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > #################################### | ||
+ | > nlsy <- read.csv(" | ||
+ | > attach(nlsy) | ||
+ | The following objects are masked from nlsy (pos = 3): | ||
+ | |||
+ | HE, math, ME | ||
+ | |||
+ | The following objects are masked from nlsy (pos = 5): | ||
+ | |||
+ | HE, math, ME | ||
+ | |||
+ | The following objects are masked from df: | ||
+ | |||
+ | HE, math, ME | ||
+ | |||
+ | > # install.packages(" | ||
+ | > library(bmem) | ||
+ | > library(sem) | ||
+ | > # install.packages(" | ||
+ | > library(cfa) | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > nlsy.model< | ||
+ | 1: math =b*HE + cp*ME | ||
+ | 2: HE = a*ME | ||
+ | 3: | ||
+ | Read 2 items | ||
+ | NOTE: adding 3 variances to the model | ||
+ | > effects< | ||
+ | > nlsy.res< | ||
+ | | ||
+ | b 0.46450283 0.14304860 | ||
+ | cp | ||
+ | a 0.13925694 0.04292438 | ||
+ | V[HE] 2.73092635 0.20078170 13.601471 0.000000e+00 | ||
+ | V[math] 20.67659134 1.52017323 13.601471 0.000000e+00 | ||
+ | V[ME] 4.00590078 0.29451968 13.601471 0.000000e+00 | ||
+ | a*b 0.06468524 0.02818457 | ||
+ | cp+a*b | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > m.me.he <- lm(ME~HE) | ||
+ | > m.math.me <- lm(math~ME) | ||
+ | > m.math.he <- lm(math~HE) | ||
+ | > m.math.mehe <- lm(math~ME+HE) | ||
+ | > m.he.me <- lm(HE~ME) | ||
+ | > summary(m.he.me) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = HE ~ ME) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -5.5020 -0.7805 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | ME | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.655 on 369 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > res.m.he.me <- resid(m.he.me) | ||
+ | > m.temp <- lm(math~res.m.he.me) | ||
+ | > summary(m.temp) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = math ~ res.m.he.me) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -12.4263 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.m.he.me | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 4.674 on 369 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > res.m.me.he <- resid(m.me.he) | ||
+ | > m.temp2 <- lm(math~res.m.me.he) | ||
+ | > summary(m.temp2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = math ~ res.m.me.he) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -14.1938 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.m.me.he | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 4.648 on 369 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > library(lavaan) | ||
+ | > specmod <- " | ||
+ | + # path c' (direct effect) | ||
+ | + math ~ c*ME | ||
+ | + | ||
+ | + # path a | ||
+ | + HE ~ a*ME | ||
+ | + | ||
+ | + # path b | ||
+ | + math ~ b*HE | ||
+ | + | ||
+ | + # indirect effect (a*b) | ||
+ | + # sobel test (Delta method) | ||
+ | + ab := a*b | ||
+ | + " | ||
+ | > | ||
+ | > # fit/ | ||
+ | > fitmod <- sem(specmod, | ||
+ | > | ||
+ | > # summarize/ | ||
+ | > summary(fitmod, | ||
+ | lavaan 0.6-12 ended normally after 1 iterations | ||
+ | |||
+ | Estimator | ||
+ | Optimization method | ||
+ | Number of model parameters | ||
+ | |||
+ | Number of observations | ||
+ | |||
+ | Model Test User Model: | ||
+ | | ||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | |||
+ | Model Test Baseline Model: | ||
+ | |||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | P-value | ||
+ | |||
+ | User Model versus Baseline Model: | ||
+ | |||
+ | Comparative Fit Index (CFI) 1.000 | ||
+ | Tucker-Lewis Index (TLI) 1.000 | ||
+ | |||
+ | Loglikelihood and Information Criteria: | ||
+ | |||
+ | Loglikelihood user model (H0) -1800.092 | ||
+ | Loglikelihood unrestricted model (H1) -1800.092 | ||
+ | | ||
+ | Akaike (AIC) 3610.184 | ||
+ | Bayesian (BIC) 3629.765 | ||
+ | Sample-size adjusted Bayesian (BIC) | ||
+ | |||
+ | Root Mean Square Error of Approximation: | ||
+ | |||
+ | RMSEA 0.000 | ||
+ | 90 Percent confidence interval - lower 0.000 | ||
+ | 90 Percent confidence interval - upper 0.000 | ||
+ | P-value RMSEA <= 0.05 NA | ||
+ | |||
+ | Standardized Root Mean Square Residual: | ||
+ | |||
+ | SRMR 0.000 | ||
+ | |||
+ | Parameter Estimates: | ||
+ | |||
+ | Standard errors | ||
+ | Information | ||
+ | Information saturated (h1) model Structured | ||
+ | |||
+ | Regressions: | ||
+ | | ||
+ | math ~ | ||
+ | ME | ||
+ | HE ~ | ||
+ | ME | ||
+ | math ~ | ||
+ | HE | ||
+ | |||
+ | Variances: | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | R-Square: | ||
+ | | ||
+ | math 0.076 | ||
+ | HE 0.028 | ||
+ | |||
+ | Defined Parameters: | ||
+ | | ||
+ | ab 0.065 0.028 2.298 0.022 | ||
+ | |||
+ | > | ||
+ | > # for a | ||
+ | > summary(m.he.me) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = HE ~ ME) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -5.5020 -0.7805 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | ME | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.655 on 369 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > # for b | ||
+ | > summary(m.temp) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = math ~ res.m.he.me) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -12.4263 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.m.he.me | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 4.674 on 369 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > # for cprime | ||
+ | > summary(m.temp2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = math ~ res.m.me.he) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -14.1938 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.m.me.he | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 4.648 on 369 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > a <- summary(m.he.me)$coefficient[2] # a | ||
+ | > b <- summary(m.temp)$coefficient[2] | ||
+ | > c <- summary(m.temp2)$coefficient[2] # c | ||
+ | > a | ||
+ | [1] 0.1392569 | ||
+ | > b | ||
+ | [1] 0.4645028 | ||
+ | > c | ||
+ | [1] 0.4628148 | ||
+ | > a*b | ||
+ | [1] 0.06468524 | ||
+ | > | ||
+ | > c2 <- summary(fitmod)$pe$est[1] | ||
+ | > a2 <- summary(fitmod)$pe$est[2] | ||
+ | > b2 <- summary(fitmod)$pe$est[3] | ||
+ | > ab2 <- summary(fitmod)$pe$est[7] | ||
+ | > a2 | ||
+ | [1] 0.1392569 | ||
+ | > b2 | ||
+ | [1] 0.4645028 | ||
+ | > c2 | ||
+ | [1] 0.4628148 | ||
+ | > ab2 | ||
+ | [1] 0.06468524 | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | ====== temp ====== | ||
+ | < | ||
+ | tests <- read.csv(" | ||
+ | colnames(tests) <- c(" | ||
+ | tests <- subset(tests, | ||
+ | attach(tests) | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | lm.gpa.clepsat <- lm(gpa ~ clep + sat, data = tests) | ||
+ | summary(lm.gpa.clepsat) | ||
+ | |||
+ | lm.gpa.clep <- lm(gpa ~ clep) | ||
+ | lm.gpa.sat <- lm(gpa ~ sat) | ||
+ | summary(lm.gpa.clep) | ||
+ | summary(lm.gpa.sat) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > lm.gpa.clepsat <- lm(gpa ~ clep + sat, data = tests) | ||
+ | > summary(lm.gpa.clepsat) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = gpa ~ clep + sat, data = tests) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -0.197888 -0.128974 -0.000528 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | clep | ||
+ | sat | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.1713 on 7 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | > | ||
+ | </ | ||
+ | 0.7778 이 두 변인 clep 과 sat 가 gpa를 설명하는 부분 | ||
+ | '' | ||
+ | |||
+ | 그리고, 위에서 sat의 설명력은 significant하지 않음 | ||
+ | 그럼 sat만으로 gpa를 보면? | ||
+ | < | ||
+ | > lm.gpa.clep <- lm(gpa ~ clep) | ||
+ | > lm.gpa.sat <- lm(gpa ~ sat) | ||
+ | > summary(lm.gpa.clep) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = gpa ~ clep) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -0.190496 -0.141167 -0.002376 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | clep | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.1637 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > summary(lm.gpa.sat) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = gpa ~ sat) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -0.23544 -0.12184 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) 1.7848101 | ||
+ | sat | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.2365 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | </ | ||
+ | |||
+ | 위에서처럼 significant함. | ||
+ | '' | ||
+ | '' | ||
+ | |||
+ | 그렇다면 sat의 영향력은 clep을 매개로 해서 나타나는가를 보기 위해서 | ||
+ | |||
+ | < | ||
+ | > lm.clep.sat <- lm(clep ~ sat) | ||
+ | > summary(lm.clep.sat) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = clep ~ sat) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -2.5316 -1.2437 -0.2848 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) 8.556962 | ||
+ | sat | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 2.386 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | res.lm.clep.sat <- resid(lm.clep.sat) | ||
+ | reg.lm.clep.sat <- predict(lm.clep.sat)-mean(clep) | ||
+ | lm.gpa.sat.mediated.via.clep <- lm(gpa~reg.lm.clep.sat) | ||
+ | lm.gpa.clep.alone <- lm(gpa~res.lm.clep.sat) | ||
+ | |||
+ | summary(lm.gpa.sat.mediated.via.clep) | ||
+ | summary(lm.gpa.clep.alone) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > res.lm.clep.sat <- resid(lm.clep.sat) | ||
+ | > reg.lm.clep.sat <- predict(lm.clep.sat)-mean(clep) | ||
+ | > lm.gpa.sat.mediated.via.clep <- lm(gpa~reg.lm.clep.sat) | ||
+ | > lm.gpa.clep.alone <- lm(gpa~res.lm.clep.sat) | ||
+ | > | ||
+ | > summary(lm.gpa.sat.mediated.via.clep) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = gpa ~ reg.lm.clep.sat) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -0.23544 -0.12184 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | reg.lm.clep.sat | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.2365 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > summary(lm.gpa.clep.alone) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = gpa ~ res.lm.clep.sat) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -0.34523 -0.23300 -0.04416 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.lm.clep.sat | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.2919 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | </ | ||
+ | sat의 영향력 mediated via clep = Multiple R-squared: | ||
+ | sat의 영향력을 제외한 celp만의 설명력 = Multiple R-squared: | ||
+ | |||
+ | ====== temp2 ====== | ||
+ | < | ||
+ | > ############################# | ||
+ | > ############################# | ||
+ | > # https:// | ||
+ | > # Download data online. This is a simulated dataset for this post. | ||
+ | > myData <- read.csv(' | ||
+ | > | ||
+ | > model.0 <- lm(Y ~ X, myData) | ||
+ | > summary(model.0) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = Y ~ X, data = myData) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -5.0262 -1.2340 -0.3282 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | X | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.929 on 98 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > # c line의 coef 값 = 0.396 at p-level = 0.0006 | ||
+ | > | ||
+ | > model.M <- lm(M ~ X, myData) | ||
+ | > summary(model.M) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = M ~ X, data = myData) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -4.3046 -0.8656 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | X 0.56102 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.639 on 98 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > # a line = 0.561 p = 0.0 | ||
+ | > | ||
+ | > model.Y <- lm(Y ~ X + M, myData) | ||
+ | > summary(model.Y) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = Y ~ X + M, data = myData) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -3.7631 -1.2393 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | X | ||
+ | M | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.631 on 97 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > ################################################# | ||
+ | > ## b line + c' line (c에서 M이 추가되었으므로) | ||
+ | > ## c' = 0.0396 --- | ||
+ | > ## b = 0.6355 *** | ||
+ | > ################################################# | ||
+ | > | ||
+ | > library(mediation) | ||
+ | > results <- mediate(model.M, | ||
+ | + treat=' | ||
+ | + mediator=' | ||
+ | + boot=TRUE, sims=500) | ||
+ | Running nonparametric bootstrap | ||
+ | |||
+ | > summary(results) | ||
+ | |||
+ | Causal Mediation Analysis | ||
+ | |||
+ | Nonparametric Bootstrap Confidence Intervals with the Percentile Method | ||
+ | |||
+ | | ||
+ | ACME | ||
+ | ADE 0.0396 | ||
+ | Total Effect | ||
+ | Prop. Mediated | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Sample Size Used: 100 | ||
+ | |||
+ | |||
+ | Simulations: | ||
+ | |||
+ | > | ||
+ | > # OR with lavaan | ||
+ | > library(lavaan) | ||
+ | > specmod <- " | ||
+ | + # path c' (direct effect) | ||
+ | + Y ~ c*X | ||
+ | + | ||
+ | + # path a | ||
+ | + M ~ a*X | ||
+ | + | ||
+ | + # path b | ||
+ | + Y ~ b*M | ||
+ | + | ||
+ | + # indirect effect (a*b) | ||
+ | + # sobel test (Delta method) | ||
+ | + ab := a*b | ||
+ | + " | ||
+ | > | ||
+ | > # fit/ | ||
+ | > fitmod <- sem(specmod, | ||
+ | > | ||
+ | > # summarize/ | ||
+ | > summary(fitmod, | ||
+ | lavaan 0.6-12 ended normally after 1 iterations | ||
+ | |||
+ | Estimator | ||
+ | Optimization method | ||
+ | Number of model parameters | ||
+ | |||
+ | Number of observations | ||
+ | |||
+ | Model Test User Model: | ||
+ | | ||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | |||
+ | Model Test Baseline Model: | ||
+ | |||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | P-value | ||
+ | |||
+ | User Model versus Baseline Model: | ||
+ | |||
+ | Comparative Fit Index (CFI) 1.000 | ||
+ | Tucker-Lewis Index (TLI) 1.000 | ||
+ | |||
+ | Loglikelihood and Information Criteria: | ||
+ | |||
+ | Loglikelihood user model (H0) | ||
+ | Loglikelihood unrestricted model (H1) | ||
+ | | ||
+ | Akaike (AIC) | ||
+ | Bayesian (BIC) | ||
+ | Sample-size adjusted Bayesian (BIC) 766.459 | ||
+ | |||
+ | Root Mean Square Error of Approximation: | ||
+ | |||
+ | RMSEA 0.000 | ||
+ | 90 Percent confidence interval - lower 0.000 | ||
+ | 90 Percent confidence interval - upper 0.000 | ||
+ | P-value RMSEA <= 0.05 NA | ||
+ | |||
+ | Standardized Root Mean Square Residual: | ||
+ | |||
+ | SRMR 0.000 | ||
+ | |||
+ | Parameter Estimates: | ||
+ | |||
+ | Standard errors | ||
+ | Information | ||
+ | Information saturated (h1) model Structured | ||
+ | |||
+ | Regressions: | ||
+ | | ||
+ | Y ~ | ||
+ | X (c) 0.040 0.108 0.367 0.714 | ||
+ | M ~ | ||
+ | X (a) 0.561 0.094 5.998 0.000 | ||
+ | Y ~ | ||
+ | M (b) 0.635 0.099 6.418 0.000 | ||
+ | |||
+ | Variances: | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | R-Square: | ||
+ | | ||
+ | Y 0.373 | ||
+ | M 0.265 | ||
+ | |||
+ | Defined Parameters: | ||
+ | | ||
+ | ab 0.357 0.081 4.382 0.000 | ||
+ | |||
+ | > | ||
+ | > # Resampling method | ||
+ | > set.seed(2019) | ||
+ | > | ||
+ | > fitmod <- sem(specmod, | ||
+ | > summary(fitmod, | ||
+ | lavaan 0.6-12 ended normally after 1 iterations | ||
+ | |||
+ | Estimator | ||
+ | Optimization method | ||
+ | Number of model parameters | ||
+ | |||
+ | Number of observations | ||
+ | |||
+ | Model Test User Model: | ||
+ | | ||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | |||
+ | Model Test Baseline Model: | ||
+ | |||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | P-value | ||
+ | |||
+ | User Model versus Baseline Model: | ||
+ | |||
+ | Comparative Fit Index (CFI) 1.000 | ||
+ | Tucker-Lewis Index (TLI) 1.000 | ||
+ | |||
+ | Loglikelihood and Information Criteria: | ||
+ | |||
+ | Loglikelihood user model (H0) | ||
+ | Loglikelihood unrestricted model (H1) | ||
+ | | ||
+ | Akaike (AIC) | ||
+ | Bayesian (BIC) | ||
+ | Sample-size adjusted Bayesian (BIC) 766.459 | ||
+ | |||
+ | Root Mean Square Error of Approximation: | ||
+ | |||
+ | RMSEA 0.000 | ||
+ | 90 Percent confidence interval - lower 0.000 | ||
+ | 90 Percent confidence interval - upper 0.000 | ||
+ | P-value RMSEA <= 0.05 NA | ||
+ | |||
+ | Standardized Root Mean Square Residual: | ||
+ | |||
+ | SRMR 0.000 | ||
+ | |||
+ | Parameter Estimates: | ||
+ | |||
+ | Standard errors | ||
+ | Number of requested bootstrap draws 1000 | ||
+ | Number of successful bootstrap draws 1000 | ||
+ | |||
+ | Regressions: | ||
+ | | ||
+ | Y ~ | ||
+ | X (c) 0.040 0.124 0.319 0.750 | ||
+ | M ~ | ||
+ | X (a) 0.561 0.095 5.888 0.000 | ||
+ | Y ~ | ||
+ | M (b) 0.635 0.102 6.224 0.000 | ||
+ | |||
+ | Variances: | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | R-Square: | ||
+ | | ||
+ | Y 0.373 | ||
+ | M 0.265 | ||
+ | |||
+ | Defined Parameters: | ||
+ | | ||
+ | ab 0.357 0.080 4.442 0.000 | ||
+ | |||
+ | > parameterEstimates(fitmod, | ||
+ | lhs op rhs label | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 Y ~~ | ||
+ | 5 M ~~ | ||
+ | 6 X ~~ | ||
+ | 7 ab := a*b ab 0.357 0.080 4.442 | ||
+ | > | ||
+ | > summary(lm(Y~X+M, | ||
+ | [1] 0.3729992 | ||
+ | > summary(lm(M~X, | ||
+ | [1] 0.2645879 | ||
+ | > var(myData$Y) | ||
+ | [1] 4.158687 | ||
+ | > var(myData$M) | ||
+ | [1] 3.616566 | ||
+ | > var(myData$X) | ||
+ | [1] 3.040303 | ||
+ | > | ||
+ | </ |
mediation_analysis.1685724094.txt.gz · Last modified: 2023/06/03 01:41 by hkimscil