问题描述
使用插入符号包在R中执行逻辑回归,并尝试强制执行零拦截,以使x = 0处的概率为0.5.在其他形式的回归中,似乎可以使用tunegrid关闭截距,但是它没有逻辑回归的功能.有什么想法吗?
Performing logistic regression in R using the caret package and trying to force a zero intercept such that probability at x=0 is .5. In other forms of regression, it seems like you can turn the intercept off using tunegrid, but that has no functionality for logistic regression. Any ideas?
model <- train(y ~ 0+ x, data = data, method = "glm", family = binomial(link="probit"),
trControl = train.control)
是的,我知道"在x = 0处的概率应为0.5,因此试图强加它.
And yes, I "know" that the probability at x=0 should be .5, and thus trying to force it.
推荐答案
有一个渐近有关如何为插入符号设置自定义模型的信息.因此,在下面的解决方案中,您还可以看到拦截仍然存在的原因:
There's a vignette on how to set up a custom model for caret. So in the solution below, you can also see why the intercept persist:
library(caret)
glm_wo_intercept = getModelInfo("glm",regex=FALSE)[[1]]
如果您看合适的话,那一行会显示:
if you look at the fit, there's a line that does:
glm_wo_intercept$fit
....
modelArgs <- c(list(formula = as.formula(".outcome ~ ."), data = dat), theDots)
...
因此,默认情况下存在拦截.您可以更改此行并在修改后的模型上运行插入符号:
So the intercept is there by default. You can change this line and run caret on this modified model:
glm_wo_intercept$fit = function(x, y, wts, param, lev, last, classProbs, ...) {
dat <- if(is.data.frame(x)) x else as.data.frame(x)
dat$.outcome <- y
if(length(levels(y)) > 2) stop("glm models can only use 2-class outcomes")
theDots <- list(...)
if(!any(names(theDots) == "family"))
{
theDots$family <- if(is.factor(y)) binomial() else gaussian()
}
if(!is.null(wts)) theDots$weights <- wts
# change the model here
modelArgs <- c(list(formula = as.formula(".outcome ~ 0+."), data = dat), theDots)
out <- do.call("glm", modelArgs)
out$call <- NULL
out
}
我们拟合了模型:
data = data.frame(y=factor(runif(100)>0.5),x=rnorm(100))
model <- train(y ~ 0+ x, data = data, method = glm_wo_intercept,
family = binomial(),trControl = trainControl(method = "cv",number=3))
predict(model,data.frame(x=0),type="prob")
FALSE TRUE
1 0.5 0.5
这篇关于插入符号中的逻辑回归-是否没有截获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!