7.4 Exercises#
1️⃣ Measurement Invariance by Age Group (Quantitative Data)#
Goal: Replicate the group invariance analysis by using a different grouping variable.
Instructions:
Re-run the
measurementInvariance()
function usingage_group
(or create one based on age) instead ofgender
.Compare the output to the original model grouped by gender.
Comment on whether strict invariance is still supported under the new grouping.
# Import data
# Use this code to create age groups
ro.r('data("Bergh")') # Load the Bergh dataset from R
Bergh = pandas2ri.rpy2py(ro.globalenv['Bergh']) # Convert the R dataframe to a pandas dataframe
np.random.seed(123) # for reproducibility
Bergh['age'] = np.random.normal(loc=40, scale=10, size=len(Bergh)).astype(int)
Bergh['age_group'] = pd.cut(Bergh['age'], bins=[0, 30, 50, 100], labels=["young", "middle", "older"]) # Group ages into categories
# Your code here:
2️⃣ DIF Detection for Gender (Qualitative Data)#
Goal: Assess whether Differential Item Functioning (DIF) exists across gender instead of race.
Instructions:
Modify the call to
lordif()
by replacingYouthDep$race
withYouthDep$gender
.Interpret the output and compare to the DIF results by race.
Which items appear most sensitive to gender-based DIF?
# Your code here
3️⃣ Testing Invariance with Additional Timepoints#
Goal: Extend the longitudinal CFA to include the year 2000 (if present in the dataset).
Instructions:
Modify the measurement model to add a latent factor
SDO2000
and link it to items measured in 2000.Enforce equal loadings and intercepts across the three timepoints.
Examine model fit and comment on any changes.
If the year 2000 is not in the dataset, simulate a third timepoint by duplicating 1998 responses.
# Your code here