library(learnr) library(learnSEM) knitr::opts_chunk$set(echo = FALSE) library(lavaan) library(semPlot) model <- ' # measurement model ind60 =~ x1 + x2 + x3 dem60 =~ y1 + y2 + y3 + y4 dem65 =~ y5 + y6 + y7 + y8 # regressions dem60 ~ ind60 dem65 ~ ind60 + dem60 # residual correlations y1 ~~ y5 y2 ~~ y4 + y6 y3 ~~ y7 y4 ~~ y8 y6 ~~ y8 ' fit <- sem(model, data=PoliticalDemocracy)
This section of the course will begin to introduce you to the terminology associated with structural equation modeling. We will begin to introduce the code specific to lavaan
to help demonstrate how these terms can be applied to the models you will be programming in the next sections. In this practice, you will learn:
You can use vignette("lecture_terms", "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 )
We are going to use an example from the lavaan
tutorials, which you can find at https://lavaan.ugent.be/tutorial/sem.html. The model represents a complex model that will allow you to think about some of the terminology covered in the lecture.
Here's a picture of the model:
We have not covered yet how to write the model code, but you can see it below.
library(lavaan) model <- ' # measurement model ind60 =~ x1 + x2 + x3 dem60 =~ y1 + y2 + y3 + y4 dem65 =~ y5 + y6 + y7 + y8 # regressions dem60 ~ ind60 dem65 ~ ind60 + dem60 # residual correlations y1 ~~ y5 y2 ~~ y4 + y6 y3 ~~ y7 y4 ~~ y8 y6 ~~ y8 ' fit <- sem(model, data=PoliticalDemocracy)
Here's the model again to help you answer these questions:
question_text( "How many latent variables are in the model?", answer("3", correct = TRUE), incorrect = "There are three circles for latent variables.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "How many manifest variables are in the model?", answer("11", correct = TRUE), incorrect = "There are 11 squares for measured/manifest variables.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "What would you label `ind60` predicting `x1`, `x2`, and `x3`?", answer("Measurement model", correct = TRUE), incorrect = "That section of the model is often called the measurement model.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "What would you label `ind60` predicting `dem60` and `dem65`?", answer("Structural model", correct = TRUE), incorrect = "That section of the model is often called the structural model.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "Is `ind60` an endogenous or exogenous variable? ", answer("Exogenous", correct = TRUE), incorrect = "This variable is exogenous because arrows only go out of the variable.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "Is `dem60` an endogenous or exogenous variable? ", answer("Both", correct = TRUE), incorrect = "This variable is both!", try_again_button = "Modify your answer", allow_retry = TRUE )
Here's a visualization of the model using semPlot
, which shows you all the estimated paths.
library(semPlot) semPaths(fit)
question_text( "How many variances (error/latent variable) are estimated?", answer("14", correct = TRUE), incorrect = "There are 14 variances estimated in this model (look at the double headed arrows on the variables).", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "How many regressions are estimated?", answer("3", correct = TRUE), incorrect = "There are three regressions estimated between the latent variables. Remember, the others are called loadings!", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "How many loadings are estimated?", answer("8", correct = TRUE), incorrect = "There are eight loadings estimated. Do not forget that you will use a marker variable, which are the dotted lines on this picture.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "How many covariances are estimated?", answer("6", correct = TRUE), incorrect = "There are six covariances between the y variables.", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "How many *possible* parameters can you estimate? ", answer("66", correct = TRUE), incorrect = "11 * (11+1) / 2 = 66", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "How many *parameters* are you estimating? (add variances, regressions, loadings, covariances)", answer("31", correct = TRUE), incorrect = "6+8+3+14 = 31", try_again_button = "Modify your answer", allow_retry = TRUE )
question_text( "Given the previous two answers, what is the *df* for your model?", answer("35", correct = TRUE), incorrect = "66 - 31 = 35", try_again_button = "Modify your answer", allow_retry = TRUE )
You can check your work against the model summary provided below. It's ok if you get it wrong! Learning how to read model diagrams and know what to expect is an important part of learning SEM.
summary(fit)
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.