Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model not found: using lavaan.survey() inside a function environment #2

Open
Deleetdk opened this issue Feb 5, 2016 · 0 comments
Open

Comments

@Deleetdk
Copy link

Deleetdk commented Feb 5, 2016

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant