2

我尝试在RStan中建立一个可变截距模型,试图根据年龄、儿童数量以及他们是否住在城市地区来预测印度某个地区的女性避孕药具使用情况,截距因地区而异。

下面是我的代码:

stan_list <- list(N = nrow(train), contra = (train$contraceptive_use),
                  district = train$district,
                  urban = train$urban,
                  children = train$living.children,
                  age = train$age_mean)
str(stan_list)

code <- '
data {                          
int<lower=0> N;                              // number of observations
int<lower=1, upper=60> district[N];          // number of districts (J = 60)
vector[N] contra;                            // dependent variable
vector[N] urban;                             // independent variable 1
vector[N] children;                          // independent variable 2
vector[N] age;                               // independent variable 3
}

parameters {
vector[60] beta_0;                       // intercept estimated with 60 districts   
real mu_beta_0;                          // mean for intercepts
real sigma_beta_0;                       // standard error for the intercept
real beta_1;
real beta_2;                              
real beta_3;
}

transformed parameters {
vector[N] contra_hat;                        // create one variable with predictions for each observation
for (i in 1:N) {                              // loop for all cases
     contra_hat[i] <- beta_0[district[i]] + beta_1 * urban[i] + beta_2 * children[i] + beta_3 * age[i];
}
}

model {
mu_beta_0 ~ normal(0, 100);
sigma_beta_0 ~ exponential(0.1);
beta_0 ~ normal(mu_beta_0, sigma_beta_0);
beta_1 ~ normal(4, sigma_beta_0);
beta_2 ~ normal(-3, sigma_beta_0);
beta_3 ~ normal(-2, sigma_beta_0);

// Likelihood
contra ~ bernoulli_logit(contra_hat);
}
'

# Translate the model into C++
model1 <- stan(model_code = code, data = stan_list, iter = 2000, chains = 4)

当我定义上面的模型时,我收到以下错误消息:

Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
No matches for: 

  vector ~ bernoulli_logit(vector)

Available argument signatures for bernoulli_logit:

  int ~ bernoulli_logit(real)
  int ~ bernoulli_logit(real[])
  int ~ bernoulli_logit(vector)
  int ~ bernoulli_logit(row vector)
  int[] ~ bernoulli_logit(real)
  int[] ~ bernoulli_logit(real[])
  int[] ~ bernoulli_logit(vector)
  int[] ~ bernoulli_logit(row vector)

require real scalar return type for probability function.
  error in 'model323f123010a7_b7f3052df681e80851694dd14c79969c' at line 36, column 38
  -------------------------------------------------
    34: 
    35: // Likelihood
    36: contra ~ bernoulli_logit(contra_hat);
                                             ^
    37: }
  -------------------------------------------------

stanc 中的错误(文件 = 文件,模型代码 = 模型代码,模型名称 = 模型名称,:

由于上述错误,无法解析 Stan 模型 'b7f3052df681e80851694dd14c79969c'。

有人可以帮忙吗?

4

1 回答 1

2

结果必须在数据块中声明为整数数组才能供 使用bernoulli_logit,如

int<lower=0,upper=1> contra[N];
于 2018-03-03T23:23:27.047 回答