Page 427 - Applied Statistics with R
P. 427

17.2. BINARY RESPONSE                                             427


                      Again, we could re-write this to better match the function we’re using to simu-
                      late the data:








                                             ∣ X = x ∼ Bern(   )
                                                    i
                                                               
                                               i
                                             
                                                                  1
                                                      =   (x ) =  1 +    −  (x i )
                                                      
                                                          i
                                                  (x ) = 1 + −4     
                                                   i



                      In this model, as    increases, the log odds decrease.




                      set.seed(1)
                      example_data = sim_logistic_data(sample_size = 50, beta_0 = 1, beta_1 = -4)







                      We again simulate some observations form this model, then fit logistic regression.




                      fit_glm = glm(y ~ x, data = example_data, family = binomial)




                      plot(y ~ x, data = example_data,
                            pch = 20, ylab = "Estimated Probability",
                            main = "Logistic Regression, Decreasing Probability")
                      grid()
                      curve(predict(fit_glm, data.frame(x), type = "response"),
                             add = TRUE, col = "dodgerblue", lty = 2)
                      curve(boot::inv.logit(1 - 4 * x), add = TRUE, col = "darkorange", lty = 1)
                      legend("bottomleft", c("True Probability", "Estimated Probability", "Data"), lty = c(1, 2, 0),
                              pch = c(NA, NA, 20), lwd = 2, col = c("darkorange", "dodgerblue", "black"))
   422   423   424   425   426   427   428   429   430   431   432