Search code examples
rlog-likelihood

Find log-likelihood using the predicted probabilities in r


I built a glm model and used it to predict probabilities for the test data.

model = glm(y ~ x, data=dt, family=binomial(link='logit'))
pred = predict(model, newdata=test.dt, type='response')

How do I find the test log-likelihood for the predicted probabilities?


Solution

  • Something like

    dbinom(dt$y, prob=pred, size=1, log=TRUE)
    

    should give you the log-likelihood (assuming that these are binary/Bernoulli responses). You could also calculate the log-likelihood directly as dt$y*log(pred) + (1-dt$y)*log(1-pred) (I think)

    If you want the combined log-likelihood just sum() the above value ... or logLik(model)