R中的非线性回归预测

时间:2018-03-23 07:31:14

标签: r prediction non-linear-regression drc

我对此警告消息感到困惑,因为我尝试使用drc软件包和drm函数将数据与非线性回归模型拟合。

我有

 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)

模型是

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

并且警告来自预测

  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.

我一直试图通过使用as.vector(次),c(次)等来改变数据输入,但仍然无法摆脱警告。有人可以帮我找出问题吗?谢谢!!

1 个答案:

答案 0 :(得分:3)

我使用提供的示例数据重新运行您的分析,我可以重现您的警告。以下是摘要:

  1. 填写f(x; d, e) = d * (1 + e/x)^-1表单的Michaelis-Menten model

    # 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. 根据模型拟合,predict原始times的响应。请注意,您可以在此处省略newdata参数,因为在这种情况下predict将只使用拟合值(基于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. 可视化数据和预测。

    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);
    
  4. enter image description here

    模型拟合似乎是合理的,我会说可以安全地忽略warnings(见详情)。

    详细

    我查看了drc源代码,警告来自201的行predict.drc.R

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

    在该行中,将维度1的数组添加到数字向量中。

    以下是重现警告的简单示例:

    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. 
    

    请注意,结果仍然正确;只是R不喜欢添加一维数组和向量,而是更喜欢添加适当的标量和向量,或向量和向量。