Open AlexGareau opened 1 year ago
I was able to replicate the two solutions with sampling.weights. I learned that measurement() and estimate_lavaan() could take function from cfa() or sem() I also learned how to manipulate the data dictionary, even though I wished it could follow the pipe sequence with no interruption, I understand better the inner working of tidySEM.
this is the lavaan solution
df %>%
select(any_of(items), WGT) %>%
mutate(across(where(is.factor), as.numeric)) %>%
cfa(cfa, data = .,
estimator = "MLR",
sampling.weights = "WGT",
meanstructure = T,
auto.fix.first = F,
std.lv = T)
this is the tidySEM solution which uses lavaan
model <- df %>%
select(any_of(items),WGT) %>%
mutate(across(where(is.factor), as.numeric)) %>%
tidy_sem()
dictionary(model) %>%
slice(-14) %>% #to take WGT out of the dictionnary
mutate(scale = "QOL") %>% #to associate all indicators to the same scale
rbind(c("QOL",NA,"latent","QOL")) -> dictionary(model)
model %>%
measurement(auto.fix.first = F, std.lv = T) %>%
estimate_lavaan(estimator = "MLR", sampling.weights = "WGT")
I would say the biggest advantage of tidySEM is the syntax generating process. Because generating lavaan syntax programmatically is not the prettiest.
cfa <-
paste(sep = "\n",
paste0("QoL = ~",
paste(head(rank$qol[!is.na(rank[[2]])], -1), collapse = " + ")))
I had the pleasure to experience the ease of pipe-sequence with tidySEM. Truly amazing for generating model syntax. However, since sampling.weight should stay as a column in the data.frame for the lavaan argument, tidySEM read this column as an observed variable. I wonder if I just hit a crucial limit of tidySEM, or if I am just not seing it from the right coding perspective.
df %>% select(all_of(items),WGT) %>% cfa(model, data = ., estimator = "MLR", sampling.weights = "WGT", meanstructure = T)
df %>% select(all_of(items),WGT) %>% tidy_sem() %>% measurement() %>% estimate_lavaan(.,"cfa", estimator = "mlr", sampling.weights = "WGT)