我是Stan的编程新手,并尝试通过我在网上找到的一些代码:https://modernstatisticalworkflow.blogspot.com/2017/11/bayesian-instrumental-variables-with.html
data {
int N;
int PX; // dimension of exogenous covariates
int PN; // dimension of endogenous covariates
int PZ; // dimension of instruments
matrix[N, PX] X_exog; // exogenous covariates
matrix[N, PN] X_endog; // engogenous covariates
matrix[N, PZ] Z; // instruments
vector[N] Y_outcome; // outcome variable
int<lower=0,upper=1> run_estimation; // simulate (0) or estimate (1)
}
transformed data {
matrix[N, 1 + PN] Y;
Y[,1] = Y_outcome;
Y[,2:] = X_endog;
}
parameters {
vector[PX + PN] gamma1;
matrix[PX + PZ, PN] gamma2;
vector[PN + 1] alpha;
vector<lower = 0>[1 + PN] scale;
cholesky_factor_corr[1 + PN] L_Omega;
}
transformed parameters {
matrix[N, 1 + PN] mu; // the conditional means of the process
mu[:,1] = rep_vector(alpha[1], N) + append_col(X_endog,X_exog)*gamma1;
mu[:,2:] = rep_matrix(alpha[2:]', N) + append_col(X_exog, Z)*gamma2;
}
这是工具变量模型的开始。在“转换后的参数”部分,我不完全确定“:”在行中做了什么:
mu[:,1] = rep_vector(alpha[1], N) + append_col(X_endog,X_exog)*gamma1;
mu[:,2:] = rep_matrix(alpha[2:]', N) + append_col(X_exog, Z)*gamma2;
它是否告诉Stan这应该迭代现有的行/列?
通常,冒号表示连续整数序列(除非它用作三元运算符的一部分)。通常,您会在颜色的两侧看到整数,例如for (n in 1:N) {...}
。但是,Stan用户手册第27.2节描述了使用“单侧”整数序列对数组,向量,矩阵等进行子集化。
也可以仅提供下限,或仅提供上限。写
c[3:]
只是c[3:size(c)]
的简写。写c[:5]
只是c[1:5]
的简写。
此外,Stan用户手册描述了使用“零边”整数序列进行子集化
最后,可以通过仅包含范围符号(
:
)作为索引或将索引位置留空来编写覆盖整个数组范围的范围索引。在这两种情况下,c[]
和c[:]
等于c[1:size(c)]
,而c
恰好等于mu[:,2:] =
。
因此,mu[ , 2:cols(mu)] =
相当于qazxswpoi,并且除了第一列以及赋值运算符右侧的(子)矩阵之外的所有行都填充。