[英]Constrain the Intercept term in H2O GLM
I'm familiar with how to constrain the Betas (regression parameters) in an h2o.glm()
, but struggling to understand how this can be extended to constrain the intercept.我熟悉如何在
h2o.glm()
约束 Betas (回归参数h2o.glm()
,但很难理解如何扩展它以约束截距。
(I do understand that intercept=FALSE
constrains it to zero, but I'm interested in a non-zero constraint.) (我知道
intercept=FALSE
将其约束为零,但我对非零约束感兴趣。)
Notional example dataset:概念示例数据集:
n <- 100
set.seed(1)
getPoints <- function(n){
rbind(
data.frame(col= factor('red', levels=c('red','blue')),
x1 = rnorm(n=n,mean=11,sd = 2),
x2 = rnorm(n=n,mean=5,sd=1)),
data.frame(col='blue',
x1 = rnorm(n=n,mean=13,sd = 2),
x2 = rnorm(n=n,mean=7,sd=1))
)
}
df1 <- getPoints(n)
Example constraints:示例约束:
param_names <- c('Intercept', 'x1', 'x2')
param_vals <- c( 27.5, -1.1, -2.7)
beta_const_df <- data.frame(names = c('Intercept','x1','x2'),
lower_bounds = param_vals-0.1,
upper_bounds = param_vals+0.1,
beta_start = param_vals)
The constraints will work if I omit the "Intercept" constraint:如果我省略“拦截”约束,约束将起作用:
glm1 <- h2o.glm(x=c('x1','x2'),
y='col',
family='binomial',
lambda=0,
alpha=0,
training_frame = 'df1',
beta_constraints=beta_const_df[-1,]
)
glm1@model$coefficients
# Intercept x1 x2
# 27.68408 -1.00000 -2.60000
But if I include an "Intercept" constraint, the other constraints fail too.但是如果我包含一个“拦截”约束,其他约束也会失败。
glm2 <- h2o.glm(x=c('x1','x2'),
y='col',
family='binomial',
lambda=0,
alpha=0,
training_frame = 'df1',
beta_constraints=beta_const_df)
glm2@model$coefficients
# Intercept x1 x2
# 0.67783085 -0.01185921 -0.03083395
What's the proper syntax to constrain the intercept?限制拦截的正确语法是什么?
Try setting the standardize
argument equal to False (shown in the code below), you can read more about the beta_constraints parameter here :尝试将
standardize
参数设置为等于 False(如下面的代码所示),您可以在此处阅读有关 beta_constraints 参数的更多信息:
glm1 <- h2o.glm(x=c('x1','x2'),
y='col',
family='binomial',
lambda=0,
alpha=0,
training_frame = as.h2o(df1),
beta_constraints=beta_const_df,
standardize = F
)
glm1@model$coefficients
> glm1@model$coefficients
#Intercept x1 x2
#27.6 -1.0 -2.6
Workaround if all constraints are strict equality如果所有约束都严格相等,则解决方法
I can inflict severe L2 penalty rho
for deviating from beta_given
, and it seems like Intercept
is supported here:我可以对偏离
beta_given
造成严重的 L2 惩罚rho
,似乎这里支持Intercept
:
beta_const_df <- data.frame(names = c('Intercept','x1','x2'),
#lower_bounds = param_vals-0.1, #don't bound
#upper_bounds = param_vals+0.1,
#beta_start = param_vals, # use beta_given
beta_given = param_vals, # new
rho = 1e9 ) # new
Then this works:然后这有效:
glm2 <- h2o.glm(x=c('x1','x2'),
y='col',
family='binomial',
lambda=0,
alpha=0,
training_frame = 'df1',
beta_constraints=beta_const_df)
glm2@model$coefficients
# Intercept x1 x2
# 27.5 -1.1 -2.7
all.equal(glm2@model$coefficients, param_vals, check.names=FALSE) # TRUE
This only works if you have all equality constraints (not distinct upper and lower bounds).这仅在您具有所有相等约束(不明确的上限和下限)时才有效。
Either way, still wondering if there is a less hacky way to do it.无论哪种方式,仍然想知道是否有更简单的方法来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.