stan packages erro

Everything related to the development of modules in jamovi
Jinghaom
Posts: 13
Joined: Mon Nov 05, 2018 1:05 pm

stan packages erro

Post by Jinghaom »

hello,
I am currently learning module development, I am trying to develop a module based on the stan package MCMC for Bayesian inference. I followed the tutorial to write a function that can call stan, but I got an error in jamovi. The error code in developer mode is as follows

Code: Select all

Debug Error in file(con, "r"): cannot open the connection private$.run()
stan(model_code = mode, data = stan_data, iter = 27000, warmup = 2000, chains = 4, cores = 4)
stan_model(file, model_name = model_name, model_code = model_code, stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib, save_dso = save cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap(\"", model_name, "\");", sep = ""), includes = inc, plugin = "rstan", save_dso = sav pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, ..., verbose = verbose)) withr::with_path(rtools_path(), code) force(code)
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, ..., verbose = verbose) compileCode(f, code, language = language, verbose = verbose) readLines(errfile)
file(con, "r")
When I run this module with r, stan runs correctly. (Although there was an error because of the code I wrote)

Code: Select all

devtools::install()
library(learn)
data = ToothGrowth
learn::tteststan(
  data = data,
  dep = "len",
  group = "supp")

Error in cpp_object_initializer(.self, .refClassDef, ...) : 
  could not find function "cpp_object_initializer"
In addition: Warning messages:
1: In FUN(X[[i]], ...) : data with name X1 is not numeric and not used
2: In FUN(X[[i]], ...) : data with name X2 is not numeric and not used
failed to create the sampler; sampling not done
I am sure this is a stan error, but stan works fine in my r. So I think this is the error that jamovic's r generates when calling stan. I tried a lot of methods and didn't solve the problem. Help.
Jmo file and source code I uploaded in Google Cloud

jmo:https://drive.google.com/file/d/1551f3- ... sp=sharing
all:https://drive.google.com/file/d/1eYttHW ... sp=sharing

My English is not very good, so I sent this post with Google Translate. There may be places that are difficult to understand.

Thank
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: stan packages erro

Post by jonathon »

hi,

this could be the issue here:

https://github.com/stan-dev/rstan/issues/353

try adding to your DESCRIPTION

Imports: Rcpp

and to your NAMESPACE

importFrom(Rcpp, sourceCpp)

see if that helps

kind regards

jonathon
Jinghaom
Posts: 13
Joined: Mon Nov 05, 2018 1:05 pm

Re: stan packages erro

Post by Jinghaom »

hello
Thank you for your reply

I did what you said. But still reported an error.

Code: Select all

Debug Error in file(con, "r"): cannot open the connection private$.run()
stan_model(model_code = mode)
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap(\"", model_name, "\");", sep = ""), includes = inc, plugin = "rstan", save_dso = sav pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, ..., verbose = verbose)) withr::with_path(rtools_path(), code) force(code)
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, ..., verbose = verbose) compileCode(f, code, language = language, verbose = verbose) readLines(errfile)
file(con, "r")
However, when I run with R, this time I can run normally. So I think maybe what happened to jamov when calling stan?

Code: Select all

> devtools::install()
> library(learn)

> data = ToothGrowth
> learn::tteststan(
+   data = data,
+   dep = "len",
+   group = "supp")

 INDEPENDENT SAMPLES T-TEST USE STAN
Inference for Stan model: 7b4ef4af8e6ae3d3b15a5316d81bb37f.
4 chains, each with iter=27000; warmup=2000; thin=1; 
post-warmup draws per chain=25000, total post-warmup draws=1e+05.

              mean se_mean   sd    2.5%     25%     50%     75%   97.5% n_eff Rhat
mu1          20.66    0.00 1.28   18.14   19.82   20.66   21.50   23.18 94333    1
mu2          16.96    0.01 1.60   13.81   15.91   16.95   18.02   20.11 96909    1
sigma1        6.91    0.00 0.96    5.35    6.23    6.81    7.48    9.08 85519    1
sigma2        8.65    0.00 1.21    6.67    7.80    8.52    9.35   11.41 83641    1
delta         3.70    0.01 2.04   -0.34    2.34    3.70    5.06    7.72 95973    1
delta_over    0.96    0.00 0.19    0.00    1.00    1.00    1.00    1.00 78298    1
lp__       -147.07    0.01 1.46 -150.75 -147.78 -146.73 -145.99 -145.26 47127    1

Samples were drawn using NUTS(diag_e) at Tue Nov 06 23:14:54 2018.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).
new jmo file: https://drive.google.com/file/d/1wK5_DT ... sp=sharing
all file: https://drive.google.com/file/d/1wK5_DT ... sp=sharing

thank you very much
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: stan packages erro

Post by jonathon »

hi,

this is stan throwing an error, however the error message isn't very informative. try asking the stan people what normally causes this. stan is assuming something which isn't quite true in jamovi and throwing an error. but without understanding the error, we can't work around it.

if you post on their mailing list or whatever, let me know, and i can join the discussion.

jonathon
Jinghaom
Posts: 13
Joined: Mon Nov 05, 2018 1:05 pm

Re: stan packages erro

Post by Jinghaom »

hi,

thank you for your reply

I asked on the github in rstan.
Look forward to your participation, thank you.
https://github.com/stan-dev/rstan/issues/584

Jinghao
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: stan packages erro

Post by jonathon »

OK, i've figured it out.

this will be fixed in the next release.

with thanks

jonathon
Jinghaom
Posts: 13
Joined: Mon Nov 05, 2018 1:05 pm

Re: stan packages erro

Post by Jinghaom »

thank you very much

Although I didn't understand the problem at all, I solved it and looked forward to the next version of the update.
If possible, can you please let me know if you have updated the version that fixes this issue.

Thank you

Jinghao
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: stan packages erro

Post by jonathon »

in the mean time, if you add this line of code before your call to stan, it should work in the current version:

Sys.setenv(PATH=paste0(Sys.getenv('PATH'), ';C:\\Windows\\System32'))

with thanks

jonathon
Jinghaom
Posts: 13
Joined: Mon Nov 05, 2018 1:05 pm

Re: stan packages erro

Post by Jinghaom »

hello

It does work. However, because I used the old version of stan, there were other errors.
I tried to delete the build file to let jamovi re-download the new package.
But when I execute
Jmvtools:: install ()

Jamovi compiler

Jamovi found at C:\jamovi-0.9.5.8-win64\bin\jamovi.exe
Wrote: test.h.R
Wrote: test.u.yaml
Wrote: test.src.js
Wrote: test.js
Wrote: ttest.h.R
Wrote: ttest.u.yaml
Wrote: ttest.src.js
Wrote: ttest.js
Wrote: tteststan.h.R
Written: tteststan.u.yaml
Wrote: tteststan.src.js
Written: tteststan.js
Writing module meta
Written: 0000.yaml
Written: jamovi.yaml
ERROR: dependency 'rstan' is not available for package 'learn'
* removing 'C:/Users/jinghaom/OneDrive/R/jamovi-module/learn/build/R/learn'

Could not build module

Looks like jamov doesn't have the package needed for normal downloads?
By the way, what do I do when I want to update the version of the r package in jamovi?

Thank you

Jinghao
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: stan packages erro

Post by jonathon »

hi jinghao,

that is unusual. normally, if you delete the build/ folder, it will download the latest versions of all the dependencies/imports, and bundle them in the module. i'm not sure why it isn't downloading it. try deleting the whole build/ folder, and try again.

let me know how you get on.

cheers

jonathon
Post Reply