Python 如何使用brms(R包)生成我需要的Stan代码,以在pystan中重现模型估计?
我已经开发了使用R包brms估算模型的管道,现在我需要将其转换为python。 我知道在python中最接近brms的是pystan,在pystan中,我必须使用Stan语法编写模型。 我想知道是否有生成Stan代码的brms函数,该代码可以用作python中pystan.StanModel函数的model_代码参数。 我尝试过使用make_stancode函数生成的代码,但没有成功 这是make_stancode生成的代码:Python 如何使用brms(R包)生成我需要的Stan代码,以在pystan中重现模型估计?,python,r,bayesian,stan,pystan,Python,R,Bayesian,Stan,Pystan,我已经开发了使用R包brms估算模型的管道,现在我需要将其转换为python。 我知道在python中最接近brms的是pystan,在pystan中,我必须使用Stan语法编写模型。 我想知道是否有生成Stan代码的brms函数,该代码可以用作python中pystan.StanModel函数的model_代码参数。 我尝试过使用make_stancode函数生成的代码,但没有成功 这是make_stancode生成的代码: life_span_code = """ // generated
life_span_code = """
// generated with brms 2.10.0
functions {
/* compute monotonic effects
* Args:
* scale: a simplex parameter
* i: index to sum over the simplex
* Returns:
* a scalar between 0 and 1
*/
real mo(vector scale, int i) {
if (i == 0) {
return 0;
} else {
return rows(scale) * sum(scale[1:i]);
}
}
}
data {
int<lower=1> N; // number of observations
vector[N] Y; // response variable
int<lower=1> Ksp; // number of special effects terms
int<lower=1> Imo; // number of monotonic variables
int<lower=2> Jmo[Imo]; // length of simplexes
// monotonic variables
int Xmo_1[N];
// prior concentration of monotonic simplexes
vector[Jmo[1]] con_simo_1;
int prior_only; // should the likelihood be ignored?
}
transformed data {
}
parameters {
// temporary intercept for centered predictors
real Intercept;
// special effects coefficients
vector[Ksp] bsp;
// simplexes of monotonic effects
simplex[Jmo[1]] simo_1;
real<lower=0> sigma; // residual SD
}
transformed parameters {
}
model {
// initialize linear predictor term
vector[N] mu = Intercept + rep_vector(0, N);
for (n in 1:N) {
// add more terms to the linear predictor
mu[n] += (bsp[1]) * mo(simo_1, Xmo_1[n]);
}
// priors including all constants
target += student_t_lpdf(Intercept | 3, 65, 12);
target += dirichlet_lpdf(simo_1 | con_simo_1);
target += student_t_lpdf(sigma | 3, 0, 12)
- 1 * student_t_lccdf(0 | 3, 0, 12);
// likelihood including all constants
if (!prior_only) {
target += normal_lpdf(Y | mu, sigma);
}
}
generated quantities {
// actual population-level intercept
real b_Intercept = Intercept;
}
"""
这就是我收到的错误:
“RuntimeError:异常:在上下文中声明和找到的数字维度不匹配;处理阶段=数据初始化;变量名=Jmo;声明的dims=(1);找到的dims=()(在第24行的“未知文件名”中)”
感谢所有的帮助事实证明,问题不在于brms生成的模型代码,而在于我定义参数的方式。 特别是,Jmo必须是列表而不是int
N = data_df.shape[0]
Y = data_df['ls'].tolist()
K = 1
X = [1]*N
Ksp = 1
Imo = 1
Xmo_1 = data_df['income_factor'].tolist()
## The following two lines have changed
Jmo = [len(data_df['income_factor'].unique().tolist())-1]
con_simo_1 = [1, 1, 1]
## End of changes
prior_only = 0
代码的其余部分是相同的。
我仍然希望澄清为什么有些参数可以声明为int,而其他参数只能声明为list
再次感谢事实证明问题不在于brms生成的模型代码,而在于我定义参数的方式。 特别是,Jmo必须是列表而不是int
N = data_df.shape[0]
Y = data_df['ls'].tolist()
K = 1
X = [1]*N
Ksp = 1
Imo = 1
Xmo_1 = data_df['income_factor'].tolist()
## The following two lines have changed
Jmo = [len(data_df['income_factor'].unique().tolist())-1]
con_simo_1 = [1, 1, 1]
## End of changes
prior_only = 0
代码的其余部分是相同的。
我仍然希望澄清为什么有些参数可以声明为int,而其他参数只能声明为list
再次感谢“我尝试过使用make_______________________________________________。您需要更具体地说明哪些不起作用,可能需要一个最小的可复制示例。也许您还需要使用
make_standata
?您能更具体地说明哪些功能不起作用吗?“我尝试过使用make_stancode函数生成的代码,但它不起作用。”因为make_stancode
应该是这样做的方法,这并没有多大帮助。您需要更具体地说明哪些不起作用,可能需要一个最小的可复制示例。也许您还需要使用make_standata
?您能否更具体地说明哪些不起作用?