r:path_analysis
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
r:path_analysis [2022/11/15 02:14] – [Path Analysis] hkimscil | r:path_analysis [2023/05/29 23:45] – hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Path Analysis ====== | ====== Path Analysis ====== | ||
- | {{youtube> | + | {{:r:pasted:20230529-234519.png}} |
- | + | ====== | |
- | < | + | |
- | + | ||
- | 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 ' | + | |
- | functions.This video will demonstrate how to specify | + | |
- | a path model involving only manifest variables and how to | + | |
- | estimate model parameters using the ' | + | |
- | this text file and a .csv file containing the raw data will | + | |
- | be available for download underneath the video description. | + | |
- | You will notice that I use the pound sign (#) in some of the | + | |
- | syntax. The # sign is used for comments and are not read by the | + | |
- | program. I use it in some of the syntax below to provide annotations. | + | |
- | --------------------------- | + | |
- | 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 ' | + | |
- | (referenced above) into R.This is the data frame we will be using when | + | |
- | running our analyses. | + | |
- | + | ||
- | 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 (' | + | |
- | its predictors. Predictors are separated in each equation by ' | + | |
- | In our model, we will also allow the residuals for anxiety and interest | + | |
- | to correlate (see ' | + | |
- | + | ||
- | #model specification | + | |
- | + | ||
- | model< | + | |
- | #equation where interest is predicted by ses & mastery and | + | |
- | # | + | |
- | 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 | + | |
- | # | + | |
- | mastery~~mastery | + | |
- | perfgoal~~perfgoal | + | |
- | ses~~ses | + | |
- | # | + | |
- | 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 ' | + | |
- | the results in an R object called ' | + | |
- | the parenthesis are arguments separated by commas. The first argument | + | |
- | contains the name of the object containing the model syntax (see above). | + | |
- | The object is named ' | + | |
- | have the ' | + | |
- | containing the raw data. | + | |
- | + | ||
- | fit< | + | |
- | + | ||
- | *The ' | + | |
- | parameter estimates for the model | + | |
- | + | ||
- | summary(fit, | + | |
- | + | ||
- | *To obtain standardized estimates, use the ' | + | |
- | it to TRUE) when using the ' | + | |
- | Std.all column in the output, as it will provide standardized estimates for all | + | |
- | measured variables in the model. | + | |
- | + | ||
- | 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 " | + | |
- | empirically based approach to model specification. Consult your theory when | + | |
- | using these! | + | |
- | + | ||
- | ----------------------------- | + | |
- | *Specification of model using auto.var argument... | + | |
- | + | ||
- | #model specification | + | |
- | + | ||
- | model< | + | |
- | #equation where interest is predicted by ses & mastery and | + | |
- | # | + | |
- | 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 | + | |
- | # | + | |
- | mastery~~mastery | + | |
- | perfgoal~~perfgoal | + | |
- | ses~~ses | + | |
- | # | + | |
- | 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 ' | + | |
- | Below, I provide a rough demo of this approach. Citations containing | + | |
- | additional information is provided below the demo. | + | |
- | + | ||
- | install.packages(" | + | |
- | library(" | + | |
- | + | ||
- | semPaths(fit, | + | |
- | rotation=2) | + | |
- | + | ||
- | *A second approach is to use the ' | + | |
- | package. | + | |
- | + | ||
- | install.packages(" | + | |
- | + | ||
- | library(lavaanPlot) | + | |
- | + | ||
- | lavaanPlot(model = fit, node_options = list(shape = " | + | |
- | " | + | |
- | 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. | + | |
- | </ | + | |
- | ---- | + | |
- | Using lavaan packages | + | |
- | + | ||
- | IVs = exogenous | + | |
- | ses | + | |
- | mastery | + | |
- | perfgoal | + | |
- | + | ||
- | DVs = endogenous | + | |
- | interest | + | |
- | anxiety | + | |
- | + | ||
- | achieve | + | |
- | + | ||
- | < | + | |
- | 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(" | + | |
- | + | ||
- | + | ||
- | </ | + | |
- | ---- | + | |
- | + | ||
- | {{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) | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | + | ||
- | + | ||
- | ---- | + | |
- | reference | + | |
- | {{youtube> | + | |
- | + | ||
- | see [[https:// | + | |
- | + | ||
- | ====== | + | |
{{youtube> | {{youtube> | ||
Line 444: | 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 773: | 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 " | ||
+ | |||
+ | ----------------------------- | ||
+ | * 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. 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:// | ||
+ | |||
+ | ===== 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.txt · Last modified: 2023/11/27 16:57 by hkimscil