Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

r - 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
question from:https://stackoverflow.com/questions/65935039/how-can-i-extract-one-specific-coefficient-from-multiple-lavaan-models

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...