-1

我想做泊松回归,但我需要我的回归函数运行得更快,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来找到可能性的最大值(我已经深入研究了源代码,但这对我来说并不容易)。

4

1 回答 1

0

我很难相信“你已经深入研究了代码”,因为有一个“控制”参数和一个将 argumetn 传递给的函数glm

?glm
# control   
#        a list of parameters for controlling the fitting process. 
#        For glm.fit this is passed to glm.control.
于 2013-09-10T04:01:52.420 回答