R中的非线性回归预测
nonlinear regression prediction in R
当我尝试使用 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(times)、c(times)等更改数据输入,但仍然无法摆脱警告。有人可以帮我找出问题所在吗?谢谢!!
我 re-ran 您使用提供的示例数据进行分析,我可以重现您的警告。总结如下:
适合 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())
基于模型拟合,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;
可视化数据和预测。
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);
模型拟合似乎合理,我想说可以安全地忽略 warnings
(查看详细信息)。
详情
我查看了 drc
source-code,警告来自 predict.drc.R
的第 201
行:
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 不喜欢添加 one-dimensional 数组和向量,而是更喜欢添加适当的标量和向量,或者向量和向量。
当我尝试使用 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(times)、c(times)等更改数据输入,但仍然无法摆脱警告。有人可以帮我找出问题所在吗?谢谢!!
我 re-ran 您使用提供的示例数据进行分析,我可以重现您的警告。总结如下:
适合
形式的 Michaelis-Menten modelf(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())
基于模型拟合,
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;
可视化数据和预测。
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);
模型拟合似乎合理,我想说可以安全地忽略 warnings
(查看详细信息)。
详情
我查看了 drc
source-code,警告来自 predict.drc.R
的第 201
行:
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 不喜欢添加 one-dimensional 数组和向量,而是更喜欢添加适当的标量和向量,或者向量和向量。