I want to get parameter estimates for a set of lavaan models, but in a way that for different type of model, different data frame is used. The model name contains the information about what data frame to use for fitting the model. It seems that my if () { } else { } statements always end up selecting the else-statement, although it shouldn't. Below is an example of such case. I would like to know, if the problem is with the custom function or with lapply (if it can't handle "if else" in this way)?
library(lavaan)
set.seed(123)
dat.a<-data.frame(X=rnorm(100),Y=rnorm(100))
dat.b<-data.frame(X=rnorm(100),Y=rnorm(100))
model.a<-'
X~~Y'
model.b<-'
X~~Y'
get.ests<-function(model){
if (grepl("a",as.character(deparse(substitute(model))))) {
fit.dat<-dat.a
} else {
fit.dat<-dat.b
}
fit<-lavaan::sem(model,data=fit.dat)
ests<-lavaan::parameterestimates(fit)
return(ests)
}
lapply returns same parameter estimates for both type of models
lapply(list(model.a,model.b),get.ests)
Although, the parameter estimates are different
parameterestimates(lavaan::sem(model.a,data=dat.a))
parameterestimates(lavaan::sem(model.b,data=dat.b))
Aucun commentaire:
Enregistrer un commentaire