# Computing Log-likelihood Model Manually for Logit Model

A log-likelihood function takes on the form of: logL$$=\Sigma^{n}_{i=1}(y_ilog(\frac{e^{\beta' x_i}}{1+e^{\beta'x_i}} )+(1-y_i)(\frac{e^{\beta' x_i}}{1+e^{\beta'x_i}})$$

My logit model is estimated as follows:

glm.logit=glm(model,binomial(),data)


Estimating $$y_i$$

yi=data\$y


x's as a matrix of the dependent variable

 xi=cbind(data$$x1+data$$x2+...)


Taking the estimates of $$\beta$$ from my model

betai=coef(glm.logit)


Putting the these together:

xibetai<-xi%*%t(betai)


Estimating the logistic form:

logiti<-exp(xibetai)/(1+exp(xibetai))


Putting everything together in the form of a log-likelihood model:

LogLi<-yi%*%log(logiti)+(1-yi)%*%log(1-logiti)


Issue with the fact that you can't take take the log of a negative value, so I only get about half of my coefficients in the output.

• The default link function for the binomial family is logit. It looks like you want log. Try binomial("log") as the family. Nov 6, 2020 at 22:06
• what exactly do you want to do? Extract the predicted probabilities for each observation? Extract the log-likelihood for the model? Compute the log-likelihood for the model manually? (see ?predict, ?logLik ...) This might be more appropriate for StackOverflow, as it looks more like "how do I compute ... ?" rather than "what should I compute?" or "what does this mean?" Nov 6, 2020 at 22:37
• @BenBolker I want to compute the log-likelihood model manually without using logLik. thanks Nov 6, 2020 at 22:40

$$\begin{split} \eta_i & = \mathbf{X}_i \boldsymbol{\beta} \quad \textrm{(so far so good)} \\ p_i & = \exp(\eta_i)/(1+\exp(\eta_i)) = 1/(1+\exp(-\eta_i)) \\ L_i & = y_i \log p_i + (1-y) \log(1-p_i) \\ L & = \sum L_i \end{split}$$

In R you can use plogis() for the logistic ($$1/(1+\exp(-\eta_i))$$):

p <- plogis(X %*% beta)
sum(y*log(p) + (1-y)*log(1-p))


(or dbinom(y, prob=p, size=1, log=TRUE))

If your coefficients include an intercept, $$\mathbf{X}$$ should include a column of 1s at the corresponding position (usually the first column). You can check various intermediate results with predict(fitted_model, type="link") ($$\eta$$), predict(fitted_model, type="response") ($$p$$).

• Thanks for your response, I had a couple of errors in my log-likelihood function, I have corrected these, my output and your output both give the same answer, however, this answer is incorrect. I suspect that the way I have multiplied x and beta may be wrong as beta includes the intercept. Nov 6, 2020 at 23:35
• Thanks again, I'm slightly confused over how to use predict(), does it give me as output, my x variables multiplied by my betas? Thanks Nov 8, 2020 at 18:12
• yes, if you specify type="link" (the default) Nov 8, 2020 at 18:21