Comments (9)
Thanks, this sounds like a good addition. I believe that blavFitIndices() is calling fitMeasures() to obtain loo and waic, so it will probably take modification of at least two functions to do this. Any initial stab you take would be welcome, and I can clean up any parts of it that are confusing.
PS I had to resist saying "This is a premium addition that can be purchased for 500 robux" :)
from blavaan.
Thanks for looking at it, and here is what I think is happening.
In the big Stan file, I post-process a number of parameters in generated quantities, and then I only save the post-processed versions (using the pars
argument of stan()
). I do this to save memory (to avoid saving redundant parameters). But it is also possible to save extra parameters using blavaan's mcmcextra
argument. So, to check that this is the problem, I think you could do this:
- Compare the parameters in the Stan file to the parameters that blavaan saves by default.
- For things that appear in the parameters block of Stan but not in the blavaan saved parameters, save them using the
mcmcextra
argument. It would look roughly like
fit <- bsem(model, data, other_arguments, mcmcextra = list(monitor = c("Lambda_y_free", "more_pars"))
Then, hopefully the moment matching would work on fit
. If so, I think we could still add a ...
, but maybe the user has to know to also use mcmcextra
. (I am not sure I want to save redundant parameters by default, though maybe I could be convinced if there were multiple functions that need everything from the parameters block.)
from blavaan.
I won't get to it by the weekend, so will build off of anything you send!
from blavaan.
I think it is because of the way that add_monitors()
works inside that if statement. I think add_monitors()
was written to expect JAGS results, because blavaan was written for JAGS first. The extra monitors still exist for Stan, they just are not included in the parameter table.
from blavaan.
Hey @ecmerkle,
It looks like the situation is a bit more difficult than I anticipated. Moment matching is only available when applying loo
to a stanfit
object. I can extract the stanfit
from blavaan
using fitstanfs_mcobj <- blavInspect(fitstanfs, "mcobj")
.
loo(fitstan_mcobj)
works as expected. However, loo(fitstanfs_mcobj, moment_match = TRUE)
gives the same error, regardless of the model used:
Error in [email protected]$stan_fit_instance$unconstrain_pars(pars) :
Exception: Variable Lambda_y_free missing (in 'model_stanmarg' at line 576)
For reference, this is what lines around 576 look like:
parameters {
// free elements (possibly with inequality constraints) for coefficient matrices
vector[len_free[1]] Lambda_y_free;
//vector[len_free[2]] Lambda_x_free;
vector[len_free[3]] Gamma_free;
...
I think loo
is calling the loo_moment_match
function, which in turn is calling the unconstrain_pars
method attached to the stanfit. I don't know much about stan, but it looks like Lambda_y_free
is already unconstrained, but I haven't come up with a way of fixing this. Do you have any ideas?
from blavaan.
That works! Unfortunately, moment matching is not going to save my model, but I can add changes.
It looks like loo_moment_match
is only implemented for rstan
models and will require all the free variables in the stan model. Because this is a pretty narrow use case, I would add an argument to the blavaan
constructor called something like moment_match_k_threshold
that defaults to NULL
. If a value > 0.5
is input, the blavaan
object will monitor all the free variables and blavFitIndices
will default to loo
with moment matching instead of loo
. Otherwise, it will default to the typical loo
.
Does that work for you?
from blavaan.
I'd glad it worked, even if it didn't help you! About the extra argument: I am trying to keep extra settings like this in mcmcextra
. For example, there is a llnsamp
argument to control the number of samples to approximate the likelihood of ordinal models; see here.
Something could be done similarly here, where moment_match_k_threshold
is supplied to mcmcextra$data, then blavaan knows what to do with it. Maybe this would be easiest for me to do, because I recently did that llnsamp
thing.
On the other hand, maybe the current functionality is enough since we can supply extra parameters to mcmcextra
. (particularly if I add an example of how to do it, say here). Let me know what you think.
from blavaan.
I think it would make sense to add moment_match_k_threshold
to mcmcdata
. My reasoning is that some free variables will change depending on the modelling decisions, such as theta for ordinal models. You would also have to change bitFitIndices
slightly to check for a flag. Currently, loo
takes in llnsamp
directly, but the model is needed when moment matching is used,
I won't stop you if you want to make the changes, but I should be able to get up PR this weekend. Up to you!
from blavaan.
Hey Ed,
I'm curious about this line. Why is the stan
target not allowed here?
Line 906 in c58e805
from blavaan.
Related Issues (20)
- modeling with ordered categorical variables in blavaan HOT 3
- not saving object because of delta parameterization HOT 2
- Model fit and ppmc() with ordinal data HOT 2
- model results become different when save.lvs = TRUE HOT 5
- problem with translation from lavaan to MCMC syntax HOT 3
- Latent variable names missing from blavPredict(type = "lv") HOT 2
- lavoptions$categorical HOT 2
- Growth Mrxture Modelling in blavaan HOT 1
- adjBGammaHat > 1 and BRMSEA is NA HOT 12
- avoid nested lists
- blavFitIndices & multiple groups HOT 1
- blavaan failed to compile (Mac M1) HOT 4
- Bug with multiple groups HOT 1
- `blavCompare` errors HOT 2
- Problem with translation from lavaan to MCMC syntax HOT 9
- blavaan ERROR: problem with translation from lavaan to MCMC syntax HOT 11
- Error in lav2standata(lavobject) : object 'ptot' not found HOT 5
- Problem with multilevel model HOT 1
- error in help-page examples for wrappers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blavaan.