我想进行泊松回归,但我需要我的回归函数比glm
运行得更快,并且至少具有更高的精度。考虑以下实验:
## Here is some "data":
da = data.frame(matrix(c(0,1,212,1,0,200,1,1,27), nrow = 3, byrow = TRUE))
names(da) = c("c1", "c2", "c")
## I want to do a Poisson regression of c on c1 and c2 and an intercept.
## Here is my function that uses optim for Poisson regression with the data da to find the intercept term:
zglm2 = function(precision = 1){ #predictors = best.terms, data = ddat, normalized = normalized
# The design matrix
M = as.matrix(cbind(rep(1, nrow(da)), da[,1:2]))
# Initialize beta, the coefficients
beta = rep(0, 3)
# State the log-likelihood (up to a constant) for the data da and parameter beta:
neg.pois.log.like.prop = function(beta){
log.lambda = M%*%beta # log-expected cell counts under poisson model
return(-sum(-exp(log.lambda) + da$c*log.lambda))}
# State the gradient of the log-likelihood:
grad.fun = function(beta){a = exp(M%*%beta)-da$c; return(t(a)%*%M)}
# Estimate the MLE
beta = optim(beta, neg.pois.log.like.prop, method = "BFGS", gr = grad.fun, control = list(reltol = precision*sqrt(.Machine$double.eps)))$par
return(beta[1])}
## Here are two ways of estimating the intercept term:
# Method 1
zglm2(precision = 1)
# Method 2
as.numeric(glm(c ~ 1+c1+c2, data = da, family = poisson)$coefficients[1])
我的函数zglm2
使用R的optim
例程来找到泊松回归问题的最大似然解(对于这种特殊情况)。 zglm2
接受论证precision
;小于1的此参数的值使optim
超出其默认终止条件,以实现更高的精度。
不幸的是,方法1和方法2的结果太不相同(出于我的目的); 7.358对7.359。为precision
参数赋予较小的值(如0.01)会使两种方法达成一致,导致我怀疑R的glm
函数非常精确。
所以这是我的问题:什么决定glm
结果的精确度?也许作为一个子问题,glm
使用什么算法来找出可能性的最大值(我已经挖掘了源代码,但这对我来说并不容易)。
答案 0 :(得分:0)
我很难相信“你已深入挖掘代码”,因为有一个“控制”参数和一个将参数传递给glm
的函数:
?glm
# control
# a list of parameters for controlling the fitting process.
# For glm.fit this is passed to glm.control.