Search code examples
rloopsparametersextractr-lavaan

How can I extract one specific coefficient from multiple lavaan models?


I wrote a function to run several lavaan models at once (from 5 different datasets). In the output I get the 5 different outputs. However, I would like to extract one specific estimate from each of these models, because I am using these in a meta-analysis (and I have many more models)

Here is my code for running the model:

df_list <- list ('Y1'=emo_dyn_1,'Y2'=emo_dyn_2,'Y3'=emo_dyn_3,'Y4'=emo_dyn_4,'Y5'=emo_dyn_5)

model <- 'DepB ~ isdNA + imeanNA + sex + age'

fun = function(emo_dyn){
  fit=sem(model,
          data=emo_dyn,
          estimator = "MLR", 
          missing = "ml.x")
  summ = summary(fit, standardized = TRUE)
  
  list(fit = fit,summary = summ)
  
}

results <- lapply(df_list,fun)
names(results) <- names(df_list)
results

And this is how I extract the coefficient. It kinda makes it a dataframe and then I extract the specific value from it. Not sure if that is the best option. It is about the standardized estimate of a specific path. But it is just copy and paste and I am sure this goes easier, but I don't know how to write this loop.

emo_dyn_1_est<-standardizedSolution(results$Y1$fit) # Standardised coefficients
emo_dyn_1_est_1<-emo_dyn_1_est[1, 4]
emo_dyn_1_est_1

emo_dyn_2_est<-standardizedSolution(results$Y2$fit) # Standardised coefficients
emo_dyn_2_est_2<-emo_dyn_2_est[1, 4]
emo_dyn_2_est_2

emo_dyn_3_est<-standardizedSolution(results$Y3$fit) # Standardised coefficients
emo_dyn_3_est_3<-emo_dyn_3_est[1, 4]
emo_dyn_3_est_3

emo_dyn_4_est<-standardizedSolution(results$Y4$fit) # Standardised coefficients
emo_dyn_4_est_4<-emo_dyn_4_est[1, 4]
emo_dyn_4_est_4

emo_dyn_5_est<-standardizedSolution(results$Y5$fit) # Standardised coefficients
emo_dyn_5_est_5<-emo_dyn_5_est[1, 4]
emo_dyn_5_est_5

Solution

  • lavaan has the parameterEstimates function so you can do something like:

    df_list <- list ('Y1'=emo_dyn_1,'Y2'=emo_dyn_2,'Y3'=emo_dyn_3,'Y4'=emo_dyn_4,'Y5'=emo_dyn_5)
    
    model <- 'DepB ~ isdNA + imeanNA + sex + age'
    
    fun <- function(emo_dyn){
      fit <- sem(model,
              data=emo_dyn,
              estimator = "MLR", 
              missing = "ml.x")
      
      fit
    }
    
    results <- lapply(df_list,fun)
    names(results) <- names(df_list)
    
    ## Get a specific parameter
    get_param <- function(fit, coef_pos) {
        param <- parameterEstimates(fit, standardized = TRUE)[coef_pos, "std.lv"]
        param
    }
    
    lapply(results, get_param, coef_pos = 1)
    

    I made one change: in your lapply to get the results I only kept the model fit. If you want all the summaries you can just do lapply(results, summary). The get_param function assumes that you know the position in the results table of the parameter you want.

    If you want to keep your existing lapply for the results then something like this would work:

    results_fit_only <- lapply(results, "[[", "fit")
    lapply(results_fit_only, get_param, coef_pos = 1)