我正在使用R进行非线性回归分析,遇到了误差传播的问题.具体地说,我使用predictNLS
来估计我的预测的误差,但在某些情况下,误差范围超过了生物学上合理的阈值(例如,预测间隔高于1或低于0,这在我的生物学背景下是没有意义的).
下面是我代码的一个可复制的例子.它模拟指数关系,拟合非线性最小二乘(NLS)模型,然后使用predictNLS
来估计预测误差.我的挑战是处理错误传播导致生物学上不可能的值的实例.
Reproducible Example:
# simulate exponential relationship
set.seed(123)
# generate random x values between 0 and 60
x <- runif(100, 0, 60)
y <- 1 - exp(-0.075 * x) * rnorm(100, 0.7, 0.1)
data = data.frame(sr= x, fipar = y)
# create a nls model to fit the data
model <- nls(fipar ~ 1 - exp(-a * sr), data = data, start = list(a = 0.001))
# create an observed and predicted dataframe
data$predicted <- predict(model, data)
library(ggplot2)
data %>%
ggplot(aes(x = sr, y = fipar)) +
geom_point() +
geom_line(aes(y = predicted), color = "red")
# estimate the errors using predictNLS
newdat = data.frame(sr = seq(1, 60, 1))
prediction_se <- predictNLS(model, newdata = newdat, interval = "prediction", type = 'response')
prediction_se$summary$sr <- newdat$sr
prediction_se$summary %>%
ggplot(aes(x = sr, y = Prop.Mean.1)) +
ylim(0, 1.2) +
geom_point() +
geom_ribbon(aes(ymin = `Prop.2.5%`, ymax = `Prop.97.5%`), alpha = 0.2) +
geom_hline(yintercept = 1)
我对此的自然react 是简单地将任何错误值1替换为1,但我猜这忽略了一堆统计假设.>是否有任何推荐的方法来限制预测区间或调整误差估计以反映生物学约束?也许我的问题会更好地在统计堆栈交换,但我想我会寻找一个答案在这里太.