0
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ The default link function for the binomial family is logit. It looks like you want log. Try binomial("log") as the family. $\endgroup$ Commented Nov 6, 2020 at 22:06
  • $\begingroup$ 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?" $\endgroup$
    – Ben Bolker
    Commented Nov 6, 2020 at 22:37
  • $\begingroup$ @BenBolker I want to compute the log-likelihood model manually without using logLik. thanks $\endgroup$ Commented Nov 6, 2020 at 22:40

1 Answer 1

1
$\begingroup$

Both your log-likelihood equation and your R code have some errors:

$$ \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$).

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Nov 6, 2020 at 23:35
  • $\begingroup$ 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 $\endgroup$ Commented Nov 8, 2020 at 18:12
  • $\begingroup$ yes, if you specify type="link" (the default) $\endgroup$
    – Ben Bolker
    Commented Nov 8, 2020 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.