Count数据中的零,如何处理?

时间:2018-03-05 18:40:16

标签: r regression nan poisson

我有一个包含计数数据的数据集。我用glm进行泊松回归。 现在我想手动计算零偏差。为此,我需要完整模型的loglike。对于loglike我得到NaN。我认为这是因为响应变量的某些值为0而log(0)产生NaN。但是glm会计算null偏差。所以必须有一个技巧来处理y中的0个条目。我应该用非常小的值替换它们,如0,00001或者什么可能是一个可能的解决方案来获得不是NaN的lf

data(discoveries)
disc <- data.frame(count=as.numeric(discoveries),
                   year=seq(0,(length(discoveries)-1),1))

yearSqr <- disc$year^2

hush <- glm(count ~ year + yearSqr , family = "poisson", disc)


# modelFrame
test <- hush$model
# reponse variable 
test$count

# formula for loglike full modell lf = sum(y * log(y) - y - log(factorial(y)))


# result is NaN
lf <- sum(test$count * log(test$count) - test$count - log(factorial(test$count)))

1 个答案:

答案 0 :(得分:0)

你申请的公式错了;它不使用任何有关估计参数的信息。您想使用以下内容:

sum(test$count * log(fitted(hush)) - fitted(hush) - log(factorial(test$count)))
# [1] -200.9226
logLik(hush)
# 'log Lik.' -200.9226 (df=3)
相关问题