You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because I need to fit multiple SEMs with weights, I decided to make a convenience function for this. However, it seems that lavaan.survey() fails to work inside a function environment. My function:
sem_weights = function(model_str, data, weights) {
#fit without weights
fit = sem(model_str, data = data)
#with weights
design = svydesign(ids = ~1,
data = data,
weights = weights)
fit_wtd = lavaan.survey(fit, design)
fit_wtd
}
Calling this with the appropriate parameters:
mod = "Sepal.Length ~ Sepal.Width"
fit = sem_weights(model_str = mod, data = iris, weights = runif(n = nrow(iris), min = 1, max = 2))
Gives a not found error:
Error in lavaan::lavaan(model = model_str, model.type = "sem", int.ov.free = TRUE, :
object 'model_str' not found
I have tried renaming the object but that didn't help. My guess is that lavaan is searching for the model object in the wrong environment.
Running the same code not using the function works fine:
#fit without weights
fit = sem(model = mod, data = iris)
#with weights
design = svydesign(ids = ~1,
data = iris,
weights = runif(n = nrow(iris), min = 1, max = 2))
fit_wtd = lavaan.survey(fit, design)
#no errors
One can also make the function work by making sure that the model object exists under that name in the global environment, but that defeats the purpose of using a function.
Ideas?
The text was updated successfully, but these errors were encountered:
Because I need to fit multiple SEMs with weights, I decided to make a convenience function for this. However, it seems that
lavaan.survey()
fails to work inside a function environment. My function:Calling this with the appropriate parameters:
Gives a not found error:
I have tried renaming the object but that didn't help. My guess is that
lavaan
is searching for the model object in the wrong environment.Running the same code not using the function works fine:
One can also make the function work by making sure that the model object exists under that name in the global environment, but that defeats the purpose of using a function.
Ideas?
The text was updated successfully, but these errors were encountered: