0

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!!!

OJH
  • 49
  • 7
  • no this is an error in my question, now corrected. – OJH Jul 06 '21 at 17:53
  • 1
    Some ideas for troubleshooting: Does using the `dr4pl()` function directly on the second dataset work correctly? Like, everything works and the model output looks reasonable, etc? If that works, what happens when your use your `predict()` function on that fitted object? Does it return predictions? My thinking here based on the warning is that something may be failing "behind the scenes" for `geom_smooth()` and so making sure the model fits and you get predictions outside of **ggplot2** is a reasonable thing to check for. – aosmith Jul 06 '21 at 18:17
  • OKay, so Data 2 curve 2 is causing the problem. When I filter out this data and run it through the dr4pl function it gives the Error in if (nrow(theta.mat) == 0) { : argument is of length zero error – OJH Jul 06 '21 at 19:40
  • I still have no idea how I avoid this error. – OJH Jul 06 '21 at 19:47
  • 1
    You may be able to use options in `dr4pl()` to get a model to fit. I don't know anything about this method, but saw `method.init = "logistic"` used in the [vignette](https://cran.r-project.org/web/packages/dr4pl/vignettes/walk_through_in_R.html). I arbitrarily tried this with data 2-curve 2 and the model fit successfully. You can add this or other arguments to your fitting function in your smooth layer via the `method.args` argument: `geom_smooth(method = "dr4pl", se = FALSE, method.args = list(method.init = "logistic"))` – aosmith Jul 06 '21 at 21:02

0 Answers0