首页 > 解决方案 > Stan:多元模型的初始化问题

问题描述

已经发布在这里

嗨,我正在尝试第一次使用多元模型。我读了手册。我在它之后做了两个模型(有和没有“通过 Cholesky 分解进行优化”)。在这两种情况下,都存在初始化问题。我将复制每个模型下方的错误消息。观测值 (y) 是整数,可以是正数、负数或零。

  1. 没有 Cholesky 分解
data {
  int<lower=0> N;              // num individuals (observations)
  int<lower=1> K;              // num ind predictors (= nb of species)
  int<lower=1> J;              // num groups (= nb of species)
  int<lower=1> L;              // num group predictors (1?)
  int<lower=1,upper=J> jj[N];  // group for individual (species)
  matrix[N, K] x;              // individual predictors (all abundances at time t)
  row_vector[L] u[J];          // group predictors (1 everywhere)
  vector[N] y;                 // outcomes (abundance of focal species at time t)
}
parameters {
  corr_matrix[K] Omega;        // prior correlation
  vector<lower=0>[K] tau;      // prior scale
  matrix[L, K] gamma;          // group coeffs
  vector[K] beta[J];           // indiv coeffs by group
  real<lower=0> sigma;         // prediction error scale
}
model {
  tau ~ cauchy(0, 2.5);
  Omega ~ lkj_corr(2);
  to_vector(gamma) ~ normal(0, 5);
  {
    row_vector[K] u_gamma[J];
    for (j in 1:J)
      u_gamma[j] = u[j] * gamma;
    beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau));
  }
  {
    vector[N] x_beta_jj;
    for (n in 1:N)
      x_beta_jj[n] = x[n] * beta[jj[n]];
    y ~ normal(x_beta_jj, sigma);
  }
}

错误:

重复消息:

Chain 1: Exception: lkj_corr_lpdf: Shape parameter is 0, but must be > 0!  (in 'model1d032109b2a5f_Model' at line 20)

在最后一条消息中:

Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done
  1. 使用 Cholesky 分解
data {
  int<lower=0> N;              // num individuals (observations)
  int<lower=1> K;              // num ind predictors (= nb of species)
  int<lower=1> J;              // num groups (= nb of species)
  int<lower=1> L;              // num group predictors (1?)
  int<lower=1,upper=J> jj[N];  // group for individual (species)
  matrix[N, K] x;              // individual predictors (all abundances at time t)
  matrix[L, J] u;          // group predictors (1 everywhere)
  vector[N] y;                 // outcomes (abundance of focal species at time t)
}
parameters {
  matrix[K, J] z;
  cholesky_factor_corr[K] L_Omega;
  vector<lower=0,upper=pi()/2>[K] tau_unif;  // prior scale
  matrix[K, L] gamma;                        // group coeffs
  real<lower=0> sigma;                       // prediction error scale
}
transformed parameters {
  vector<lower=0>[K] tau = 2.5 * tan(tau_unif);
  matrix[K, J] beta = gamma * u + diag_pre_multiply(tau, L_Omega) * z;
}
model {
  vector[N] mu;
  for(n in 1:N) {
    mu[n] = x[n, ] * beta[, jj[n]];
  }
  to_vector(z) ~ std_normal();
  L_Omega ~ lkj_corr_cholesky(2);
  to_vector(gamma) ~ normal(0, 5);
  y ~ normal(mu, sigma);
}

重复消息:

Chain 1: Rejecting initial value:
Chain 1:   Log probability evaluates to log(0), i.e. negative infinity.
Chain 1:   Stan can't start sampling from this initial value.

最后的消息:

Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done

我不知道该怎么做,因为我不知道数学……如果你们中的一些人有想法,我很乐意尝试!

标签: stan

解决方案


推荐阅读