Deviance test for binomial distribution

# Let A et B being observations from binomial distribution:
A <- c(10, 10, 9, 7, 3, 2, 3)
B <- c(2, 5, 6, 6, 10, 15, 20)

# And the prediction of proportion being pred:
pred <- c(0.9, 0.85, 0.75, 0.4, 0.3, 0.2, 0.1)

# For example prod could be the result of a glm, or other model.
# Number of parameters obtained from observations and used to estimate pred
parameter <- 2

# Then the degrees of freedom are:
df <- length(pred)-parameter

LnL <- sum(dbinom(x=A, size = A+B, 
                  prob = pred, log = TRUE))
LnLSat <- sum(dbinom(x=A, size = A+B, 
                     prob = A/(A+B), log = TRUE))
deviance <- -2*(LnL - LnLSat)
p <- 1-pchisq(deviance, df=df)

# The larger is p, the more the observations could have been obtained from the model used to calculate pred
# Do not use p<0.05 as a criteria; p is like a proxy

plot(x=pred, y=A/(A+B), bty="n", las=1, xlim=c(0, 1), ylim=c(0,1))
text(x=0.8, y=0.2, labels = paste("p=", round(p, digits = 2)))



Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04