Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision |
r:path_analysis [2022/11/15 22:12] – [Lavaan in R: explanation] hkimscil | r:path_analysis [2023/11/27 16:56] – [Lavaan in R: explanation] hkimscil |
---|
====== Path Analysis ====== | ====== Path Analysis ====== |
| {{:r:pasted:20230529-234519.png}} |
====== Introduction ====== | ====== Introduction ====== |
{{youtube>UGIVPtFKwc0}} | {{youtube>UGIVPtFKwc0}} |
# my own | # my own |
# pbt model | # pbt model |
specmod5 <- " | specmod5 <- ' |
# Directional relations (path) | # Directional relations (path) |
intention ~ attitude + norms + control | intention ~ a*attitude + b*norms + c*control |
behavior ~ intention + norms | behavior ~ d*intention |
# Covariances | # Covariances |
attitude ~~ norms + control | attitude ~~ norms + control |
norms ~~ control | norms ~~ control |
" | ad := a*d |
| bd := b*d |
| cd := c*d |
| ' |
fitmod5 <- sem(specmod5, data=df) | fitmod5 <- sem(specmod5, data=df) |
summary(fitmod5, fit.measures=TRUE, rsquare=TRUE) | summary(fitmod5, fit.measures=TRUE, rsquare=TRUE) |
<code> | <code> |
# processdata<-read.csv("path analysis dataN BinW.csv", header=TRUE, sep=",") | # processdata<-read.csv("path analysis dataN BinW.csv", header=TRUE, sep=",") |
processdata<-read.csv("processdata<-read.csv("path analysis dataN BinW.csv", header=TRUE, sep=",")", header=TRUE, sep=",") | processdata<-read.csv("http://commres.net/wiki/_media/r/path_analysis_datan_binw.csv", |
| header=TRUE, sep=",", fileEncoding="UTF-8-BOM") |
</code> | </code> |
| |
* Step 2: Use 'lavaan' function to run analysis. Here, I will be saving the results in an R object called 'fit' (arbitrarily named). Inside 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 'model' (again, arbitrarily named above). Next, we have the 'data' argument. This identifies the object (i.e., data frame) containing the raw data. | * Step 2: Use 'lavaan' function to run analysis. Here, I will be saving the results in an R object called 'fit' (arbitrarily named). Inside 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 'model' (again, arbitrarily named above). Next, we have the 'data' argument. This identifies the object (i.e., data frame) containing the raw data. |
<code> | <code> |
fit<-lavaan(model,data=processdata) | fit<-lavaan(model, data=processdata) |
</code> | </code> |
* The 'summary' function can be used to obtain various fit measures and the parameter estimates for the model | * The 'summary' function can be used to obtain various fit measures and the parameter estimates for the model |
<code> | <code> |
summary(fit,fit.measures=TRUE) | summary(fit, fit.measures=TRUE) |
</code> | </code> |
* To obtain standardized estimates, use the 'standardized' argument (setting it to TRUE) when using the 'summary' function. You will need to interpret the Std.all column in the output, as it will provide standardized estimates for all measured variables in the model. | * To obtain standardized estimates, use the 'standardized' argument (setting it to TRUE) when using the 'summary' function. You will need to interpret the Std.all column in the output, as it will provide standardized estimates for all measured variables in the model. |
<code> | <code> |
summary(fit,fit.measures=TRUE,standardized=TRUE,rsquare=TRUE) | summary(fit, fit.measures=TRUE, standardized=TRUE, rsquare=TRUE) |
</code> | </code> |
| |
interest~~anxiety' | interest~~anxiety' |
| |
fit<-lavaan(model,data=processdata,auto.var=TRUE) | fit<-lavaan(model, data=processdata, auto.var=TRUE) |
summary(fit,fit.measures=TRUE,standardized=TRUE,rsquare=TRUE) | summary(fit, fit.measures=TRUE, standardized=TRUE, rsquare=TRUE) |
</code> | </code> |
| |
CODING | CODING |
<code> | <code> |
processdata <- read.csv("http://commres.net/wiki/_media/r/path_analysis_datan_binw.csv") | processdata<-read.csv("http://commres.net/wiki/_media/r/path_analysis_datan_binw.csv", |
| header=TRUE, sep=",", fileEncoding="UTF-8-BOM") |
str(processdata) | str(processdata) |
library(lavaan) | library(lavaan) |
see [[https://www.rensvandeschoot.com/tutorials/lme4/|lme4 tutorial]] | see [[https://www.rensvandeschoot.com/tutorials/lme4/|lme4 tutorial]] |
| |
| ===== Exercise ===== |
| Using mtcars in R |
| <code> |
| ?mtcars |
| mtcars |
| str(mtcars) |
| df <- mtcars |
| </code> |
| |
| <code> |
| # model specfication |
| model <-' |
| mpg ~ hp + gear + cyl + disp + carb + am + wt |
| hp ~ cyl + disp + carb |
| ' |
| # model fit |
| fit <- cfa(model, data = mtcars) |
| summary(fit, fit.measures = TRUE, standardized=T, rsquare=T) |
| semPaths(fit, 'std', layout = 'circle') |
| </code> |
| |