0

Eu estou tentando ajustar a reta do modelo binomial no gráfico de observações, no entanto, observa-se um problema no código.

Vejam que para realizar o gráfico sem utilizar a função ggplot2 os códigos são estes:

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")

Para reproduzir em ggplot2, realizei a seguinte rotina computacional:

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.

0

1 Resposta 1

1

Você está fornecendo dois pontos como coordenadas xy; deve especificar uma quantidade igual à de pontos a serem plotados. Mas fornecer coordenadas independentes do conjunto de dados é uma prática ruim; ao invés disso, deixe a plot ou ggplot determinar os eixos pelos dados, ajustando os limites de exibição se precisar.

library(ggplot2)

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: usei nomes de objetos diferentes se quiser comparar com o que estava fazendo.

1
  • Obrigado Carlos Eduardo Lagosta!!!
    – user55546
    15/02/2021 às 18:40

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .