Adjusted line of the binomial regression model made in ggplot2

Asked

Viewed 84 times

0

I am trying to adjust the line of the binomial model in the graph of observations, however, one observes a problem in the code.

The data is below: https://drive.google.com/file/d/19jOpLfwJ2zYyTF3PP_1eILl8nww6kxB1/view?usp=sharing

See that to carry out the graph without using the ggplot2 function the codes are these:

plot(c(0,4.5), c(0.3,0.75), type="n") 
points(Treat1.dat$Dose,pe, pch=".", cex=3)
points(d,meanD, col='blue', pch="*")
lines(x, predict(modQL1.1, data.frame(Dose = x), 
                     type = "response"),col= "blue")

To play in ggplot2, I performed the following computational routine:

library(ggplot2)
Treat1.dat <- subset(dataSt, Treat==1);Treat1.dat
Treat1.dat$Dose = as.factor(Treat1.dat$Dose);str(Treat1.dat)
resp1<-cbind(Treat1.dat$RazaoSexual,Treat1.dat$TotRazaoSexual-Treat1.dat$RazaoSexual)
modQL1.1<-glm(resp1~Dose, family=quasibinomial, Treat1.dat) 

d <- c(0.5,1.0,2.0,3.0,4.0)
meanD <- round(fitted(modQL1.1)[1:5],4); meanD
(pe <- Treat1.dat$RazaoSexual/Treat1.dat$TotRazaoSexual)
x<-seq(0.3,4,0.1)

ggplot(Treat1.dat, aes(x = c(0,4.5), y = c(0.3,0.75))) + 
   geom_point(aes(Dose,pe),size=3, pch = ".") +
   geom_point(aes(d,meanD),size=3, pch = "*", col="blue") +
   coord_cartesian(ylim=c(0.3,0.75), xlim=c(0,4.5)) +
    geom_abline(x, predict(modQL1.1, data.frame(Dose = x), 
                          type = "response"),col= "blue") +
   theme(legend.position = "none",axis.title = element_text(size = 22,color="black"),
                                               axis.text = element_text(size = 22,color="black"),
                                               strip.text.x = element_text(size = 22,color="black"),
                                               legend.title = element_text(size = 22),
                                               legend.text = element_text(size = 22))

Erro: Aesthetics must be either length 1 or the same as the data (20): x and y
Run `rlang::last_error()` to see where the error occurred.

1 answer

1


You are providing two points as xy coordinates; you must specify an equal amount of points to be plotted. But providing independent coordinates of the dataset is a bad practice; instead, let the plot or ggplot determine axes by data, adjusting display limits if required.

library(ggplot2)

dataSt <- read.csv2("https://docs.google.com/uc?id=19jOpLfwJ2zYyTF3PP_1eILl8nww6kxB1&export=download")

Treat1 <- subset(dataSt, Treat == 1)
Treat1$de <- with(Treat1, RazaoSexual/TotRazaoSexual)

modQl1 <- glm(de ~ Dose, quasibinomial, Treat1)
# Converter uma variável quantitativa para fator para rodar seu modelo não é uma boa ideia

# Com plot
plot(de ~ Dose, Treat1, pch = ".", xlim = c(0, 4.5), ylim = c(.3, .75))
points(modQl1$model$Dose, fitted(modQl1), pch = "*", col = "blue")
lines(Treat1$Dose, predict(modQl1, Treat1["Dose"], type = "response"), col= "blue")

# Com ggplot
ggplot(Treat1, aes(Dose, de)) +
  geom_point() +
  geom_point(aes(y = fitted(modQl1)), colour = "blue") +
  geom_line(aes(y = predict(modQl1, Treat1["Dose"], type = "response")), colour = "blue") +
  coord_cartesian(c(0, 4.5), c(.3, .75))

inserir a descrição da imagem aqui

OBS: I used names of different objects if you want to compare to what I was doing.

  • Thank you Carlos Eduardo Lobster!!!

  • @Breno-g, stop using comments to request personal help. Understand what the OS is and how it works. Write a good question and any user with time and willingness will answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.