Why is my data generating 'Argument is of length zero' in 'dr4pl' dose-response curves?

166 Views Asked by At

I am plotting 4 parameter logistic curves. It's a huge dataset with lots of curves, and one of my outputs is failing because the model fit doesn't work for some of the data. Some of the data fails to produce a curve and gives a

Warning message: Computation failed in 'stat_smooth()': argument is of length zero

This is the code for the curve model

library(dr4pl)
library(car)
predict.dr4pl <- function (object, newdata=NULL, se.fit=FALSE, level, interval) {
      xseq <- if (is.null(newdata)) object$data$Dose else newdata$x
      pred <- MeanResponse(xseq, object$parameters)
      if (!se.fit) {
        return(pred)
      }
      qq <- qnorm((1+level)/2)
      se <- sapply(xseq,
                   function(x) car::deltaMethod(object, 
                                                "UpperLimit + (LowerLimit - UpperLimit)/(1 + (x/IC50)^Slope)")[["Estimate"]])
      return(list(fit=data.frame(fit=pred,lwr=pred-qq*se,
                                 upr=pred+qq*se), se.fit=se))
    }

Dataset 1 works fine(even though the curves are terrible):

Data1<-structure(list(medPOC = c(1.05706789111184, 1.1384060347655, 
                             1.10675631354542, 1.09544112823877, 1.09872089209577, 1.08773424190801, 
                             1.11870837850395, 1.18073408703732, 1.04746786433328, 1.10523462908471, 
                             1.15984893939929, 1.05297709384816, 1.15628419157872, 1.12589559877175, 
                             1.11894257579501), Curve = c("Curve1", "Curve1", "Curve1", "Curve1", 
                                                              "Curve1", "Curve2", "Curve2", "Curve2", "Curve2", "Curve2", "Curve3", 
                                                              "Curve3", "Curve3", "Curve3", "Curve3"), dose = c(0.1, 0.3, 1, 
                                                                                                                3, 10, 0.1, 0.3, 1, 3, 10, 0.1, 0.3, 1, 3, 10)), row.names = c(NA, 
                                                                                                                                                                               -15L), class = c("tbl_df", "tbl", "data.frame"))

yet Dataset 2 generates the error and no curves are fitted:

Data2<-structure(list(medPOC = c(1.04874856862424, 1.19188614428268, 
                      1.27809586127924, 1.2592834941927, 1.14690004907574, 1.14877338877339, 
                      1.02852390852391, 1.00515592515593, 1.15293139293139, 0.864033264033264, 
                      1.08484115891663, 1.11691157027915, 1.16582933554943, 1.03384580515997, 
                      0.684622067767159), Curve = c("Curve1", "Curve1", "Curve1", "Curve1", 
                                                        "Curve1", "Curve2", "Curve2", "Curve2", "Curve2", "Curve2", "Curve3", 
                                                        "Curve3", "Curve3", "Curve3", "Curve3"), dose = c(0.1, 0.3, 1, 
                                                                                                          3, 10, 0.1, 0.3, 1, 3, 10, 0.1, 0.3, 1, 3, 10)), row.names = c(NA, 
                                                                                                                                                                         -15L), class = c("tbl_df", "tbl", "data.frame"))

Plotting code:

library(ggplot)
ggplot(Data1, aes(dose,medPOC, col=Curve))+ 
      geom_point(size=4, shape=1) +
      geom_smooth(method="dr4pl",se=F)+ 
      coord_trans(x="log10")+
      theme_bw()+
      scale_x_continuous(breaks = c(0.01, 0.1, 1, 10, 100))+
      theme(plot.title = element_text(lineheight = 0.9, face="bold", size=20, hjust=0.5))+
      ggtitle("Dose Response")+
      theme(axis.title = element_text(face="bold", size = 14))+
      theme(axis.text = element_text(face="bold", size = 12, colour="black"))+
      theme(panel.spacing = unit(0.8, "lines"))+
      scale_color_manual(values = c("Curve1" = "grey28",
                                    "Curve2" = "yellow2",
                                    "Curve3"="tomato2"))

Any help would be most appreciated!!!

0

There are 0 best solutions below