Search code examples
rpredictionnon-linear-regressiondrc

nonlinear regression prediction in R


I am confused by this warning message as I try to fit my data with a nonlinear regression model by using the drc package and drm function.

I have

 N_obs <- c(1, 80, 80, 80, 81, 82, 83, 84, 84, 95, 102, 102, 102, 103, 104, 105, 105, 109, 111, 117, 120, 123, 123, 124, 126, 127, 128, 128, 129, 130)

 times <- c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)

The model is

  model.drm <- drm(N_obs ~ times, data = data.frame(N_obs = N_obs, times = times), fct = MM.2())

and the warnings come from predictions

  preds <- predict(model.drm, times = times, interval = "confidence", level = 0.95)

There were 30 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In (tquan * sqrt(varVal + sumObjRV)) * c(-1, 1) :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.
2: In (tquan * sqrt(varVal + sumObjRV)) * c(-1, 1) :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.

3: In (tquan * sqrt(varVal + sumObjRV)) * c(-1, 1) :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.

I have been trying to change data inputs by using as.vector(times), c(times),etc., but still cannot get rid of the warnings. Could someone help me identify the problem? Thank you!!


Solution

  • I re-ran your analysis with the sample data provided, and I can reproduce your warnings. Here's a summary:

    1. Fit a Michaelis-Menten model of the form f(x; d, e) = d * (1 + e/x)^-1.

      # Fit a 2 parameter Michaelis-Menten model
      library(drc);
      fit <- drm(
          formula = N_obs ~ times,
          data = data.frame(N_obs = N_obs, times = times),
          fct = MM.2())
      
    2. Based on the model fit, predict the response for the original times. Note you can omit the newdata argument here, because in that case predict will simply use the fitted values (which are based on times).

      # Predictions
      pred <- as.data.frame(predict(
          fit,
          newdata = data.frame(N_obs = N_obs, times = times),
          interval = "confidence", level = 0.95));
      pred$times <- times;
      
    3. Visualise data and predictions.

      library(tidyverse);
      data.frame(times = times, N_obs = N_obs) %>%
          ggplot(aes(times, N_obs)) +
              geom_point() +
              geom_line(data = pred, aes(x = times, y = Prediction)) +
              geom_ribbon(
                  data = pred,
                  aes(x = times, ymin = Lower, ymax = Upper),
                  alpha = 0.4);
      

    enter image description here

    The model fit seems reasonable, and I would say that the warnings can be safely ignored (see details).

    Details

    I had a look at the drc source-code, and the warning originates from line 201 of predict.drc.R:

    retMat[rowIndex, 3:4] <- retMat[rowIndex, 1] + (tquan * sqrt(varVal + sumObjRV)) * c(-1, 1)
    

    In that line, an array of dimension 1 is added to a numeric vector.

    Here is a simple example to reproduce the warning:

    arr <- array(5, dim = 1);
    arr + c(1, 2);
    #[1] 6 7
    #Warning message:
    #In arr + c(1, 2) :
    #  Recycling array of length 1 in array-vector arithmetic is deprecated.
    #  Use c() or as.vector() instead. 
    

    Note that the result is still correct; it's just that R doesn't like the addition of a one-dimensional array and a vector, and prefers instead adding proper scalars and vectors, or vectors and vectors.