# Generated quantities block problems to use

**URL:** <https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714>\
**Category:** General\
**Tags:** rstan, fitting-issues\
**Created:** [October 15, 2020, 11:40pm UTC](https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714 "2020-10-15T23:40:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vibass7](https://avatars.discourse-cdn.com/v4/letter/v/dc4da7/32.png) [@Vibass7](https://discourse.mc-stan.org/u/Vibass7)\
**Post date:** [October 15, 2020, 11:40pm UTC](https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714/1 "2020-10-15T23:40:19Z")

</div>

i’m building a standard linear regression model and i want to include the `generated quantities` block and i want to use the `dot_self()` function. The problem is I can´t get simulation samples. The error is: `Stan model 'LinearRegression' does not contain samples.` . I think the the function `dot_self()` is not being recognized as a function. I show `stan` code and `R` code here. Thanks in advance.

Note: I am sure that the data entered is correct because the model without the `generated quantities` block works perfectly.

STAN CODE

```
data {          
  int<lower=1> N;           
  int<lower=1> K;           
  matrix[N, K] X;           
  vector[N] y;              
}
parameters {                
  vector[K] beta;           
  real<lower=0> sigma;      
}
model{                     
  vector[N] mu;             
  mu = X * beta;             

  beta ~ normal(0, 10);
  sigma ~ cauchy(0, 5);                                     
  y ~ normal(mu, sigma);    
}

generated quantities {
  real rss;                
  real totalss;
  real<lower=0, upper=1> R2;                 
  vector[N] mu;
  mu=X * beta;
  rss=dot_self(y-mu);
  totalss=dot_self(y-mean(y));
  R2=1 - rss/totalss;
}

```

R CODE

```
library(rstan)
library(coda)
library(ggplot2)
rstan_options(auto_write=T)
options(mc.cores=parallel::detectCores())

dat=list(N=N, K=ncol(X), y=y, X=X)
fit3 = stan(file = "C:.... LinearRegression.stan", data = dat, iter = 100,chains = 4)

print(fit3, digits=3, prob=c(.025,.5,.975))

```

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [October 16, 2020, 12:02am UTC](https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714/2 "2020-10-16T00:02:26Z")

</div>

Hi @Vibass7 welcome to the Stan forums.

> [@Vibass7](#):
>
> The error is: `Stan model 'LinearRegression' does not contain samples.`

Can you try running your model with argument `cores=1` and checking what the error message is? When using multiple cores some of the error message can get suppressed, so it’s possible that there is some useful information about the error that isn’t being displayed.

---

<div class="post-metadata">

**Author:** ![Vibass7](https://avatars.discourse-cdn.com/v4/letter/v/dc4da7/32.png) [@Vibass7](https://discourse.mc-stan.org/u/Vibass7)\
**Post date:** [October 16, 2020, 1:37am UTC](https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714/3 "2020-10-16T01:37:57Z")

</div>

Thanks ! @jonah I ran my model with `cores=1` and I have this errors now.

```
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                                                                                                                                                              
[2] " Exception: model307034ec1faa_LinearRegression_namespace::write_array: R2 is -6.02356, but must be greater than or equal to 0 (in 'model307034ec1faa_LinearRegression' at line 21)"
[3] "In addition: Warning message:"                                                                                                                                                                                 
[4] "In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :"                                                                                                                                     
[5] " 'C:/rtools40/usr/mingw_/bin/g++' not found"                                                                                                                                                                  
[1] "error occurred during calling the sampler; sampling not done"

```

And this WARNING

```
Chain 1: Gradient evaluation took 0.001 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 10 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: WARNING: There aren't enough warmup iterations to fit the
Chain 1: three stages of adaptation as currently configured.
Chain 1: Reducing each adaptation stage to 15%/75%/10% of
Chain 1: the given number of warmup iterations:
Chain 1: init_buffer = 7
Chain 1: adapt_window = 38
Chain 1: term_buffer = 5

```

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [October 20, 2020, 8:17pm UTC](https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714/4 "2020-10-20T20:17:31Z")

</div>

Sorry it took me so long to respond! I try to bookmark posts I need to respond to but forgot for this one.

> [@Vibass7](#):
>
> `Exception: model307034ec1faa_LinearRegression_namespace::write_array: R2 is -6.02356, but must be greater than or equal to 0 `

This is the key part of the error. It looks like you’re getting negative values for something declared to be greater than 0. In this case it must be that rss/totalss is greater than 1 so that when you subtract it from 1 you get a negative number. Have you seen this paper we have on Bayesian R^2?

[https://www.tandfonline.com/doi/abs/10.1080/00031305.2018.1549100](https://www.tandfonline.com/doi/abs/10.1080/00031305.2018.1549100)

I think that might help. If you don’t have access to the journal here’s a preprint from Andrew’s website: [http://www.stat.columbia.edu/~gelman/research/published/bayes\_R2\_v3.pdf](http://www.stat.columbia.edu/~gelman/research/published/bayes_R2_v3.pdf)

And this warning

> [@Vibass7](#):
>
> ```no-highlight
> Chain 1: WARNING: There aren't enough warmup iterations to fit the
> Chain 1: three stages of adaptation as currently configured.
> 
> ```

is saying that you’ve told it to run for too few warmup iterations for Stan to do its normal recommended warmup process. I don’t think that’s causing this error you’re seeing, but it’s something to be aware of.

---

<div class="post-metadata">

**Author:** ![Vibass7](https://avatars.discourse-cdn.com/v4/letter/v/dc4da7/32.png) [@Vibass7](https://discourse.mc-stan.org/u/Vibass7)\
**Post date:** [October 21, 2020, 7:54pm UTC](https://discourse.mc-stan.org/t/generated-quantities-block-problems-to-use/18714/5 "2020-10-21T19:54:25Z")

</div>

@jonah Thank you so much ! I´m going to read the paper.  
David
