library(EPSY905R) data("dataMath") #IMPORT DATA AND PUT INTO DATASET data01 = dataMath #AUTOMATING PACKAGES NEEDED FOR ANALYSES-------------------------------------------------------------------- haspackage = require("lavaan") if (haspackage==FALSE){ install.packages("lavaan") } library("lavaan") #model 01----------------------------------------------------------------------- model01.syntax = " #endogenous variable equations perf ~ hsl + msc + mse use ~ mse mse ~ hsl + cc + female msc ~ mse + cc + hsl cc ~ hsl hsl ~ female #endogenous variable intercepts perf ~ 1 use ~ 1 mse ~ 1 msc ~ 1 cc ~ 1 hsl ~ 1 #endogenous variable residual variances perf ~~ perf use ~~ use mse ~~ mse msc ~~ msc cc ~~ cc hsl ~~ hsl #endogenous variable residual covariances #none specfied in the original model so these have zeros: perf ~~ 0*use + 0*mse + 0*msc + 0*cc + 0*hsl use ~~ 0*mse + 0*msc + 0*cc + 0*hsl mse ~~ 0*msc + 0*cc + 0*hsl msc ~~ 0*cc + 0*hsl cc ~~ 0*hsl " #estimate model model01.fit = sem(model01.syntax, data=data01, mimic = "MPLUS", estimator = "MLR") #see if model converged inspect(model01.fit, what="converged") #show summary of model fit statistics and parameters summary(model01.fit, standardized=TRUE, fit.measures=TRUE) #show normalized residual covariance matrix residuals(model01.fit ,type="normalized") #calculate modification indices model01.mi=inspect(model01.fit, what="mi") #order from highest to lowest model01.mi=model01.mi[with(model01.mi,order(-mi)),] #display values of indices model01.mi #model 02: Add residual covariance between USE and MSC------------------------------------- model02.syntax = " #endogenous variable equations perf ~ hsl + msc + mse use ~ mse mse ~ hsl + cc + female msc ~ mse + cc + hsl cc ~ hsl hsl ~ female #endogenous variable intercepts perf ~ 1 use ~ 1 mse ~ 1 msc ~ 1 cc ~ 1 hsl ~ 1 #endogenous variable residual variances perf ~~ perf use ~~ use mse ~~ mse msc ~~ msc cc ~~ cc hsl ~~ hsl #endogenous variable residual covariances #none specfied in the original model so these have zeros: perf ~~ 0*use + 0*mse + 0*msc + 0*cc + 0*hsl use ~~ 0*mse + msc + 0*cc + 0*hsl #<- the changed part of syntax here (no 0* in front of msc) mse ~~ 0*msc + 0*cc + 0*hsl msc ~~ 0*cc + 0*hsl cc ~~ 0*hsl " #estimate model model02.fit = sem(model02.syntax, data=data01, mimic = "MPLUS", estimator = "MLR") #see if model converged inspect(model02.fit, what="converged") #show summary of model fit statistics and parameters summary(model02.fit, standardized=TRUE, fit.measures=TRUE) #show normalized residual covariance matrix residuals(model02.fit ,type="normalized") #calculate modification indices model02.mi=inspect(model02.fit, what="mi") #order from highest to lowest model02.mi=model02.mi[with(model02.mi,order(-mi)),] #display values of indices model02.mi #compare fit of model02 with model01 anova(model01.fit, model02.fit) #model 03: removing HSL predicting PERF and Gender predicting HSL ---------------- model03.syntax = " #endogenous variable equations perf ~ msc + mse use ~ mse mse ~ hsl + cc + female msc ~ mse + cc + hsl cc ~ hsl #endogenous variable intercepts perf ~ 1 use ~ 1 mse ~ 1 msc ~ 1 cc ~ 1 #endogenous variable residual variances perf ~~ perf use ~~ use mse ~~ mse msc ~~ msc cc ~~ cc #endogenous variable residual covariances #none specfied in the original model so these have zeros: perf ~~ 0*use + 0*mse + 0*msc + 0*cc use ~~ 0*mse + msc + 0*cc mse ~~ 0*msc + 0*cc msc ~~ 0*cc " #estimate model model03.fit = sem(model03.syntax, data=data01, mimic = "MPLUS", estimator = "MLR") #see if model converged inspect(model03.fit, what="converged") #show summary of model fit statistics and parameters summary(model03.fit, standardized=TRUE, fit.measures=TRUE) #show normalized residual covariance matrix residuals(model03.fit ,type="normalized") #calculate modification indices model03.mi=inspect(model03.fit, what="mi") #order from highest to lowest model03.mi=model03.mi[with(model03.mi,order(-mi)),] #display values of indices model03.mi #compare fit of model02 with model03 anova(model02.fit, model03.fit) #model 04: Add gender predicting cc ------------------------------------------------------------- model04.syntax = " #endogenous variable equations perf ~ msc + mse use ~ mse mse ~ (b_hsl_mse)*hsl + (b_cc_mse)*cc + female msc ~ mse + cc + hsl cc ~ (b_hsl_cc)*hsl + female #endogenous variable intercepts perf ~ 1 use ~ 1 mse ~ 1 msc ~ 1 cc ~ 1 #endogenous variable residual variances perf ~~ perf use ~~ use mse ~~ mse msc ~~ msc cc ~~ cc #endogenous variable residual covariances #none specfied in the original model so these have zeros: perf ~~ 0*use + 0*mse + 0*msc + 0*cc use ~~ 0*mse + msc + 0*cc mse ~~ 0*msc + 0*cc msc ~~ 0*cc #indirect effect of interest: ind_hsl_mse := b_hsl_cc*b_cc_mse #total effect of interest: tot_hsl_mse := b_hsl_mse + (b_hsl_cc*b_cc_mse) " #estimate model model04.fit = sem(model04.syntax, data=data01, mimic = "MPLUS", estimator = "MLR") #see if model converged inspect(model04.fit, what="converged") #show summary of model fit statistics and parameters summary(model04.fit, standardized=TRUE, fit.measures=TRUE) #show normalized residual covariance matrix residuals(model04.fit ,type="normalized") #calculate modification indices model04.mi=inspect(model04.fit, what="mi") #order from highest to lowest model04.mi=model04.mi[with(model04.mi,order(-mi)),] #display values of indices model04.mi #compare fit of model02 with model03 anova(model03.fit, model04.fit) inspect(model04.fit, what="r2") #r-squared values for DVs