泊松回归(glm,R)算法的精度
我想做泊松回归,但我需要我的回归函数比glm运行得更快,并且至少有同样的精度。考虑下面的实验:泊松回归(glm,R)算法的精度,r,precision,glm,poisson,R,Precision,Glm,Poisson,我想做泊松回归,但我需要我的回归函数比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 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
接受参数精度
;此参数的值小于1会使optim
超出其默认终止条件以获得更高的精度
不幸的是,方法1和方法2的结果差别太大(出于我的目的);7.358对7.359。给precision
参数一个较小的值,如0.01,使这两种方法达到合理的一致性,使我怀疑R的glm
函数非常精确
因此,我的问题是:
glm
的结果的精度水平由什么决定?也许作为一个子问题,glm
使用什么算法来寻找最大可能性(我已经深入研究了源代码,但对我来说并不容易)。我很难相信“你已经深入研究了代码”,因为有一个“控制”参数和将参数传递给glm的函数:
?glm
# control
# a list of parameters for controlling the fitting process.
# For glm.fit this is passed to glm.control.
是的,我看过
控件
参数文档,但没有发现它有什么帮助。无论如何,我对我的问题感到不满,我想把它删除。