# [R] Plotting Fitted vs Observed Values in Logistic Regression Model

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Aug 2 15:50:17 CEST 2023

```Às 14:57 de 01/08/2023, Paul Bernal escreveu:
> Dear friends,
>
> I hope  this email finds you all well. This is the dataset I am working
> with:
> dput(random_mod12_data2)
> structure(list(Index = c(1L, 5L, 11L, 3L, 2L, 8L, 9L, 4L), x = c(5,
> 13, 25, 9, 7, 19, 21, 11), n = c(500, 500, 500, 500, 500, 500,
> 500, 500), r = c(100, 211, 391, 147, 122, 310, 343, 176), ratio = c(0.2,
> 0.422, 0.782, 0.294, 0.244, 0.62, 0.686, 0.352)), row.names = c(NA,
> -8L), class = "data.frame")
>
> A brief description of the dataset:
> Index: is just a column that shows the ID of each observation (row)
> x: is a column which gives information on the discount rate of the coupon
> n: is the sample or number of observations
> r: is the count of redeemed coupons
> ratio: is just the ratio of redeemed coupons to n (total number of
> observations)
>
> #Fitting a logistic regression model to response variable y for problem 13.4
> logistic_regmod2 <- glm(formula = ratio~x, family = binomial(logit), data =
> random_mod12_data2)
>
> I would like to plot the value of r (in the y-axis) vs x (the different
> discount rates) and then superimpose the logistic regression fitted values
> all in the same plot.
>
> How could I accomplish this?
>
> Any help and/or guidance will be greatly appreciated.
>
> Kind regards,
> Paul
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Here is another way with ggplot2.
It doesn't give you the fitted values but it plots the fitted line.

library(ggplot2)

ggplot(random_mod12_data2, aes(x, ratio)) +
geom_point() +
stat_smooth(
formula = y ~ x,
method = glm,
method.args = list(family = binomial),
se = FALSE
)

Hope this helps,