Paul Bernal
2023-Aug-01 13:57 UTC
[R] Plotting Fitted vs Observed Values in Logistic Regression Model
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]]
Ben Bolker
2023-Aug-01 14:06 UTC
[R] Plotting Fitted vs Observed Values in Logistic Regression Model
logistic_regmod2 <- glm(formula = ratio~x, family = binomial(logit), data random_mod12_data2, weights =n) plot(ratio ~ x, data = random_mod12_data2) pframe <- data.frame(x = sort(random_mod12_data2$x)) pframe$ratio <- predict(logistic_regmod2, newdata = pframe, type = "response") with(pframe, lines(x, ratio)) On 2023-08-01 9:57 a.m., Paul Bernal wrote:> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Dr. Benjamin Bolker Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering (Acting) Graduate chair, Mathematics & Statistics > E-mail is sent at my convenience; I don't expect replies outside of working hours.
Rui Barradas
2023-Aug-02 13:50 UTC
[R] Plotting Fitted vs Observed Values in Logistic Regression Model
?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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > 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, Rui Barradas