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

问题描述 投票:0回答:1

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

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

sum(test$count * log(fitted(hush)) - fitted(hush) - log(factorial(test$count)))
# [1] -200.9226
logLik(hush)
# 'log Lik.' -200.9226 (df=3)
© www.soinside.com 2019 - 2024. All rights reserved.