【问题标题】:Generated quantities block in stan modelstan 模型中的生成量块
【发布时间】: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))

【问题讨论】:

    标签: r stan rstan


    【解决方案1】:

    错误是由于 R2 上的界限造成的。我相信没有必要对生成的数量施加限制。

    这里我使用了模拟的 X 和 y:

    X = matrix(runif(N*K), N, K)
    y = rowSums(X)
    

    去掉边界后的结果是:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多