Hi!
I am using “rstan” and exercising with a multiple regression model.
I found that when I included the “generated quantities” in the Stan code, an error appeared.
Here is the code.
set.seed(123)
rm(list=ls())
library(rstan)
N = 250
K = 3
covariates = replicate(K, rnorm(n=N))
colnames(covariates) = c('X1','X2','X3')
X = cbind(Intercept=1, covariates)
coefs = c(5, .2, -1.5, .9)
mu = X %*% coefs
sigma = 2
y = rnorm(N, mu, sigma)
dat = list(N=N, K=ncol(X), y=y, X=X)
stanmodelcode = "
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);
}
"
stanmodelcodeRsqr = "
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;
}
"
fitmodel = stan(model_code = stanmodelcode,
data = dat,
iter = 5000,
warmup = 2500,
thin = 10,
chains = 1)
fitRsq = stan(model_code = stanmodelcodeRsqr,
data = dat,
iter = 5000,
warmup = 2500,
thin = 10,
chains = 1)
When I run this code, “fitmodel” runs successfully, but “fitRsq” does not with the error message:
[1] "Chain 1: Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"
[2] "Chain 1: Exception: normal_lpdf: Scale parameter is 0, but must be > 0! (in 'model36661a4a4b97_6a85a8dbcc29774478602cd182a22e81' at line 20)"
[3] "Chain 1: If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[4] "Chain 1: but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."
[5] "Chain 1: "
[6] "Error in sampler$call_sampler(args_list[[i]]) : "
[7] " Exception: model36661a4a4b97_6a85a8dbcc29774478602cd182a22e81_namespace::write_array: R2 is -1.99745, but must be greater than or equal to 0 (in 'model36661a4a4b97_6a85a8dbcc29774478602cd182a22e81' at line 26)"
error occurred during calling the sampler; sampling not done
Do you have any suggestions?
add]
I found that the error disappeared when I change the boundary of R2 from
real<lower=0, upper=1> R2;
to
real R2
.
But cannot I constrain the range of the generated quantities?