这里是源代码pscl:::pR2.glm
:
function (object, ...)
{
llh <- logLik(object)
objectNull <- update(object, ~1)
llhNull <- logLik(objectNull)
n <- dim(object$model)[1]
pR2Work(llh, llhNull, n)
}
<environment: namespace:pscl>
如果在公式中指定了偏移量,它会在第二行中丢失(update
用于计算仅截距模型)。
看这个例子:
library("foreign")
ceb <- read.dta("http://data.princeton.edu/wws509/datasets/ceb.dta")
ceb$y <- round(ceb$mean*ceb$n, 0)
ceb$os <- log(ceb$n)
m0 <- glm(y ~ res + offset(os), data=ceb, family=poisson)
m1 <- glm(y ~ res, offset=os, data=ceb, family=poisson)
all.equal(coef(m0), coef(m1))
# [1] TRUE
### compute null models
coef(update(m0, ~1)) # wrong, offset not considered
# (Intercept)
# 5.02
coef(update(m1, ~1))
# (Intercept)
# 1.376
coef(update(m0, ~1, offset=os))
# (Intercept)
# 1.376