6.3 At-Home Exercises
This week, we’ll take another look at the Kestilä (2006) results. During this practical, you will conduct an SEM to replicate the regression analysis of the Finnish data that you conducted in the Week 4 In-Class Exercises.
6.3.1
Load the Finnish subsample of ESS data.
- The relevant data are contained in the [ess_finland.rds][fin_data] file.
- These are the processed Finnish subsample data from the Week 4 exercises.
Note: Unless otherwise noted, all the following analyses use these data.
We need to do a little data processing before we can fit the regression model.
At the moment, lavaan
will not automatically convert a factor variable into
dummy codes. So, we need to create explicit dummy codes for the two factors
we’ll use as predictors in our regression analysis: sex and political
orientation.
6.3.2
Convert the sex and political interest factors into dummy codes.
Click to show code
library(dplyr)
## Create a dummy codes by broadcasting a logical test on the factor levels:
ess_fin <- mutate(ess_fin,
female = ifelse(sex == "Female", 1, 0),
hi_pol_interest = ifelse(polintr_bin == "High Interest", 1, 0)
)
## Check the results:
with(ess_fin, table(dummy = female, factor = sex))
## factor
## dummy Male Female
## 0 960 0
## 1 0 1040
## factor
## dummy Low Interest High Interest
## 0 1070 0
## 1 0 929
Click for explanation
In R, we have several ways of converting a factor into an appropriate set of dummy codes.
- We could use the
dplyr::recode()
function as we did last week. - We can use the
model.matrix()
function to define a design matrix based on the inherent contrast attribute of the factor.- Missing data will cause problems here.
- We can us
as.numeric()
to revert the factor to its underlying numeric representation {Male = 1, Female = 2} and use arithmetic to convert {1, 2} \(\rightarrow\) {0, 1}.
When our factor only has two levels, though, the ifelse()
function is the
simplest way.
We are now ready to estimate our latent regression model. Specifically, we want
to combine the three OLS regression models that you ran in 4.4.16
into a single SEM that we will estimate in lavaan
.
The following path diagram shows the intended theoretical model.