2

I have a binary outcome fit by a logistic model with a continuous independent variable that is logged to ensure a linear rather than exponential relationship to the dependent variable. I have included the second order term as it appears the fit is improved overall. The resulting formula is:

fit <- glm(y ~ poly(log(x),2), family=binomial(link=logit))

How do I calculate the probability of each event with these higher order terms given a first order term coefficient of A and second order term coefficient of B? Is it

sum = A x log(x) + B x log(x)^2 --> probability = e^sum / (1+e^sum)?

Haitao Du
  • 32,885
  • 17
  • 118
  • 213
Frank H.
  • 566
  • 1
  • 4
  • 16

1 Answers1

1

Using R, you can directly use the function predict.

If we want to do it manually, we need to get the data matrix $X$, and calucate $X\beta$ then apply function $\text{logit}^{-1}(X\beta)$. $X$ is obtained by appending $1$ (intercept term) to poly(x,2).

Note that, We need to use poly(x,2), instead of calculating cbind(x,x^2) manually, because the model is using orthogonal polynomials not raw polynomials.

set.seed(0)
x=runif(10)
y=sample(0:1,10,replace = T)
fit=glm(y~poly(x,2), family="binomial")

# using R predict function
predict(fit,data.frame(x=x),type="response")

# calculate manually
m=poly(x,2)
link=cbind(1,m) %*% fit$coefficients
as.vector(plogis(link))
Haitao Du
  • 32,885
  • 17
  • 118
  • 213
  • 1
    Yes, but how can I do it manually? – Frank H. Jun 07 '17 at 16:54
  • I think my confusion lies in your comment about the model using orthogonal, not raw, polynomials. Can you provide some reading for me to better understand this difference? – Frank H. Jun 07 '17 at 17:01
  • 1
    you can start to check my answer [here](https://stats.stackexchange.com/questions/233414/why-are-there-large-coefficents-for-higher-order-polynomial/234196#234196) and a related question [here](https://stats.stackexchange.com/questions/104085/is-there-ever-a-reason-not-to-use-orthogonal-polynomials-when-fitting-regression) – Haitao Du Jun 07 '17 at 17:02
  • How do I know if an orthogonal polynomial produces a better fit than a raw polynomial? Compare AUROC as usual, or some other way? – Frank H. Jun 07 '17 at 17:06
  • Also, if you don't mind, please answer the related post I made here: https://stats.stackexchange.com/questions/283888/is-this-a-good-example-of-adding-a-second-order-term-but-not-the-first-order-ter – Frank H. Jun 07 '17 at 17:07
  • 2
    @FrankH. `orthogonal polynomial` and `raw` produce **exact same fit**. But orthogonal polynomial have many advantages, such as numerical stability. – Haitao Du Jun 07 '17 at 17:09