library(learnr) library(learnSEM) knitr::opts_chunk$set(echo = FALSE) library(lavaan) library(semPlot) university.cov <- lav_matrix_lower2full( c(169.00, 73.710, 182.2500, 73.229, 88.4250, 171.6100, 63.375, 72.5625, 127.7250, 156.2500, 42.120, 67.4325, 122.0265, 123.1875, 182.2500, 57.226, 63.2610, 117.1926, 154.4250, 138.0240, 201.6400, 30.875, 32.0625, 60.9805, 62.9375, 76.9500, 79.5910, 90.2500, 36.075, 38.9610, 61.0722, 58.2750, 65.9340, 70.9290, 81.1965, 123.2100, 18.096, 21.1410, 26.2131, 39.1500, 44.6310, 46.9452, 48.7635, 56.0106, 75.6900)) rownames(university.cov) <- colnames(university.cov) <- c("class", "social", "learn", "chronic", "physical", "sex", "depression", "anxiety", "stress")
This section of the course increases your skills by expanding from measurement models and CFA to full structural models. We will begin to use the latent variables to predict each other or add other covariates to predict the measurement models. You will use the sem()
function and learn more about how to write lavaan
code for composite variables. The learning outcomes are:
You can use vignette("lecture_sem", "learnSEM")
to view these notes in R.
In this next section, you will answer questions using the R code blocks provided. Be sure to use the solution
option to see the answer if you need it!
Please enter your name for submission. If you do not need to submit, just type anything you'd like in this box.
question_text( "Student Name:", answer("Your Name", correct = TRUE), incorrect = "Thanks!", try_again_button = "Modify your answer", allow_retry = TRUE )
The data is provided for you below, and it has been loaded for you in the background, along with the lavaan
and semPlot
libraries. The data contains:
The data is called university.cov
, and we will use n = 300 for our model.
university.cov <- lav_matrix_lower2full( c(169.00, 73.710, 182.2500, 73.229, 88.4250, 171.6100, 63.375, 72.5625, 127.7250, 156.2500, 42.120, 67.4325, 122.0265, 123.1875, 182.2500, 57.226, 63.2610, 117.1926, 154.4250, 138.0240, 201.6400, 30.875, 32.0625, 60.9805, 62.9375, 76.9500, 79.5910, 90.2500, 36.075, 38.9610, 61.0722, 58.2750, 65.9340, 70.9290, 81.1965, 123.2100, 18.096, 21.1410, 26.2131, 39.1500, 44.6310, 46.9452, 48.7635, 56.0106, 75.6900)) rownames(university.cov) <- colnames(university.cov) <- c("class", "social", "learn", "chronic", "physical", "sex", "depression", "anxiety", "stress")
In this section, we will specific our overall composite.model
by creating three latent variables for our measurement model:
University
: class, social, learn as a composite variableHealth
: chronic, physical, sex as a reflexive variablePsychology
: depression, anxiety, stress as a reflexive variableThe structural model should include the university composite predicting both the psychological and health factors. We expect that as university perception changes, changes in mental and physical health symptoms should exist as well. Note: all variables have been scaled so that high scores are negative indicators.
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress Psychology ~ University Health ~ University '
Analyze your composite.model
using the sem()
function, and name the output composite.fit
. There are 300 participants in the dataset. Remember the data name is university.cov
, which is a covariance table.
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress Psychology ~ University Health ~ University '
composite.fit <- sem(model = composite.model, sample.cov = university.cov, sample.nobs = 300)
Oh no! Your model says it is not identified. What should you do? In this case, we have to remember that the entire model has to be identified. Let's look at a picture to see where that might be:
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress Psychology ~ University Health ~ University ' composite.fit <- sem(model = composite.model, sample.cov = university.cov, sample.nobs = 300) semPaths(composite.fit, whatLabels = "std", layout = "spring", edge.label.cex = 1)
Because University is predicting only Psychology and Health, we have two regression paths that act like a small measurement model for University. The identification for this section is not met, so we need to set them equal to each other or create a marker variable to control for these paths. The easiest solution is to set University =~ Psychology + Health
rather than set two separate regressions. Rerun your composite.fit
to make sure you do not get an error.
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress University =~ Psychology + Health '
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress University =~ Psychology + Health '
composite.fit <- sem(model = composite.model, sample.cov = university.cov, sample.nobs = 300)
Let's summarize the final model you just created. Use the summary()
function on both models with the standardized solution, r-square values, and fit.measures all included.
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress University =~ Psychology + Health ' composite.fit <- sem(model = composite.model, sample.cov = university.cov, sample.nobs = 300)
summary(composite.fit, standardized = TRUE, rsquare = TRUE, fit.measures = TRUE)
question_text( "Look at the solution of the final model and describe that model. Do all of the items load with their latent variable? Does university appear to predict psychological and health factors?", answer("Lots of right answers.", correct = TRUE), incorrect = "Be sure to detail your answer! It appears that psychological and health items load on their latent, but the university latent is not a very good composite. Additionally, the university variable does predict the other two latents but it's likely driven by the learning environment which is related to the university variable.", try_again_button = "Modify your answer", allow_retry = TRUE )
Use semPaths()
to create a picture of your full structural model. Use std
for the whatLabels
argument, any layout you would like, and edge.label.cex = 1
to increase the font size.
composite.model <- ' University <~ class + social + learn Health =~ chronic + physical + sex Psychology =~ depression + anxiety + stress University =~ Psychology + Health ' composite.fit <- sem(model = composite.model, sample.cov = university.cov, sample.nobs = 300)
semPaths(composite.fit, whatLabels = "std", layout = "spring", edge.label.cex = 1)
On this page, you will create the submission for your instructor (if necessary). Please copy this report and submit using a Word document or paste into the text window of your submission page. Click "Generate Submission" to get your work!
encoder_logic()
encoder_ui()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.