7.4 Exercises#

1️⃣ Measurement Invariance by Age Group (Quantitative Data)#

Goal: Replicate the group invariance analysis by using a different grouping variable.

Instructions:

  1. Re-run the measurementInvariance() function using age_group (or create one based on age) instead of gender.

  2. Compare the output to the original model grouped by gender.

  3. 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:

  1. Modify the call to lordif() by replacing YouthDep$race with YouthDep$gender.

  2. Interpret the output and compare to the DIF results by race.

  3. 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:

  1. Modify the measurement model to add a latent factor SDO2000 and link it to items measured in 2000.

  2. Enforce equal loadings and intercepts across the three timepoints.

  3. Examine model fit and comment on any changes.

  4. If the year 2000 is not in the dataset, simulate a third timepoint by duplicating 1998 responses.

# Your code here