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] – old revision restored (2022/11/15 06:37) hkimscil | r:path_analysis [2024/11/04 10:28] (current) – [Introduction] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Path Analysis ====== | ====== Path Analysis ====== | ||
- | ===== Lavaan in R: explanation ===== | + | {{:r:pasted:20230529-234519.png}} |
- | + | ====== | |
- | {{youtube> | + | |
- | Path analysis in R with Lavaan (introduction) | + | |
- | + | ||
- | By Mike Crowson, Ph.D. | + | |
- | September 17, 2019 | + | |
- | + | ||
- | * Overview: There are two basic functions that allow you to run path analysis in Lavaan: the ' | + | |
- | + | ||
- | * If you have not already done so, you will need to install Lavaan. | + | |
- | + | ||
- | < | + | |
- | install.packages(" | + | |
- | </ | + | |
- | + | ||
- | * Read data into R and store in data object. Make sure you have R correctly pointed to the folder containing your data. Below is syntax to create a data frame called ' | + | |
- | + | ||
- | < | + | |
- | processdata< | + | |
- | </ | + | |
- | + | ||
- | * Using the ' | + | |
- | + | ||
- | < | + | |
- | str(processdata) | + | |
- | </ | + | |
- | + | ||
- | * Use libary function to call up lavaan | + | |
- | + | ||
- | < | + | |
- | library(lavaan) | + | |
- | </ | + | |
- | + | ||
- | * ' | + | |
- | + | ||
- | * Step 1: Use lavaan model syntax to specify path model and have it stored in an R object. In our model, we will treat ses, mastery goals, and performance goals as predictors of student achievement. The effect of mastery on achievement will be both direct and indirect (via interest and anxiety. The effects of ses and performance goals will be treated as being fully mediated through anxiety and interest. | + | |
- | * When specifying predictive relationships in the model, we use the tilde sign (' | + | |
- | + | ||
- | < | + | |
- | # model specification | + | |
- | model <- ' | + | |
- | #equation where interest is predicted by ses | + | |
- | # & mastery and performance goals | + | |
- | interest ~ mastery + perfgoal + ses | + | |
- | + | ||
- | # equation where achieve is predicted by | + | |
- | # interest and anxiety | + | |
- | achieve ~ anxiety + interest + mastery | + | |
- | + | ||
- | # equation where anxiety is predicted | + | |
- | # by mastery and performance goals | + | |
- | anxiety ~ perfgoal + mastery | + | |
- | + | ||
- | # estimating the variances of | + | |
- | # the exogenous variables (ses, mastery, | + | |
- | mastery ~~ mastery | + | |
- | perfgoal ~~ perfgoal | + | |
- | ses ~~ ses | + | |
- | + | ||
- | # estimtating the covariances of the exogenous | + | |
- | # variables (ses, mastery, | + | |
- | mastery ~~ perfgoal + ses | + | |
- | perfgoal ~~ ses | + | |
- | + | ||
- | # estimating the residual variances | + | |
- | # for endogenous variables (interest, anxiety, achieve) | + | |
- | interest ~~ interest | + | |
- | anxiety ~~ anxiety | + | |
- | achieve ~~ achieve | + | |
- | + | ||
- | # estimating the covariance of residuals | + | |
- | # for interest and anxiety | + | |
- | interest ~~ anxiety ' | + | |
- | </ | + | |
- | * Step 2: Use ' | + | |
- | < | + | |
- | fit< | + | |
- | </ | + | |
- | * The ' | + | |
- | < | + | |
- | summary(fit, | + | |
- | </ | + | |
- | * To obtain standardized estimates, use the ' | + | |
- | < | + | |
- | summary(fit, | + | |
- | </ | + | |
- | + | ||
- | * Using the ' | + | |
- | < | + | |
- | parameterEstimates(fit) | + | |
- | </ | + | |
- | + | ||
- | * For a more comprehensive set of fit measures, use the ' | + | |
- | < | + | |
- | fitMeasures(fit) | + | |
- | </ | + | |
- | + | ||
- | * To obtain modification indices, you can use the ' | + | |
- | < | + | |
- | modificationIndices(fit) | + | |
- | </ | + | |
- | * Note: Modification indices represent the expected decrease in model chi-square after freeing a given parameter (Schumacker & Lomax, 2004). The EPC is an estimate of the model parameter itself. A MI value of 3.84 or greater may be considered " | + | |
- | + | ||
- | ----------------------------- | + | |
- | * Specification of model using auto.var argument... | + | |
- | < | + | |
- | # model specification | + | |
- | + | ||
- | model< | + | |
- | # equation where interest is predicted by ses & mastery and | + | |
- | # performance goals | + | |
- | interest ~ mastery + perfgoal + ses | + | |
- | + | ||
- | # equation where achieve is predicted by interest and anxiety | + | |
- | achieve~anxiety+interest+mastery | + | |
- | + | ||
- | #equation where anxiety is predicted by mastery and performance goals | + | |
- | anxiety~perfgoal+mastery | + | |
- | + | ||
- | # estimtating the variances of the exogenous variables (ses, mastery, | + | |
- | mastery~~mastery | + | |
- | perfgoal~~perfgoal | + | |
- | ses~~ses | + | |
- | + | ||
- | # estimtating the covariances of the exogenous variables (ses, mastery, | + | |
- | mastery~~perfgoal+ses | + | |
- | perfgoal~~ses | + | |
- | + | ||
- | # The auto.var argument when fitting the model can be used so that | + | |
- | # you do not have to directly request estimation of residual variances | + | |
- | + | ||
- | # Estimating the covariance of residuals for interest and anxiety | + | |
- | interest~~anxiety' | + | |
- | + | ||
- | fit< | + | |
- | summary(fit, | + | |
- | </ | + | |
- | + | ||
- | * There are a couple of ways you can obtain path diagrams (although they can be somewhat tricky to implement. | + | |
- | * One approach is to use the ' | + | |
- | < | + | |
- | install.packages(" | + | |
- | library(" | + | |
- | + | ||
- | semPaths(fit, | + | |
- | rotation=2) | + | |
- | </ | + | |
- | + | ||
- | * A second approach is to use the ' | + | |
- | < | + | |
- | install.packages(" | + | |
- | library(lavaanPlot) | + | |
- | + | ||
- | lavaanPlot(model = fit, | + | |
- | node_options = list(shape = " | + | |
- | edge_options = list(color = " | + | |
- | coefs = TRUE, | + | |
- | covs = TRUE, | + | |
- | stars = c(" | + | |
- | </ | + | |
- | + | ||
- | ---- | + | |
- | Resources on the use of lavaan: | + | |
- | + | ||
- | * http:// | + | |
- | * https:// | + | |
- | * https:// | + | |
- | * http:// | + | |
- | + | ||
- | ---- | + | |
- | Using the ' | + | |
- | * https://cran.r-project.org/ | + | |
- | * https:// | + | |
- | * http:// | + | |
- | + | ||
- | ---- | + | |
- | Using the ' | + | |
- | * https:// | + | |
- | * https:// | + | |
- | * https:// | + | |
- | * http:// | + | |
- | + | ||
- | ---- | + | |
- | Raw data for all examples can be downloaded at... | + | |
- | * https:// | + | |
- | + | ||
- | A copy of the Powerpoint of the model specification can be downloaded at... | + | |
- | * https:// | + | |
- | + | ||
- | Basics of path analysis using Lavaan.txt | + | |
- | Displaying Basics of path analysis using Lavaan.txt. | + | |
- | + | ||
- | CODING | + | |
- | < | + | |
- | processdata <- read.csv(" | + | |
- | str(processdata) | + | |
- | library(lavaan) | + | |
- | 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 | + | |
- | ' | + | |
- | fit <- lavaan(model. data=processdata) | + | |
- | fit <- sem(model. data=processdata) | + | |
- | + | ||
- | summary(fit, | + | |
- | summary(fit, | + | |
- | + | ||
- | parameterEstimates(fit) | + | |
- | fitMeasures(fit) | + | |
- | modificationIndices(fit) | + | |
- | + | ||
- | install.packages(" | + | |
- | library(" | + | |
- | + | ||
- | semPaths(fit, | + | |
- | rotation=2) | + | |
- | + | ||
- | install.packages(" | + | |
- | library(lavaanPlot) | + | |
- | + | ||
- | lavaanPlot( | + | |
- | model = fit, | + | |
- | node_options = list(shape = " | + | |
- | edge_options = list(color = " | + | |
- | coefs = TRUE, covs=TRUE, | + | |
- | stars = c(" | + | |
- | + | ||
- | + | ||
- | </ | + | |
- | ---- | + | |
- | ===== 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:// | + | |
- | + | ||
- | ====== | + | |
{{youtube> | {{youtube> | ||
Line 373: | Line 16: | ||
* The number of unique (non-redundent) source of information | * The number of unique (non-redundent) source of information | ||
* $p(p+1)/2$ | * $p(p+1)/2$ | ||
- | | + | |
- | * Just-identified (df = 0) | + | * Just-identified (df = 0) |
- | * Model can be estimated, but cannot be assessed | + | * Model can be estimated, but cannot be assessed |
- | * Over-identified (df > 0) | + | * Over-identified (df > 0) |
- | * Model can be estimated and assessed | + | * Model can be estimated and assessed |
- | * Under-identified (df < 0) | + | * Under-identified (df < 0) |
- | * Model cannot be either estimated or assessed | + | * Model cannot be either estimated or assessed |
* Exogenous and | * Exogenous and | ||
Line 468: | 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 797: | 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 ===== | ||
+ | {{youtube> | ||
+ | Path analysis in R with Lavaan (introduction) | ||
+ | |||
+ | By Mike Crowson, Ph.D. | ||
+ | September 17, 2019 | ||
+ | |||
+ | * Overview: There are two basic functions that allow you to run path analysis in Lavaan: the ' | ||
+ | |||
+ | * If you have not already done so, you will need to install Lavaan. | ||
+ | |||
+ | < | ||
+ | install.packages(" | ||
+ | </ | ||
+ | |||
+ | * Read data into R and store in data object. Make sure you have R correctly pointed to the folder containing your data. Below is syntax to create a data frame called ' | ||
+ | |||
+ | < | ||
+ | # processdata< | ||
+ | processdata< | ||
+ | | ||
+ | </ | ||
+ | |||
+ | * Using the ' | ||
+ | |||
+ | < | ||
+ | str(processdata) | ||
+ | </ | ||
+ | |||
+ | * Use libary function to call up lavaan | ||
+ | |||
+ | < | ||
+ | library(lavaan) | ||
+ | </ | ||
+ | |||
+ | * ' | ||
+ | |||
+ | * Step 1: Use lavaan model syntax to specify path model and have it stored in an R object. In our model, we will treat ses, mastery goals, and performance goals as predictors of student achievement. The effect of mastery on achievement will be both direct and indirect (via interest and anxiety. The effects of ses and performance goals will be treated as being fully mediated through anxiety and interest. | ||
+ | * When specifying predictive relationships in the model, we use the tilde sign (' | ||
+ | |||
+ | < | ||
+ | # model specification | ||
+ | model <- ' | ||
+ | #equation where interest is predicted by ses | ||
+ | # & mastery and performance goals | ||
+ | interest ~ mastery + perfgoal + ses | ||
+ | | ||
+ | # equation where achieve is predicted by | ||
+ | # interest and anxiety | ||
+ | achieve ~ anxiety + interest + mastery | ||
+ | |||
+ | # equation where anxiety is predicted | ||
+ | # by mastery and performance goals | ||
+ | anxiety ~ perfgoal + mastery | ||
+ | |||
+ | # estimating the variances of | ||
+ | # the exogenous variables (ses, mastery, | ||
+ | mastery ~~ mastery | ||
+ | perfgoal ~~ perfgoal | ||
+ | ses ~~ ses | ||
+ | |||
+ | # estimtating the covariances of the exogenous | ||
+ | # variables (ses, mastery, | ||
+ | mastery ~~ perfgoal + ses | ||
+ | perfgoal ~~ ses | ||
+ | |||
+ | # estimating the residual variances | ||
+ | # for endogenous variables (interest, anxiety, achieve) | ||
+ | interest ~~ interest | ||
+ | anxiety ~~ anxiety | ||
+ | achieve ~~ achieve | ||
+ | |||
+ | # estimating the covariance of residuals | ||
+ | # for interest and anxiety | ||
+ | interest ~~ anxiety ' | ||
+ | </ | ||
+ | * Step 2: Use ' | ||
+ | < | ||
+ | fit< | ||
+ | </ | ||
+ | * The ' | ||
+ | < | ||
+ | summary(fit, | ||
+ | </ | ||
+ | * To obtain standardized estimates, use the ' | ||
+ | < | ||
+ | summary(fit, | ||
+ | </ | ||
+ | |||
+ | * Using the ' | ||
+ | < | ||
+ | parameterEstimates(fit) | ||
+ | </ | ||
+ | |||
+ | * For a more comprehensive set of fit measures, use the ' | ||
+ | < | ||
+ | fitMeasures(fit) | ||
+ | </ | ||
+ | |||
+ | * To obtain modification indices, you can use the ' | ||
+ | < | ||
+ | modificationIndices(fit) | ||
+ | </ | ||
+ | * Note: Modification indices represent the expected decrease in model chi-square after freeing a given parameter (Schumacker & Lomax, 2004). The EPC is an estimate of the model parameter itself. A MI value of 3.84 or greater may be considered " | ||
+ | |||
+ | output | ||
+ | |||
+ | < | ||
+ | > # install.packages(" | ||
+ | > | ||
+ | > # processdata< | ||
+ | > processdata< | ||
+ | + header=TRUE, | ||
+ | > | ||
+ | > str(processdata) | ||
+ | ' | ||
+ | $ id : int 1 2 3 4 5 6 7 8 9 10 ... | ||
+ | $ ses : int 1 0 0 1 1 1 0 0 1 1 ... | ||
+ | $ genderid: int 1 0 1 1 1 1 0 0 0 0 ... | ||
+ | $ perfgoal: num 29.5 29.5 30.4 33.5 28.7 ... | ||
+ | $ achieve : num 6.12 1.62 4.5 2.38 5.12 ... | ||
+ | $ mastery : num 5.71 1.43 1.29 2.29 4.57 ... | ||
+ | $ interest: num 6 4 2 4 5.5 4 4 5 4.5 4 ... | ||
+ | $ anxiety : num 1.67 6.33 3.67 3.67 3.67 ... | ||
+ | $ pgoal_MS: int 0 0 1 1 0 1 0 1 0 0 ... | ||
+ | > library(lavaan) | ||
+ | > | ||
+ | > # model specification | ||
+ | > model <- ' | ||
+ | + # equation where interest is predicted by ses | ||
+ | + # & mastery and performance goals | ||
+ | + | ||
+ | + | ||
+ | + # equation where achieve is predicted by | ||
+ | + # interest and anxiety | ||
+ | + | ||
+ | + | ||
+ | + # equation where anxiety is predicted | ||
+ | + # by mastery and performance goals | ||
+ | + | ||
+ | + | ||
+ | + # estimating the variances of | ||
+ | + # the exogenous variables (ses, mastery, | ||
+ | + | ||
+ | + | ||
+ | + ses ~~ ses | ||
+ | + | ||
+ | + # estimtating the covariances of the exogenous | ||
+ | + # variables (ses, mastery, | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + # estimating the residual variances | ||
+ | + # for endogenous variables (interest, anxiety, achieve) | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + # estimating the covariance of residuals | ||
+ | + # for interest and anxiety | ||
+ | + | ||
+ | > | ||
+ | > fit< | ||
+ | > summary(fit, | ||
+ | lavaan 0.6.16 ended normally after 27 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) 0.860 | ||
+ | Tucker-Lewis Index (TLI) 0.300 | ||
+ | |||
+ | Loglikelihood and Information Criteria: | ||
+ | |||
+ | Loglikelihood user model (H0) -1391.274 | ||
+ | Loglikelihood unrestricted model (H1) -1376.659 | ||
+ | | ||
+ | Akaike (AIC) 2818.548 | ||
+ | Bayesian (BIC) 2871.498 | ||
+ | Sample-size adjusted Bayesian (SABIC) | ||
+ | |||
+ | Root Mean Square Error of Approximation: | ||
+ | |||
+ | RMSEA 0.250 | ||
+ | 90 Percent confidence interval - lower 0.172 | ||
+ | 90 Percent confidence interval - upper 0.336 | ||
+ | P-value H_0: RMSEA <= 0.050 0.000 | ||
+ | P-value H_0: RMSEA >= 0.080 1.000 | ||
+ | |||
+ | Standardized Root Mean Square Residual: | ||
+ | |||
+ | SRMR 0.074 | ||
+ | |||
+ | Parameter Estimates: | ||
+ | |||
+ | Standard errors | ||
+ | Information | ||
+ | Information saturated (h1) model Structured | ||
+ | |||
+ | Regressions: | ||
+ | | ||
+ | interest ~ | ||
+ | mastery | ||
+ | perfgoal | ||
+ | ses | ||
+ | achieve ~ | ||
+ | anxiety | ||
+ | interest | ||
+ | mastery | ||
+ | anxiety ~ | ||
+ | perfgoal | ||
+ | mastery | ||
+ | |||
+ | Covariances: | ||
+ | | ||
+ | mastery ~~ | ||
+ | perfgoal | ||
+ | ses | ||
+ | perfgoal ~~ | ||
+ | ses -0.226 | ||
+ | | ||
+ | | ||
+ | |||
+ | Variances: | ||
+ | | ||
+ | mastery | ||
+ | perfgoal | ||
+ | ses | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | > summary(fit, | ||
+ | lavaan 0.6.16 ended normally after 27 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) 0.860 | ||
+ | Tucker-Lewis Index (TLI) 0.300 | ||
+ | |||
+ | Loglikelihood and Information Criteria: | ||
+ | |||
+ | Loglikelihood user model (H0) -1391.274 | ||
+ | Loglikelihood unrestricted model (H1) -1376.659 | ||
+ | | ||
+ | Akaike (AIC) 2818.548 | ||
+ | Bayesian (BIC) 2871.498 | ||
+ | Sample-size adjusted Bayesian (SABIC) | ||
+ | |||
+ | Root Mean Square Error of Approximation: | ||
+ | |||
+ | RMSEA 0.250 | ||
+ | 90 Percent confidence interval - lower 0.172 | ||
+ | 90 Percent confidence interval - upper 0.336 | ||
+ | P-value H_0: RMSEA <= 0.050 0.000 | ||
+ | P-value H_0: RMSEA >= 0.080 1.000 | ||
+ | |||
+ | Standardized Root Mean Square Residual: | ||
+ | |||
+ | SRMR 0.074 | ||
+ | |||
+ | Parameter Estimates: | ||
+ | |||
+ | Standard errors | ||
+ | Information | ||
+ | Information saturated (h1) model Structured | ||
+ | |||
+ | Regressions: | ||
+ | | ||
+ | interest ~ | ||
+ | mastery | ||
+ | perfgoal | ||
+ | ses | ||
+ | achieve ~ | ||
+ | anxiety | ||
+ | interest | ||
+ | mastery | ||
+ | anxiety ~ | ||
+ | perfgoal | ||
+ | mastery | ||
+ | |||
+ | Covariances: | ||
+ | | ||
+ | mastery ~~ | ||
+ | perfgoal | ||
+ | ses | ||
+ | perfgoal ~~ | ||
+ | ses -0.226 | ||
+ | | ||
+ | | ||
+ | |||
+ | Variances: | ||
+ | | ||
+ | mastery | ||
+ | perfgoal | ||
+ | ses | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | R-Square: | ||
+ | | ||
+ | interest | ||
+ | anxiety | ||
+ | achieve | ||
+ | |||
+ | > | ||
+ | > parameterEstimates(fit) | ||
+ | lhs op rhs est se z pvalue ci.lower ci.upper | ||
+ | 1 interest | ||
+ | 2 interest | ||
+ | 3 interest | ||
+ | 4 | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 perfgoal ~~ perfgoal | ||
+ | 11 ses ~~ ses 0.249 0.030 8.367 0.000 0.191 0.308 | ||
+ | 12 mastery ~~ perfgoal -0.935 0.361 -2.590 | ||
+ | 13 mastery ~~ ses 0.170 0.061 2.805 0.005 0.051 0.288 | ||
+ | 14 perfgoal ~~ ses -0.226 0.128 -1.768 | ||
+ | 15 interest ~~ interest | ||
+ | 16 anxiety ~~ anxiety | ||
+ | 17 achieve ~~ achieve | ||
+ | 18 interest ~~ anxiety | ||
+ | > fitMeasures(fit) | ||
+ | | ||
+ | | ||
+ | | ||
+ | 3.000 | ||
+ | baseline.df | ||
+ | | ||
+ | tli nnfi | ||
+ | 0.300 | ||
+ | nfi pnfi | ||
+ | 0.856 | ||
+ | rni logl | ||
+ | 0.860 | ||
+ | aic | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 0.336 | ||
+ | | ||
+ | 0.050 | ||
+ | rmr rmr_nomean | ||
+ | 0.122 | ||
+ | | ||
+ | 0.074 | ||
+ | crmr_nomean | ||
+ | 0.088 | ||
+ | cn_05 | ||
+ | | ||
+ | | ||
+ | 0.587 | ||
+ | | ||
+ | 0.466 | ||
+ | > modificationIndices(fit) | ||
+ | lhs op rhs | ||
+ | 19 interest ~~ achieve 25.396 -2.899 | ||
+ | 23 achieve ~~ anxiety | ||
+ | 24 achieve ~~ mastery 22.476 -1.743 | ||
+ | 25 achieve ~~ perfgoal | ||
+ | 26 achieve ~~ ses 20.541 | ||
+ | 27 anxiety ~~ mastery | ||
+ | 28 anxiety ~~ perfgoal | ||
+ | 29 anxiety ~~ ses 0.921 -0.061 | ||
+ | 30 interest | ||
+ | 32 achieve | ||
+ | 33 achieve | ||
+ | 34 anxiety | ||
+ | 35 anxiety | ||
+ | 36 anxiety | ||
+ | 37 mastery | ||
+ | 38 mastery | ||
+ | 39 mastery | ||
+ | 43 perfgoal | ||
+ | 44 perfgoal | ||
+ | 47 ses ~ interest | ||
+ | 48 ses ~ achieve 20.964 | ||
+ | 49 ses ~ anxiety | ||
+ | > | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ----------------------------- | ||
+ | * Specification of model using auto.var argument... | ||
+ | < | ||
+ | # model specification | ||
+ | |||
+ | model< | ||
+ | # equation where interest is predicted by ses & mastery and | ||
+ | # performance goals | ||
+ | interest ~ mastery + perfgoal + ses | ||
+ | |||
+ | # equation where achieve is predicted by interest and anxiety | ||
+ | achieve~anxiety+interest+mastery | ||
+ | |||
+ | #equation where anxiety is predicted by mastery and performance goals | ||
+ | anxiety~perfgoal+mastery | ||
+ | |||
+ | # estimtating the variances of the exogenous variables (ses, mastery, | ||
+ | mastery~~mastery | ||
+ | perfgoal~~perfgoal | ||
+ | ses~~ses | ||
+ | |||
+ | # estimtating the covariances of the exogenous variables (ses, mastery, | ||
+ | mastery~~perfgoal+ses | ||
+ | perfgoal~~ses | ||
+ | |||
+ | # The auto.var argument when fitting the model can be used so that | ||
+ | # you do not have to directly request estimation of residual variances | ||
+ | |||
+ | # Estimating the covariance of residuals for interest and anxiety | ||
+ | interest~~anxiety' | ||
+ | |||
+ | fit< | ||
+ | summary(fit, | ||
+ | </ | ||
+ | |||
+ | * There are a couple of ways you can obtain path diagrams (although they can be somewhat tricky to implement. | ||
+ | * One approach is to use the ' | ||
+ | < | ||
+ | install.packages(" | ||
+ | library(" | ||
+ | |||
+ | semPaths(fit, | ||
+ | rotation=2) | ||
+ | </ | ||
+ | |||
+ | * A second approach is to use the ' | ||
+ | < | ||
+ | install.packages(" | ||
+ | library(lavaanPlot) | ||
+ | |||
+ | lavaanPlot(model = fit, | ||
+ | node_options = list(shape = " | ||
+ | edge_options = list(color = " | ||
+ | coefs = TRUE, | ||
+ | covs = TRUE, | ||
+ | stars = c(" | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | Resources on the use of lavaan: | ||
+ | |||
+ | * http:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | |||
+ | ---- | ||
+ | Using the ' | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | |||
+ | ---- | ||
+ | Using the ' | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | |||
+ | ---- | ||
+ | Raw data for all examples can be downloaded at... | ||
+ | * https:// | ||
+ | |||
+ | A copy of the Powerpoint of the model specification can be downloaded at... | ||
+ | * https:// | ||
+ | |||
+ | Basics of path analysis using Lavaan.txt | ||
+ | Displaying Basics of path analysis using Lavaan.txt. | ||
+ | |||
+ | CODING | ||
+ | < | ||
+ | processdata< | ||
+ | | ||
+ | str(processdata) | ||
+ | library(lavaan) | ||
+ | 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 | ||
+ | ' | ||
+ | fit <- lavaan(model, | ||
+ | fit <- sem(model, data=processdata) | ||
+ | |||
+ | summary(fit, | ||
+ | summary(fit, | ||
+ | |||
+ | parameterEstimates(fit) | ||
+ | fitMeasures(fit) | ||
+ | modificationIndices(fit) | ||
+ | |||
+ | install.packages(" | ||
+ | library(" | ||
+ | |||
+ | semPaths(fit, | ||
+ | rotation=2) | ||
+ | |||
+ | install.packages(" | ||
+ | library(lavaanPlot) | ||
+ | |||
+ | lavaanPlot( | ||
+ | model = fit, | ||
+ | node_options = list(shape = " | ||
+ | edge_options = list(color = " | ||
+ | coefs = TRUE, covs=TRUE, | ||
+ | stars = c(" | ||
+ | |||
+ | |||
+ | </ | ||
+ | ---- | ||
+ | ===== 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.1668484837.txt.gz · Last modified: 2022/11/15 13:00 by hkimscil