【发布时间】:2020-10-14 03:01:04
【问题描述】:
我正在构建一个标准的线性回归模型,我想包含generated quantities 块,我想使用dot_self() 函数。问题是我无法获得模拟样本。错误是:Stan model 'LinearRegression' does not contain samples.。我认为函数dot_self() 没有被识别为函数。
我在这里显示stan 代码和R 代码。
提前致谢。
注意:我确信输入的数据是正确的,因为没有generated quantities 块的模型可以正常工作。
斯坦代码:
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;
}
运行 Stan 模型的 R 代码:
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))
【问题讨论】: