Function to performed exploratory mediation with continuous and categorical variables
xmed( data, iv, mediators, dv, covariates = NULL, type = "lasso", nfolds = 10, show.lambda = F, epsilon = 0.001, seed = NULL )
data
: Name of the datasetiv
: Name (or vector of names) of independent variable(s)mediators
: Name of mediatorsdv
: Name of dependent variablecovariates
: Name of covariates to be included in model.type
: What type of penalty. Options include lasso, ridge, and enet.nfolds
: Number of cross-validation folds.show.lambda
: Displays lambda values in outputepsilon
: Threshold for determining whether effect is 0 or not.seed
: Set seed to control CV resultsCoefficients from best fitting model
# example library(ISLR) College1 = College[which(College$Private=="Yes"),] Data = data.frame(scale(College1[c("Grad.Rate","Accept","Outstate","Room.Board","Books","Expend")])) Data$Grad.Rate <- ifelse(Data$Grad.Rate > 0,1,0) Data$Grad.Rate <- as.factor(Data$Grad.Rate) #lavaan model with all mediators model1 <- ' # direct effect (c_prime) Grad.Rate ~ c_prime*Accept # mediators Outstate ~ a1*Accept Room.Board ~ a2*Accept Books ~ a3*Accept Expend ~ a6*Accept Grad.Rate ~ b1*Outstate + b2*Room.Board + b3*Books + b6*Expend # indirect effects (a*b) a1b1 := a1*b1 a2b2 := a2*b2 a3b3 := a3*b3 a6b6 := a6*b6 # total effect (c) c := c_prime + (a1*b1) + (a2*b2) + (a3*b3) + (a6*b6) ' #p-value approach using delta method standard errors fit.delta = sem(model1,data=Data,fixed.x=TRUE,ordered="Grad.Rate") summary(fit.delta) #xmed() iv <- "Accept" dv <- "Grad.Rate" mediators <- c("Outstate","Room.Board","Books","Expend") out <- xmed(Data,iv,mediators,dv) out