使用NLS回归指数衰减数据

发布于 2025-02-09 07:11:08 字数 1326 浏览 1 评论 0原文

我从该模型中获得了衰减参数的负估计。特别是较低的置信区间。我怀疑我正在错误地计算CI或错误指定该模型:

library(nlstools)
## example data
df <- data.frame(week = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
                          1L, 1L, 1L, 1L, 1L, 1L),
                 count = c(1.3e+09, 9e+08, 4e+09, 1.7e+09, 1.2e+09,
                           3e+09, 1.58e+09, 1.6e+09, 3e+09, 1e+09,
                           1100000, 2e+06, 1700000, 4e+06, 1400000,
                           4e+06))

## taking log to get starting values for nls
model_lm <- lm(log(count)~week, data = df)

## extract the coefficients and to convert values back to non-log
intercpt <- exp(model_lm$coefficients[1])
coeff<- model_lm$coefficients[2]

## Run the nls model for exponential decay
model <- nls(count ~ b0 * exp(-b1 * week),
             start = list(b0 = intercpt,
                          b1 = coeff),
             trace=T,
             data = df)

## Point estimate of decay parameter
b1 <- coef(model)[2]

## 95% CI for the decay parameter
low <- confint2(model)[2,1]
high <- confint2(model)[2,2]

## What is the half life ?
log(2)/b1
log(2)/high
log(2)/low

## The result of half life doesn't make sense. The CI for half life
## includes 0. You cannot have a negative half life. The CI also does
## not include the point estimate.

I am getting negative estimates of the decay parameter from this model. Specifically the lower confidence interval. I suspect I am calculating the CI incorrectly or mis-specifying the model:

library(nlstools)
## example data
df <- data.frame(week = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
                          1L, 1L, 1L, 1L, 1L, 1L),
                 count = c(1.3e+09, 9e+08, 4e+09, 1.7e+09, 1.2e+09,
                           3e+09, 1.58e+09, 1.6e+09, 3e+09, 1e+09,
                           1100000, 2e+06, 1700000, 4e+06, 1400000,
                           4e+06))

## taking log to get starting values for nls
model_lm <- lm(log(count)~week, data = df)

## extract the coefficients and to convert values back to non-log
intercpt <- exp(model_lm$coefficients[1])
coeff<- model_lm$coefficients[2]

## Run the nls model for exponential decay
model <- nls(count ~ b0 * exp(-b1 * week),
             start = list(b0 = intercpt,
                          b1 = coeff),
             trace=T,
             data = df)

## Point estimate of decay parameter
b1 <- coef(model)[2]

## 95% CI for the decay parameter
low <- confint2(model)[2,1]
high <- confint2(model)[2,2]

## What is the half life ?
log(2)/b1
log(2)/high
log(2)/low

## The result of half life doesn't make sense. The CI for half life
## includes 0. You cannot have a negative half life. The CI also does
## not include the point estimate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2025-02-16 07:11:08

您可以通过删除双方的日志以获得相同的系数,但要使用更现实的置信区间来重新指定模型:

library(nlstools)

## Run the nls model for exponential decay
model <- nls(log(count) ~ log(b0) - b1 * week,
             start = list(b0 = intercpt,
                          b1 = coeff),
             trace=T,
             data = df)
#> 1083.602    (1.70e+01): par = (1712732584 -6.707821)
#> 3.725045    (1.41e-07): par = (1712732584 6.707821)

## Point estimate of decay parameter
b1 <- coef(model)[2]

## 95% CI for the decay parameter
low <- confint2(model)[2,1]
high <- confint2(model)[2,2]

log(2)/b1
#>        b1 
#> 0.1033342
log(2)/high
#> [1] 0.09522392
log(2)/low
#> [1] 0.1129546

我不确定您为什么在初始模型中遇到问题,但我怀疑该模型中的巨大残留物非努力转换数据是原因。

无论哪种情况,我都不确定通过lm在此处使用nls获得什么。

You can re-specify the model by taking the log of both sides to get the same coefficients but with more realistic confidence intervals:

library(nlstools)

## Run the nls model for exponential decay
model <- nls(log(count) ~ log(b0) - b1 * week,
             start = list(b0 = intercpt,
                          b1 = coeff),
             trace=T,
             data = df)
#> 1083.602    (1.70e+01): par = (1712732584 -6.707821)
#> 3.725045    (1.41e-07): par = (1712732584 6.707821)

## Point estimate of decay parameter
b1 <- coef(model)[2]

## 95% CI for the decay parameter
low <- confint2(model)[2,1]
high <- confint2(model)[2,2]

log(2)/b1
#>        b1 
#> 0.1033342
log(2)/high
#> [1] 0.09522392
log(2)/low
#> [1] 0.1129546

I'm not entirely sure why you get the problem with your initial model, but I suspect the huge residuals in the non-log transformed data are the cause.

In either case, I'm not sure what you gain by using nls over lm here.

Created on 2022-06-21 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文