moderation model orthogonalizing in lavaan in r

Moderation describes the relationship between two variables which is conditional on a third variable. Orthogonalizing a predictor can simplify modeling with moderations, by removing covariation with other predictors. With the lavaan package in R, you can create a moderation model with orthogonalized predictors by following these steps:

  1. Load the lavaan package and your data:

    main.r
    library(lavaan)
    mydata <- read.csv("mydata.csv")
    
    49 chars
    3 lines
  2. Center and standardize your predictors:

    main.r
    mydata$centered_predictor <- scale(mydata$predictor, center = TRUE, scale = TRUE)
    mydata$centered_moderator <- scale(mydata$moderator, center = TRUE, scale = TRUE)
    
    164 chars
    3 lines
  3. Create a model with the orthogonalized predictor as a mediator of the relationship between the original predictor and the outcome:

    main.r
    model <- "
              outcome ~ c(predictor) + c(moderator) + c(predictor.centered) + b*predictor.centered + b2*moderator
              predictor.centered ~ c(predictor) + c(moderator) + b3*moderator
             "
    
    206 chars
    5 lines
  4. Fit the model using the lavaan sem() function:

    main.r
    model_fit <- sem(model, data = mydata)
    
    39 chars
    2 lines
  5. Inspect the results using the summary() function:

    main.r
    summary(model_fit)
    
    19 chars
    2 lines

By orthogonalizing one predictor, you can model conditional effects of that predictor while removing covariation with other predictors. This can lead to a more parsimonious and interpretable model.

gistlibby LogSnag