我有以下多项式回归模型:
图片版
LaTeX版本
$ Y_i | \ mu_i,\ sigma ^ 2 \ sim \ text {Normal}(\ mu_i,\ sigma ^ 2),i = 1,\ dots,n \ \ text {independent} $
$ \ mu_i = \ alpha + \ beta_1 x_ {i1} + \ beta_2 x_ {i2} + \ beta_3 x_ {i1} ^ 2 + \ beta_4 x_ {i2} ^ 2 + \ beta_5 x_ {i1} x_ { I2} $
$ \ alpha \ sim \ text {some suitable prior} $
$ \ beta_1,\ dots,\ beta_5 \ sim \ text {some suitable priors} $
$ \ sigma ^ 2 \ sim \ text {some suitable prior} $
我想在$ y_i $,$ x_ {i1} $和$ x_ {i2} $上将样本大小和观察向量作为输入。代码如下:
data{
int<lower=1> n;
vector[n] x1;
vector[n] x2;
vector[n] y;
}
我想标准化(居中和缩放)两个输入变量,以获得标准化的回归变量x1_std
和x2_std
。此代码位于transformed data
块中,如下所示:
transformed data{
real bar_x1;
real x1_sd;
vector[n] x1_std;
real bar_x2;
real x2_sd;
vector[n] x2_std;
real y_sd;
bar_x1 = mean(x1);
x1_sd = sd(x1);
x1_std = (x1 - bar_x1)/x1_sd; // centered and scaled
bar_x2 = mean(x2);
x2_sd = sd(x2);
x2_std = (x2 - bar_x2)/x2_sd; // centered and scaled
y_sd = sd(y);
}
然后我想使用标准化的回归量变量拟合上述多项式回归模型,并在两者上回归参数$ \ alpha $,$ \ beta_1 $和$ \ dots,\ beta_5 $返回估计值原始和标准化的规模。
基于此,如果我没有弄错,从标准化参数到原始尺度的转换公式如下:
图片版
LaTeX版本
$ \ alpha = \ tilde {\ alpha} - \ dfrac {\ gamma_1} {s_1} \ bar {x} _1 - \ dfrac {\ gamma_2} {s_2} \ bar {x} _2 + \ dfrac {\ gamma_3} {s_1 ^ 2} \ bar {x} _1 ^ 2 + \ dfrac {\ gamma_4} {s_2 ^ 2} \ bar {x} _2 ^ 2 + \ dfrac {\ gamma_5} {s_1 s_2} \ bar {x } _1 \酒吧{X} _2 $
$ \ beta_1 = \ left(\ dfrac {\ gamma_1} {s_1} - 2 \ dfrac {\ gamma_3} {s_1 ^ 2} \ bar {x} _1 - \ dfrac {\ gamma_5} {s_1 s_2} \ bar {x} _2 \ right)$
$ \ beta_2 = \ left(\ dfrac {\ gamma_2} {s_2} - 2 \ dfrac {\ gamma_4} {s_2 ^ 2} \ bar {x} _2 - \ dfrac {\ gamma_5} {s_1 s_2} \ bar {x} _1 \ right)$
$ \ beta_3 = \ dfrac {\ gamma_3} {s_1 ^ 2} $
$ \ beta_4 = \ dfrac {\ gamma_4} {s_2 ^ 2} $
$ \ beta_5 = \ dfrac {\ gamma_5} {s_1 s_2} $
实现此目的的代码包含在generated quantities
块中,如下所示:
alpha = alpha_std - beta1_std*bar_x1/x1_sd - beta2_std*bar_x2/x2_sd
+ (beta3_std*bar_x1^2)/x1_sd^2 + (beta4_std*bar_x2^2)/x2_sd^2
+ (beta5_std*bar_x2*bar_x1)/(x1_sd*x2_sd);
beta1 = beta1_std/x1_sd - 2*beta3_std*bar_x1/x1_sd^2
- beta5_std*bar_x2/(x1_sd*x2_sd);
beta2 = beta2_std/x2_sd - 2*beta4_std*bar_x2/x2_sd^2
- beta5_std*bar_x1/(x1_sd*x2_sd);
beta3 = beta3_std/x1_sd^2;
beta4 = beta4_std/x2_sd^2;
beta5 = beta5_std/(x1_sd*x2_sd);
我的整个模型如下:
data{
int<lower=1> n;
vector[n] x1;
vector[n] x2;
vector[n] y;
}
transformed data{
real bar_x1;
real x1_sd;
vector[n] x1_std;
real bar_x2;
real x2_sd;
vector[n] x2_std;
real y_sd;
bar_x1 = mean(x1);
x1_sd = sd(x1);
x1_std = (x1 - bar_x1)/x1_sd; // centered and scaled
bar_x2 = mean(x2);
x2_sd = sd(x2);
x2_std = (x2 - bar_x2)/x2_sd; // centered and scaled
y_sd = sd(y);
}
parameters{
real<lower=0> sigma;
real alpha_std;
real beta1_std;
real beta2_std;
real beta3_std;
real beta4_std;
real beta5_std;
}
transformed parameters {
real mu[n];
for(i in 1:n) {
mu[i] = alpha_std + beta1_std*x1_std[i]
+ beta2_std*x2_std[i] + beta3_std*x1_std[i]^2
+ beta4_std*x2_std[i]^2 + beta5_std*x1_std[i]*x2_std[i];
}
}
model{
alpha_std ~ normal(0, 10);
beta1_std ~ normal(0, 2.5);
beta2_std ~ normal(0, 2.5);
beta3_std ~ normal(0, 2.5);
beta4_std ~ normal(0, 2.5);
beta5_std ~ normal(0, 2.5);
sigma ~ exponential(1 / y_sd);
y ~ normal(mu, sigma);
}
generated quantities {
real alpha;
real beta1;
real beta2;
real beta3;
real beta4;
real beta5;
alpha = alpha_std - beta1_std*bar_x1/x1_sd - beta2_std*bar_x2/x2_sd
+ (beta3_std*bar_x1^2)/x1_sd^2 + (beta4_std*bar_x2^2)/x2_sd^2
+ (beta5_std*bar_x2*bar_x1)/(x1_sd*x2_sd);
beta1 = beta1_std/x1_sd - 2*beta3_std*bar_x1/x1_sd^2
- beta5_std*bar_x2/(x1_sd*x2_sd);
beta2 = beta2_std/x2_sd - 2*beta4_std*bar_x2/x2_sd^2
- beta5_std*bar_x1/(x1_sd*x2_sd);
beta3 = beta3_std/x1_sd^2;
beta4 = beta4_std/x2_sd^2;
beta5 = beta5_std/(x1_sd*x2_sd);
}
我正在使用R hills
包中的MASS
数据集:
library(MASS)
hills[18, 3] <- 18.65 # Fixing transcription error
x1 <- hills$dist
x2 <- hills$climb
y <- hills$time
n <- length(x1)
data.in <- list(x1 = x1, x2 = x2, y = y, n = n)
model.fit <- sampling(example, data.in)
现在我输出标准化的(alpha_std
,beta1_std
,beta2_std
,beta3_std
,beta4_std
,beta5_std
)和原始比例( alpha
,beta1
,beta2
,beta3
,beta4
,beta5
)回归参数:
print(model.fit, pars = c("alpha_std", "alpha", "beta1_std", "beta2_std", "beta3_std", "beta4_std", "beta5_std", "beta1", "beta2", "beta3", "beta4", "beta5", "sigma"), probs = c(0.05, 0.5, 0.95), digits = 5)
如果有人能告诉我是否正确地解决了这个问题,我将不胜感激。我还对数学进行了两次和三次检查,所以我认为它应该是正确的。尽管如此,我担心的一件事是beta4
是0.00000。这是否表明我犯了错误?正如我所说,我已经完成了所有的代码和数学,所以,据我所知,似乎的一切都很好......
答案 0 :(得分:0)
好吧,我刚刚发现问题是我没有打印足够数字的值(5不足),看不到该值不是0.00000。其他一切都很好。