Answers:
由于L1正则化在相关系数上等于先验Laplace(双指数),因此您可以按以下步骤进行操作。在这里,我有三个自变量x1,x2和x3,而y是二进制目标变量。此处通过在其上放置一个超优先级来选择正则化参数,在这种情况下,仅在一个适当大小的范围内保持一致。
model {
# Likelihood
for (i in 1:N) {
y[i] ~ dbern(p[i])
logit(p[i]) <- b0 + b[1]*x1[i] + b[2]*x2[i] + b[3]*x3[i]
}
# Prior on constant term
b0 ~ dnorm(0,0.1)
# L1 regularization == a Laplace (double exponential) prior
for (j in 1:3) {
b[j] ~ ddexp(0, lambda)
}
lambda ~ dunif(0.001,10)
# Alternatively, specify lambda via lambda <- 1 or some such
}
让我们使用dclone
R中的包尝试一下!
library(dclone)
x1 <- rnorm(100)
x2 <- rnorm(100)
x3 <- rnorm(100)
prob <- exp(x1+x2+x3) / (1+exp(x1+x2+x3))
y <- rbinom(100, 1, prob)
data.list <- list(
y = y,
x1 = x1, x2 = x2, x3 = x3,
N = length(y)
)
params = c("b0", "b", "lambda")
temp <- jags.fit(data.list,
params=params,
model="modela.jags",
n.chains=3,
n.adapt=1000,
n.update=1000,
thin=10,
n.iter=10000)
与不规则逻辑回归相比,这里是结果:
> summary(temp)
<< blah, blah, blah >>
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
b[1] 1.21064 0.3279 0.005987 0.005641
b[2] 0.64730 0.3192 0.005827 0.006014
b[3] 1.25340 0.3217 0.005873 0.006357
b0 0.03313 0.2497 0.004558 0.005580
lambda 1.34334 0.7851 0.014333 0.014999
2. Quantiles for each variable: << deleted to save space >>
> summary(glm(y~x1+x2+x3, family="binomial"))
<< blah, blah, blah >>
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.02784 0.25832 0.108 0.9142
x1 1.34955 0.32845 4.109 3.98e-05 ***
x2 0.78031 0.32191 2.424 0.0154 *
x3 1.39065 0.32863 4.232 2.32e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
<< more stuff deleted to save space >>
我们可以看到,这三个b
参数确实缩小为零。
很抱歉,我对Laplace分布的超参数/正则化参数了解不多。我倾向于使用均匀分布并查看后验,看它是否表现合理,例如,没有在端点附近堆积并且在中间出现了严重的峰值,而没有可怕的偏度问题。到目前为止,通常就是这种情况。将其视为方差参数并使用Gelman Prior分布的推荐用于层次模型中的方差参数对我也适用。