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

Issue saving ulam object when using cmdstanr #443

Closed
FishModeler opened this issue Sep 26, 2024 · 11 comments
Closed

Issue saving ulam object when using cmdstanr #443

FishModeler opened this issue Sep 26, 2024 · 11 comments

Comments

@FishModeler
Copy link

          Hello! Could you potentially clarify this edit - from a student at their wits end with this issue :') 

I run my model and everything finishes fine. So do I immediately run the stanfit <- as_cmdstan_fit(cmdstanfit$output_files()) line of code afterwards? I tried doing that and running the line of code exactly as shown, but it gives me the error "Error: object 'cmdstanfit' not found. I replaced cmdstanfit with the name of my model and it give me the error of "Error in ModelName$output_files : $ operator not defined for the s4 class."

Any help would be greatly appreciated cause this issue has been a huge set back and I've tried to understand all the coding behind the scenes but can't make any sense of it. Thank you!

Originally posted by @FishModeler in #425 (comment)

@FishModeler
Copy link
Author

to add more context, I was previously able to save the ulam as rds, then reload it and get all results no problem. Now when I save as rds, the file is tiny when it should be quiet large. Then the issue described in the saving and reloading ulam pull request occurs.

@FishModeler
Copy link
Author

Here is the code I am working with:

Load In Packages

library(readr)
library(vegan)
library(ggplot2)
library(factoextra)
library(tibble)
library(rethinking) #version from timjzee
library(readxl)
library(rstan)
library(dplyr)
library(WriteXLS)

Cmdstanr is version 0.8.1
Cmdstan is version
R version is 2023.06.0 for Linux
Toolchain is Ubuntu 11.4.0
Computer has 24 CPUs with 320ish GB of memory available.

Here is my code:
Model1<- ulam( alist( Var1Number ~ dzipois(pbar,lamba), logit(pbar) <- z1+Yv[Year]+Mv[Month], log(lamba)<-a+f1*DV1+f2*DV2+f3*DV3+f4*DV4+f5*DV5+f6*DV6+HC[CN]+GR[Gear], a ~ dnorm(19.71,294), f1 ~ dnorm(0,1), f2 ~ dnorm(0,1), f3 ~ dnorm(0,1), f4 ~ dnorm(0,1), f5 ~ dnorm(0,1), f6 ~ dnorm(0,1), z1~dnorm(0,450), Yv[YearR]~dnorm(0,sigmaYear), sigmaYear~dcauchy(0,50), Mv[Month]~dnorm(0,sigmaMonth), sigmaMonth~dcauchy(0,50), HC[Model2CN]~dnorm(0,sigmaHC), sigmaHC~dcauchy(0,50), GR[Gear]~dnorm(0,sigmaGear), sigmaGear~dcauchy(0,10) ), data =FinalTB3Data, start=list(a=0,f1=0,f2=0,f3=0,f4=0,f5=0,f6=0,z1=1), iter=4000,warmup=2500,chains =3, cores = 3, log_lik = TRUE )

It runs fine and says all the chains are completed. Then, when it seems to be processing everything to put the ulam object into the global evironment, I get this error I haven't seen before:
Error in if (max(save_warmup) == 0L {: missing value where TRUE/FALSE needed In addition: Warning messages: 1: in parse_stancsv_comments(comments) : NAs introduced by coercion 2: in parse_stancsv_comments(comments) : NAs introduced by coercion 3: in parse_stancsv_comments(comments) : NAs introduced by coercion

This is all happening on my work computer that is much more "powerful" and all the packages got updated recently.
On my personal computer, the models run just fine, just it just takes forever and I can only store one model output at a time. I can save my models as rda or rds, upload them to the cloud and reopen them just fine on any computer. My personal computer is running:
cmdstanr: 0.5.3
rethinking: 2.31
R: 2023.12.1+402 for Mac

For all those much more knowledgable than I am, if this is a problem with cmdstanr, could I just reload an older version of it?
Thank you so much for any help and insight. This is a problem that has really thrown me for a loop. :-)

@FishModeler
Copy link
Author

FishModeler commented Sep 27, 2024

Let me try putting my model here again since it was formatted weird above:
Model1<- ulam(
alist(
Var1Number ~ dzipois(pbar,lamba),
logit(pbar) <- z1+Yv[Year]+Mv[Month],
log(lamba)<-a+f1DV1+f2DV2+f3DV3+f4DV4+f5DV5+f6DV6+HC[CN]+GR[Gear],
a ~ dnorm(19.71,294),
f1 ~ dnorm(0,1),
f2 ~ dnorm(0,1),
f3 ~ dnorm(0,1),
f4 ~ dnorm(0,1),
f5 ~ dnorm(0,1),
f6 ~ dnorm(0,1),
z1~dnorm(0,450),
Yv[Year]dnorm(0,sigmaYear),
sigmaYear
dcauchy(0,50),
Mv[Month]dnorm(0,sigmaMonth),
sigmaMonth
dcauchy(0,50),
HC[CN]dnorm(0,sigmaHC),
sigmaHC
dcauchy(0,50),
GR[Gear]dnorm(0,sigmaGear),
sigmaGear
dcauchy(0,10)
),
data =FinalTB3Data,
start=list(a=0,f1=0,f2=0,f3=0,f4=0,f5=0,f6=0,z1=1),
iter=4000,warmup=2500,chains =3, cores = 3, log_lik = TRUE
)

@timjzee
Copy link

timjzee commented Sep 30, 2024

As a start, you could try only loading the libraries that you actually need. You are currently loading rstan after having already loaded rethinking. By doing this, you are masking certain functions from rethinking with similar functions from rstan. Just loading rethinking should automatically load all required libraries. By the way, rstan should not be one of those as rethinking now uses cmdstan by default.

@FishModeler
Copy link
Author

Great thank you! I am able to save my outputs now.
Hopefully one last issue, when I extract.samples, I receive the following error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'object' in selecting a method for function 'extract': no slot of name "stanfit" for this object of class "ulam"

@timjzee
Copy link

timjzee commented Oct 2, 2024

What is the output of str(your_object_name)? Obviously replace your_object_name with the name of the model object you are trying to run extract.samples on.

@FishModeler
Copy link
Author

For str(Model) i get something that looks like this for all my variables:
.. .. ..$ PF6 :List of 3
.. .. .. ..$ type : chr "par"
.. .. .. ..$ dims : chr "real"
.. .. .. ..$ constraint: chr NA

Then if I scroll down a little bit more I get outputs that look like this:

.. .. .. ..$ samples :List of 3
.. .. .. .. ..$ :'data.frame': 100 obs. of 45890 variables:
.. .. .. .. .. ..(with all my variables and a bunch of numbers following)

Only included a little bit since the output was several hundred lines long. Thank you!

@timjzee
Copy link

timjzee commented Oct 2, 2024

does it include this line:

..$ cstanfit :Classes 'CmdStanMCMC_CSV', 'CmdStanMCMC', 'CmdStanFit', 'R6' <CmdStanMCMC_CSV>

@FishModeler
Copy link
Author

Almost, I have:
' ..$ cstanfit :Classes 'CmdStanMCMC', 'CmdStanFit', 'R6' '

@timjzee
Copy link

timjzee commented Oct 3, 2024

This suggests that your extract.samples() call expects a stanfit instead of a cstanfit. This would happen if you're on a much older version of rethinking that does not support cmdstan yet. Are you doing this on a different installation of rethinking?

@FishModeler
Copy link
Author

I don't know what happened, but everything works just fine now! Maybe my computer just needed a good restart.
Thank you so much for all your help! You are a lifesaver! :)

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

2 participants