In this (https://mc-stan.org/rstan/articles/external.html) article, it is suggested that the rstan “includes” option can be used to allow a stan model to calculate a derivative in c++. I’m having trouble finding examples of this, and my recent attempts at implementing this have been fruitless. The code for the derivative is coming from this (https://arxiv.org/pdf/1509.07164.pdf) paper; for this instance I’m trying to use a gamma. The R call is:

```
library(rstan)
library(rstanarm)
library(ggplot2)
library(StanHeaders)
fitted_model_fid1 = stan_model( "gamma.stan", allow_undefined = TRUE,verbose = TRUE,
includes = paste0('\n#include "',
file.path(getwd(), 'lgammaJacLinf.hpp'), '"\n'))
```

where my stan model is

```
functions {
real lgammaJacLinf(vector x, real alpha, real lambda);
}
data {
int<lower=1> N;
vector[N] y;
}
parameters {
real<lower=0> alpha;
real<lower=0> lambda;
}
model {
y ~ gamma_lpdf(alpha,1/lambda);
target+=lgammaJacLinf(y,alpha,lambda);
}
```

and the two header files I want to use are attached. Any advice would be quite appreciated. I asked something similar on this (Trying to run autodifferentiation example) thread, and have since confirmed that I am not running Catalina. I decided to start a new thread to make my issue clear and to lay out completely my progress.

gammaCDFderiv.hpp (226 Bytes) lgammaJacLinf.hpp (737 Bytes)