# How to use the specific stan math branches function in rstan?

Short summary of the problem:

Operating System: windows 11
Interface Version: rstan 2.32.3
Compiler/Toolkit: rtools 43

I want to use the wiener_full_lpdf function in rstan, when I run my code in rstudio, it returns :

``````A returning function was expected but an undeclared identifier 'wiener_full_lpdf' was supplied.
``````

But I found the function in GitHub stan-dev/math branch “fix/wiener5_fixes”. My question is how to use the function in that branch? Here is my stan code

``````data {
int<lower=1> P;            // Number of participants
int<lower=1> I;            // Number of trials per participant
array[P,I] real<lower=0> rt;           // Response times for each participant
array[P,I] int<lower=0, upper=1> resp;         // Responses for each participant
}

parameters {
array[P] real gamma;     // decision criterion for each participant
array[P] real theta;     // ability theta for each participant
array[P] real<lower=0> t0;        // Non-decision time for each participant

array[I] real a;         // tiem pressure m for each item
array[I] real b;         // difficulty b for each item
array[I] real<lower=0> sv;        // Variability in drift for each item
array[I] real<lower=0, upper=1> szr; // Variability in starting point for each item

array[P,I] real<lower=0, upper=1> cs;  // Parameters following Bernoulli distribution
}

model {

gamma ~ normal(1, 1);
theta ~ normal(2, 3);
t0 ~ normal(0.183, 0.09);
a ~ normal(0.5, 0.1);
b ~ normal(0.435, 0.12);
sv ~ normal(1, 3);
szr ~ beta(1, 3);

for (i in 1:P) {
for (j in 1:I) {
array[P,I] real alpha;
array[P,I] real v;
array[P,I] real sw;
alpha[i,j] = gamma[i] / a[j];
v[i,j] = theta[i] - b[j];
sw[i,j] = szr[j] * alpha[i,j];

if (resp[i, j] == 1) {
target += wiener_full_lpdf(rt[i, j] | alpha, t0[i], 0.5, v[i,j], sv[j], sw[i,j], 0);
} else {
target += wiener_full_lpdf(rt[i, j] | alpha, t0[i], 0.5, -v[i,j], sv[j], sw, 0);
}
}
}
}

``````

According to the tutorials on the website"Interfacing with External C++ Code", I download the “wiener_full_lpdf.hpp” in my rstudio working directory, and my code change to:

``````stancode <-
'
functions{
real wiener_full_lpdf(real rt, real alpha, real t0, real w, real v, real sv, real sw, real st0);}

data {
int<lower=1> P;            // Number of participants
int<lower=1> I;            // Number of trials per participant
array[P,I] real<lower=0> rt;           // Response times for each participant
array[P,I] int<lower=0, upper=1> resp;         // Responses for each participant
}

parameters {
array[P] real gamma;     // decision criterion for each participant
array[P] real theta;     // ability theta for each participant
array[P] real<lower=0> t0;        // Non-decision time for each participant

array[I] real a;         // tiem pressure m for each item
array[I] real b;         // difficulty b for each item
array[I] real<lower=0> sv;        // Variability in drift for each item
array[I] real<lower=0, upper=1> szr; // Variability in starting point for each item

array[P,I] real<lower=0, upper=1> cs;  // Parameters following Bernoulli distribution
}

model {

gamma ~ normal(1, 1);
theta ~ normal(2, 3);
t0 ~ normal(0.183, 0.09);
a ~ normal(0.5, 0.1);
b ~ normal(0.435, 0.12);
sv ~ normal(1, 3);
szr ~ beta(1, 3);

for (i in 1:P) {
for (j in 1:I) {
array[P,I] real alpha;
array[P,I] real v;
array[P,I] real sw;
alpha[i,j] = gamma[i] / a[j];
v[i,j] = theta[i] - b[j];
sw[i,j] = szr[j] * alpha[i,j];

if (resp[i, j] == 1) {
target += wiener_full_lpdf(rt[i, j] | alpha[i,j], t0[i], 0.5, v[i,j], sv[j], sw[i,j], 0.1);
} else {
target += wiener_full_lpdf(rt[i, j] | alpha[i,j], t0[i], 0.5, -v[i,j], sv[j], sw[i,j], 0.1);
}
}
}
}
'
mod <- stan_model(model_code = stancode, model_name = "external", allow_undefined = TRUE,verbose = TRUE,
includes = paste0('\n#include "', getwd(), '/wiener_full_lpdf.hpp"\n'))

``````

when I run the above code in the rstudio, it returns:

``````Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
|               ^~~~~~~~~~~~~~~~file28e4216157e2.cpp:534:15: note:   template argument deduction/substitution failed:file28e4216157e2.cpp:993:23: note:   couldn't deduce template parameter '<anonymous>'  993 |       write_array_impl(base_rng, params_r, params_i, vars, emit_transformed_parameters, emit_generated_quantities, pstream);      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~make: *** [E:/Program Files/R/R-4.3.1/etc/x64/Makeconf:272: file28e4216157e2.o] Error 1
``````

I think I need some help with this question

If it’s a function that exists in a different branch and you just need a different math branch, it’s easiest to do that through CmdStanR. Just check out CmdStan, then cd to the `stan` directory and check out the appropriate Stan submodule (and matching math module).

``````> git clone ...cmdstan...
> cd cmdstan/stan
> git checkout <branch you want of Stan>
> make math-update
``````

The math update makes sure you have the matching version of the math lib for the version of Stan you’re using. If you want just a new version of the math lib, then

``````> cd cmdstan/stan/lib/stan_math
> git checkout <branch of Math lib you want>
``````

It’s trivial to swap versions of Stan in CmdStanR as it doesn’t involve rebuilding anything in R.

If it’s a truly new function, you can declare the function in the functions block of the Stan program (without a body) and then link in the external library. Otherwise, you have to add it to the language through OCaml. You have to be careful the function has enough template traits implemented to uniquely pick out the signature that you want. Many of our functions are written with heavy overloading in the math library that cannot be resolved for arbitrary calls, but are sufficient for the internal calling pattern.

thank a lot.