Search code examples
rlogistic-regressionpolynomials

why the difference between plot produced by glm() and polynomial()


library(polynom)
set.seed(12345)
x<-seq(1,5,0.01)
lp<-rnorm(401,-0.7,1)-20*x+7*x^2
link_lp <- exp(lp)/(1 + exp(lp))
y<-(runif(401) < link_lp)
f<-glm(y~poly(x,degree=2),family="binomial")
par(mfrow=c(1,3))
plot(x,f$linear.predictors)
plot(polynomial(coef(f)),xlim=c(1,5))
plot(x,f$fitted)

Figures produced with above codes

enter image description here

the figures produced by linear predictor and polynomial() are supposed to be the same, but actually they are different. what's wrong with my code?


Solution

  • You need to study help("poly") and need to learn what an orthogonal polynomial is.

    f<-glm(y~poly(x,degree=2, raw = TRUE),family="binomial")
    par(mfrow=c(1,3))
    plot(x,f$linear.predictors)
    plot(polynomial(coef(f)),xlim=c(1,5))
    plot(x,f$fitted)
    

    resulting plot