在 Stan 调整线性回归的问题
Problems adjusting Linear Regression at Stan
我在 stan 上调整线性回归模型时遇到问题。观察报错信息时,注意到转换后参数块部分的标识。
见下面stan中的代码结构。
Packages:
library(rstan)
library(bayesplot)
数据:
head(Orange)
cols <- c(colnames(Orange[-1]))
Orange <- Orange[,cols]
str(Orange)
stan 中的代码:
看到 stan 中的块结构遵循推荐的模式,但是我无法确定代码的哪一部分对我来说可能是错误的。
y = Orange$circumference
x = Orange$age
n = length(y)
regresstan = '
data{
int n;
real y[n];
real x[n];
}
parameters{
real alpha;
real beta;
real sigma;
}
transformed parameters{
real mu[n];
mu = alpha + beta*x;
}
model{
//Priors
alpha ~ normal(0, 100);
beta ~ normal(0, 100);
sigma ~ uniform(0, 100);
//Likelihood
y ~ normal(mu, sigma);
}
'
错误:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:
real * real[ ]
Available argument signatures for operator*:
real * real
vector * real
row_vector * real
matrix * real
row_vector * vector
vector * row_vector
matrix * vector
row_vector * matrix
matrix * matrix
real * vector
real * row_vector
real * matrix
No matches for:
real + ill-formed
Available argument signatures for operator+:
int + int
real + real
vector + vector
row_vector + row_vector
matrix + matrix
vector + real
row_vector + real
matrix + real
real + vector
real + row_vector
real + matrix
+int
+real
+vector
+row_vector
+matrix
Expression is ill formed.
error in 'modele28054257a16_a9d23411185fa271b60f20be43062e80' at line 16, column 23
-------------------------------------------------
14: transformed parameters{
15: real mu[n];
16: mu = alpha + beta*x;
^
17: }
-------------------------------------------------
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'a9d23411185fa271b60f20be43062e80' due to the above error.
错误来自行
处的转换参数块
mu = alpha + beta*x;
错误是说你不能用实数向量乘以实数(real * real[ ]
的错误)。您可以通过遍历 mu
的值来解决这个问题
transformed parameters {
real mu[n];
for(i in 1:n) {
mu[i] = alpha + beta * x[i];
}
}
这解决了问题,因为现在您有了一个实标量乘以一个实标量。
我在 stan 上调整线性回归模型时遇到问题。观察报错信息时,注意到转换后参数块部分的标识。
见下面stan中的代码结构。
Packages:
library(rstan)
library(bayesplot)
数据:
head(Orange)
cols <- c(colnames(Orange[-1]))
Orange <- Orange[,cols]
str(Orange)
stan 中的代码:
看到 stan 中的块结构遵循推荐的模式,但是我无法确定代码的哪一部分对我来说可能是错误的。
y = Orange$circumference
x = Orange$age
n = length(y)
regresstan = '
data{
int n;
real y[n];
real x[n];
}
parameters{
real alpha;
real beta;
real sigma;
}
transformed parameters{
real mu[n];
mu = alpha + beta*x;
}
model{
//Priors
alpha ~ normal(0, 100);
beta ~ normal(0, 100);
sigma ~ uniform(0, 100);
//Likelihood
y ~ normal(mu, sigma);
}
'
错误:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:
real * real[ ]
Available argument signatures for operator*:
real * real
vector * real
row_vector * real
matrix * real
row_vector * vector
vector * row_vector
matrix * vector
row_vector * matrix
matrix * matrix
real * vector
real * row_vector
real * matrix
No matches for:
real + ill-formed
Available argument signatures for operator+:
int + int
real + real
vector + vector
row_vector + row_vector
matrix + matrix
vector + real
row_vector + real
matrix + real
real + vector
real + row_vector
real + matrix
+int
+real
+vector
+row_vector
+matrix
Expression is ill formed.
error in 'modele28054257a16_a9d23411185fa271b60f20be43062e80' at line 16, column 23
-------------------------------------------------
14: transformed parameters{
15: real mu[n];
16: mu = alpha + beta*x;
^
17: }
-------------------------------------------------
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'a9d23411185fa271b60f20be43062e80' due to the above error.
错误来自行
处的转换参数块mu = alpha + beta*x;
错误是说你不能用实数向量乘以实数(real * real[ ]
的错误)。您可以通过遍历 mu
transformed parameters {
real mu[n];
for(i in 1:n) {
mu[i] = alpha + beta * x[i];
}
}
这解决了问题,因为现在您有了一个实标量乘以一个实标量。