r:path_analysis
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
r:path_analysis [2022/11/15 13:00] – hkimscil | r:path_analysis [2023/11/27 16:57] (current) – [Lavaan in R: explanation] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Path Analysis ====== | ====== Path Analysis ====== | ||
- | ===== Lavaan 2 ===== | + | {{:r:pasted:20230529-234519.png}} |
- | + | ====== | |
- | + | ||
- | {{youtube> | + | |
- | < | + | |
- | model <- ' | + | |
- | # labeling path from mastery to interest | + | |
- | interest ~ a*mastery + perfgoal + ses | + | |
- | + | ||
- | # labeling path from interest to achieve. | + | |
- | # Adding labeled path from | + | |
- | # mastery to achieve | + | |
- | achieve ~ e*anxiety + b*interest + c*mastery | + | |
- | + | ||
- | # predicting anxiety and labeling path from mastery | + | |
- | anxiety ~ perfgoal + d*mastery | + | |
- | # estimtating the variances and covariances of | + | |
- | # the exogenous variables (ses, mastery, | + | |
- | mastery~~mastery | + | |
- | perfgoal~~perfgoal | + | |
- | ses~~ses | + | |
- | + | ||
- | mastery~~perfgoal+ses | + | |
- | perfgoal~~ses | + | |
- | + | ||
- | # estimating the variances of residuals | + | |
- | # for endogenous variables | + | |
- | # (interest, anxiety, achieve) | + | |
- | interest~~interest | + | |
- | anxiety~~anxiety | + | |
- | achieve~~achieve | + | |
- | + | ||
- | # estimating the covariance of residuals | + | |
- | # for interest and anxiety | + | |
- | interest~~anxiety | + | |
- | # calculating specific indirect effect | + | |
- | # of mastery on achieve via interest | + | |
- | SIE1:=a*b | + | |
- | # calculating specific indirect effect of | + | |
- | # mastery on achieve via anxiety | + | |
- | SIE2:=d*e | + | |
- | # calculating total indirect effect of | + | |
- | # mastery on achievement via mediators | + | |
- | TIE:=SIE1+SIE2 | + | |
- | # calculating total effect of mastery on achieve | + | |
- | TE: | + | |
- | + | ||
- | # using naive bootstrap to obtain standard errors | + | |
- | fit <- sem(model, data=processdata, | + | |
- | summary(fit, | + | |
- | + | ||
- | # using ' | + | |
- | # us confidence intervals based on naive bootstrap. | + | |
- | # A standard approach to testing indirect effects. | + | |
- | parameterEstimates(fit) | + | |
- | + | ||
- | </ | + | |
- | ---- | + | |
- | ===== Lavaan 3: Testing data normality ===== | + | |
- | + | ||
- | {{youtube> | + | |
- | < | + | |
- | processdata <- read.csv(" | + | |
- | str(processdata) | + | |
- | # install.packages(" | + | |
- | library(MVN) | + | |
- | newdata <- processdata[c(" | + | |
- | str(newdata) | + | |
- | </ | + | |
- | Use the ' | + | |
- | + | ||
- | Multivariate normality is evidenced by p-values associated with multivariate skewness and kurtosis statistics that are > .05. In those cases where both the skewness and kurtosis results are non-significant (p's > .05), then the data are assumed to follow a multivariate normal distribution where p > .05 (Korkmaz, Goksuluk, & Zarasiz, 2014, 2019). | + | |
- | + | ||
- | You can also use plots to explore possible multivariate outliers. Moreover, you can examine univariate tests of normality (the default is Shapiro-Wilk test, but can be changed if desired). A significant test result regarding a specific variable indicates a significant departure from normality. | + | |
- | < | + | |
- | mvn(newdata, | + | |
- | mvn(newdata, | + | |
- | mvn(newdata, | + | |
- | </ | + | |
- | + | ||
- | You can generate univariate plot as well to evaluate distribution of the endogenous variables for non-normality. Skewness values approaching 2 or kurtoisis values over 7 may be considered indicative of more " | + | |
- | < | + | |
- | mvn(newdata, | + | |
- | mvn(newdata, | + | |
- | + | ||
- | model <- ' | + | |
- | interest ~ mastery + perfgoal + ses | + | |
- | achieve ~ anxiety + interest + mastery | + | |
- | anxiety ~ perfgoal + mastery | + | |
- | # variances | + | |
- | mastery ~~ mastery | + | |
- | perfgoal ~~ perfgoal | + | |
- | ses ~~ ses | + | |
- | + | ||
- | mastery ~~ perfgoal + ses | + | |
- | perfgoal ~~ ses | + | |
- | + | ||
- | interest ~~ interest | + | |
- | anxiety ~~ anxiety | + | |
- | achieve ~~ achieve | + | |
- | interest~~anxiety | + | |
- | ' | + | |
- | </ | + | |
- | We will fit the model using the ' | + | |
- | < | + | |
- | fit <- sem(model, data=processdata, | + | |
- | summary(fit, | + | |
- | </ | + | |
- | ---- | + | |
- | reference | + | |
- | {{youtube> | + | |
- | + | ||
- | see [[https:// | + | |
- | + | ||
- | ====== | + | |
{{youtube> | {{youtube> | ||
Line 224: | Line 111: | ||
</ | </ | ||
+ | ---- | ||
+ | < | ||
+ | # my own | ||
+ | # pbt model | ||
+ | specmod5 <- ' | ||
+ | # Directional relations (path) | ||
+ | intention ~ a*attitude + b*norms + c*control | ||
+ | behavior ~ d*intention | ||
+ | # Covariances | ||
+ | attitude ~~ norms + control | ||
+ | norms ~~ control | ||
+ | ad := a*d | ||
+ | bd := b*d | ||
+ | cd := c*d | ||
+ | ' | ||
+ | fitmod5 <- sem(specmod5, | ||
+ | summary(fitmod5, | ||
+ | </ | ||
====== Output ====== | ====== Output ====== | ||
< | < | ||
Line 553: | Line 458: | ||
</ | </ | ||
+ | ===== specmod5 ===== | ||
+ | < | ||
+ | > specmod5 <- " | ||
+ | + # Directional relations (path) | ||
+ | + | ||
+ | + | ||
+ | + # Covariances | ||
+ | + | ||
+ | + norms ~~ control | ||
+ | + " | ||
+ | > fitmod5 <- sem(specmod5, | ||
+ | > summary(fitmod5, | ||
+ | lavaan 0.6-12 ended normally after 18 iterations | ||
+ | |||
+ | Estimator | ||
+ | Optimization method | ||
+ | Number of model parameters | ||
+ | |||
+ | Number of observations | ||
+ | |||
+ | Model Test User Model: | ||
+ | | ||
+ | Test statistic | ||
+ | Degrees of freedom | ||
+ | P-value (Chi-square) | ||
+ | |||
+ | 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.006 | ||
+ | |||
+ | Loglikelihood and Information Criteria: | ||
+ | |||
+ | Loglikelihood user model (H0) -1258.396 | ||
+ | Loglikelihood unrestricted model (H1) -1257.506 | ||
+ | | ||
+ | Akaike (AIC) 2542.792 | ||
+ | Bayesian (BIC) 2585.605 | ||
+ | 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.136 | ||
+ | P-value RMSEA <= 0.05 0.569 | ||
+ | |||
+ | Standardized Root Mean Square Residual: | ||
+ | |||
+ | SRMR 0.018 | ||
+ | |||
+ | Parameter Estimates: | ||
+ | |||
+ | Standard errors | ||
+ | Information | ||
+ | Information saturated (h1) model Structured | ||
+ | |||
+ | Regressions: | ||
+ | | ||
+ | intention ~ | ||
+ | attitude | ||
+ | norms | ||
+ | control | ||
+ | behavior ~ | ||
+ | intention | ||
+ | norms | ||
+ | |||
+ | Covariances: | ||
+ | | ||
+ | attitude ~~ | ||
+ | norms | ||
+ | control | ||
+ | norms ~~ | ||
+ | control | ||
+ | |||
+ | Variances: | ||
+ | | ||
+ | | ||
+ | | ||
+ | attitude | ||
+ | norms | ||
+ | control | ||
+ | |||
+ | R-Square: | ||
+ | | ||
+ | intention | ||
+ | behavior | ||
+ | |||
+ | </ | ||
+ | |||
===== Lavaan in R: explanation ===== | ===== Lavaan in R: explanation ===== | ||
Line 572: | Line 573: | ||
< | < | ||
- | processdata< | + | # processdata< |
+ | processdata< | ||
+ | | ||
</ | </ | ||
Line 630: | Line 633: | ||
* Step 2: Use ' | * Step 2: Use ' | ||
< | < | ||
- | fit< | + | fit< |
</ | </ | ||
* The ' | * The ' | ||
< | < | ||
- | summary(fit, | + | summary(fit, |
</ | </ | ||
* To obtain standardized estimates, use the ' | * To obtain standardized estimates, use the ' | ||
< | < | ||
- | summary(fit, | + | summary(fit, |
</ | </ | ||
Line 688: | Line 691: | ||
interest~~anxiety' | interest~~anxiety' | ||
- | fit< | + | fit< |
- | summary(fit, | + | summary(fit, |
</ | </ | ||
Line 748: | Line 751: | ||
CODING | CODING | ||
< | < | ||
- | processdata <- read.csv(" | + | processdata< |
+ | | ||
str(processdata) | str(processdata) | ||
library(lavaan) | library(lavaan) | ||
Line 768: | Line 772: | ||
interest~~anxiety | interest~~anxiety | ||
' | ' | ||
- | fit <- lavaan(model. data=processdata) | + | fit <- lavaan(model, data=processdata) |
- | fit <- sem(model. data=processdata) | + | fit <- sem(model, data=processdata) |
summary(fit, | summary(fit, | ||
Line 797: | Line 801: | ||
</ | </ | ||
---- | ---- | ||
+ | ===== Lavaan 2 ===== | ||
+ | |||
+ | |||
+ | {{youtube> | ||
+ | < | ||
+ | model <- ' | ||
+ | # labeling path from mastery to interest | ||
+ | interest ~ a*mastery + perfgoal + ses | ||
+ | |||
+ | # labeling path from interest to achieve. | ||
+ | # Adding labeled path from | ||
+ | # mastery to achieve | ||
+ | achieve ~ e*anxiety + b*interest + c*mastery | ||
+ | | ||
+ | # predicting anxiety and labeling path from mastery | ||
+ | anxiety ~ perfgoal + d*mastery | ||
+ | # estimtating the variances and covariances of | ||
+ | # the exogenous variables (ses, mastery, | ||
+ | mastery~~mastery | ||
+ | perfgoal~~perfgoal | ||
+ | ses~~ses | ||
+ | |||
+ | mastery~~perfgoal+ses | ||
+ | perfgoal~~ses | ||
+ | |||
+ | # estimating the variances of residuals | ||
+ | # for endogenous variables | ||
+ | # (interest, anxiety, achieve) | ||
+ | interest~~interest | ||
+ | anxiety~~anxiety | ||
+ | achieve~~achieve | ||
+ | |||
+ | # estimating the covariance of residuals | ||
+ | # for interest and anxiety | ||
+ | interest~~anxiety | ||
+ | # calculating specific indirect effect | ||
+ | # of mastery on achieve via interest | ||
+ | SIE1:=a*b | ||
+ | # calculating specific indirect effect of | ||
+ | # mastery on achieve via anxiety | ||
+ | SIE2:=d*e | ||
+ | # calculating total indirect effect of | ||
+ | # mastery on achievement via mediators | ||
+ | TIE: | ||
+ | # calculating total effect of mastery on achieve | ||
+ | TE: | ||
+ | | ||
+ | # using naive bootstrap to obtain standard errors | ||
+ | fit <- sem(model, data=processdata, | ||
+ | summary(fit, | ||
+ | | ||
+ | # using ' | ||
+ | # us confidence intervals based on naive bootstrap. | ||
+ | # A standard approach to testing indirect effects. | ||
+ | parameterEstimates(fit) | ||
+ | |||
+ | </ | ||
+ | ---- | ||
+ | ===== Lavaan 3: Testing data normality ===== | ||
+ | |||
+ | {{youtube> | ||
+ | < | ||
+ | processdata <- read.csv(" | ||
+ | str(processdata) | ||
+ | # install.packages(" | ||
+ | library(MVN) | ||
+ | newdata <- processdata[c(" | ||
+ | str(newdata) | ||
+ | </ | ||
+ | Use the ' | ||
+ | |||
+ | Multivariate normality is evidenced by p-values associated with multivariate skewness and kurtosis statistics that are > .05. In those cases where both the skewness and kurtosis results are non-significant (p's > .05), then the data are assumed to follow a multivariate normal distribution where p > .05 (Korkmaz, Goksuluk, & Zarasiz, 2014, 2019). | ||
+ | |||
+ | You can also use plots to explore possible multivariate outliers. Moreover, you can examine univariate tests of normality (the default is Shapiro-Wilk test, but can be changed if desired). A significant test result regarding a specific variable indicates a significant departure from normality. | ||
+ | < | ||
+ | mvn(newdata, | ||
+ | mvn(newdata, | ||
+ | mvn(newdata, | ||
+ | </ | ||
+ | |||
+ | You can generate univariate plot as well to evaluate distribution of the endogenous variables for non-normality. Skewness values approaching 2 or kurtoisis values over 7 may be considered indicative of more " | ||
+ | < | ||
+ | mvn(newdata, | ||
+ | mvn(newdata, | ||
+ | |||
+ | model <- ' | ||
+ | interest ~ mastery + perfgoal + ses | ||
+ | achieve ~ anxiety + interest + mastery | ||
+ | anxiety ~ perfgoal + mastery | ||
+ | # variances | ||
+ | mastery ~~ mastery | ||
+ | perfgoal ~~ perfgoal | ||
+ | ses ~~ ses | ||
+ | | ||
+ | mastery ~~ perfgoal + ses | ||
+ | perfgoal ~~ ses | ||
+ | | ||
+ | interest ~~ interest | ||
+ | anxiety ~~ anxiety | ||
+ | achieve ~~ achieve | ||
+ | interest~~anxiety | ||
+ | ' | ||
+ | </ | ||
+ | We will fit the model using the ' | ||
+ | < | ||
+ | fit <- sem(model, data=processdata, | ||
+ | summary(fit, | ||
+ | </ | ||
+ | ---- | ||
+ | reference | ||
+ | {{youtube> | ||
+ | |||
+ | see [[https:// | ||
+ | |||
+ | ===== Exercise ===== | ||
+ | Using mtcars in R | ||
+ | < | ||
+ | ?mtcars | ||
+ | mtcars | ||
+ | str(mtcars) | ||
+ | df <- mtcars | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | # model specfication | ||
+ | model <-' | ||
+ | mpg ~ hp + gear + cyl + disp + carb + am + wt | ||
+ | hp ~ cyl + disp + carb | ||
+ | ' | ||
+ | # model fit | ||
+ | fit <- cfa(model, data = mtcars) | ||
+ | summary(fit, | ||
+ | semPaths(fit, | ||
+ | </ | ||
r/path_analysis.1668484800.txt.gz · Last modified: 2022/11/15 13:00 by hkimscil