3

我试图在只有几个时间点的数据上拟合指数衰减函数。我想使用指数衰减方程 y = y0*e^(-r*time)来比较r数据集和因子之间的(或最终的半衰期)。我已经明白,如果我想估计置信区间(我会这样做),使用线性拟合而不是 nls 是这个特定函数 [ 1 , 2 ] 的更好选择。

复制它以获取一些示例数据:

x <- structure(list(Factor = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 
1L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 4L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L), .Label = c("A", "B", "C", "D"), class = "factor"), 
time = c(0.25, 0.26, 0.26, 0.26, 0.27, 0.29, 0.29, 0.33, 
0.38, 0.38, 0.38, 0.39, 0.4, 0.4, 0.41, 0.45, 0.45, 0.45, 
0.45, 0.47, 0.51, 0.51, 0.52, 0.57, 0.57, 0.57, 0.57, 0.58, 
    0.58, 0.58, 0.6, 0.6, 0.6, 0.61, 0.61, 0.61, 0.62, 0.62, 
    0.64, 0.64, 0.67, 0.67, 0.67, 0.67, 0.69, 0.7, 0.7, 0.71, 
    0.76, 0.76, 0.77, 0.77, 0.79, 0.79, 0.8, 0.8, 0.83, 0.83, 
    0.84, 0.84, 0.86, 0.86, 0.87, 0.87, 18.57, 18.57, 18.57, 
    18.58, 18.69, 18.69, 18.7, 18.7, 18.7, 18.71, 18.71, 18.71, 
    18.74, 18.74, 18.74, 18.79, 18.85, 18.85, 18.86, 18.88, 18.89, 
    18.89, 18.89, 18.93, 18.93, 18.95, 18.95, 18.95, 18.96, 18.96, 
    18.96, 20.57, 20.57, 20.61, 20.62, 20.66, 20.67, 20.67, 20.67, 
    20.72, 20.72, 20.72, 21.18, 21.19, 21.19, 21.19, 21.22, 21.22, 
    21.22, 21.23, 21.25, 21.25, 21.25, 21.25, 87.58, 87.58, 87.64, 
    87.64, 87.65, 87.84, 87.85, 87.91, 87.91, 87.91, 89.27, 89.28, 
    89.28, 89.36, 89.36, 89.4, 89.4, 110.91, 112.19, 112.19, 
    112.2, 112.2, 112.24, 112.25, 112.25, 112.26, 185.6, 185.6, 
    185.63, 185.63, 185.64, 213, 234.96, 234.97, 234.97, 234.98, 
    235.01, 235.01, 235.02, 235.02), y = c(58.1, 42.9, 54.2, 
    45.3, 51.2, 44.4, 56.9, 53.4, 61.3, 49.3, 54.4, 55.6, 25.6, 
    48.1, 50.8, 54.7, 41.8, 46.2, 39.5, 51.7, 37.7, 43.1, 44.6, 
    48.4, 50.9, 62.5, 58.6, 47.8, 44.3, 55.6, 44.9, 49.1, 49.1, 
    60.3, 40.8, 57.6, 42.9, 60, 49.4, 54.1, 37.8, 46.5, 59, 64.3, 
    48, 54.3, 51.7, 59, 57.1, 29.4, 49.2, 50, 41.3, 40.5, 43.4, 
    48.6, 38.5, 35.7, 43.6, 60, 32, 27.3, 34.3, 44.4, 36.5, 25.4, 
    22.6, 25.5, 24.1, 18.9, 25, 5.9, 19.6, 15.7, 32.3, 14.3, 
    23.4, 29.4, 17, 18.3, 34.4, 26.4, 35.7, 22.6, 23.5, 19.3, 
    25.5, 34.7, 45.5, 38.1, 33.8, 47.9, 32.3, 32.1, 43, 27.8, 
    33.3, 25.5, 22.2, 29.2, 24.2, 22.8, 19.2, 31.6, 20.8, 26.4, 
    35.8, 50, 10.7, 24, 54.3, 67, 77.7, 51.7, 64.8, 49.3, 57.8, 
    43.2, 17, 17.4, 36.4, 60.2, 36, 4, 0, 0, 9.1, 2.9, 24.3, 
    18.8, 36, 16.3, 18.4, 17.1, 26.5, 29.3, 17.4, 23.1, 25.7, 
    32.7, 16.3, 14.6, 13.7, 16.2, 16.7, 21.9, 0, 0, 11.6, 8.6, 
    0, 3.7, 3.6, 5, 3.2, 0, 2.5, 5.7)), .Names = c("Factor", 
"time", "y"), row.names = c(NA, -158L), class = "data.frame")

我设法使用标准对数函数来做到这一点log(y) = x(感谢这个例子),但是当试图在线性空间中拟合几个参数时失败。

summary(lm(log(y) ~ time, data = x, subset = Factor)) # I need the summary statistics to compare models
ggplot(x, aes(x = time, y = y, color = Factor)) + geom_point() + geom_smooth(method = "glm", family = gaussian(lin="log"), start=c(5,0))

在此处输入图像描述

这是我尝试过的:

## Summary

log.dec.fun <- function(N, r, time) -r*time + log(N) # The function in linear format

summary(glm(y ~ log.dec.fun(N, r, time), data = x, subset = Factor, start = c(5,0)))
# Error in log.dec.fun(N, r, time) : object 'r' not found

predict(glm(y ~ log.dec.fun(N, r, time), data = x, start = c(5,0)))
# Error in log.dec.fun(N, r, time) : object 'r' not found

## Plot

ggplot(x, aes(x = time, y = y, color = Factor)) + geom_point() + geom_smooth(method = "glm", formula = y ~ log.dec.fun(N, r, time), start = c(5,0))
#Error in log.dec.fun(N, r, time) : object 'r' not found
#Error in if (nrow(layer_data) == 0) return() : argument is of length zero

我可以使用 得到相当满意的模型nls,但我了解到计算nls函数的置信区间几乎是魔法,初学者甚至不应该尝试这样做。

dec.fun <- function(N, r, time) N*exp(-r*time) ## The function in non-linear form
g <- c()
for(i in 1:nlevels(x$Factor)){
z <- subset(x, Factor == levels(x$Factor)[i])
g <- append(g, predict(nls(y ~ dec.fun(N, r, time), data = z, start = list(N = 5, r = 0))))}
x <- x[with(x, order(Factor, time)),]

x$modelled <- g

ggplot(x, aes(x = time, color = Factor)) + geom_point(aes(y = y)) + geom_line(aes(y = modelled))

在此处输入图像描述 所以我的问题是如何使用 R、ggplot2 和线性逼近来拟合指数衰减函数?SO 中有一个答案,@Joe Kington 表示这是可能的,并提供了 Python 代码。不幸的是,我不懂 Python。

4

1 回答 1

7

Factor我相信当您使用响应的自然对数变换拟合模型时,您只需要允许单独的斜率和截距由您的分组变量拟合。我称之为单独的线模型。然后,您可以预测并获得对数范围内每个 的置信度(或预测)区间Factor,并进行反向变换以查看线条(很像您的原始帖子中的图表来自ggplot2.

R中的单独线模型示例:

fit1 = lm(y ~ time*Factor, data = x)
summary(fit1)

该模型的输出将显示参考水平的估计截距、参考水平Factor的估计斜率以及参考水平与所有其他水平之间的截距和斜率的差异。

或者,您可以编写单独的行模型:

fit2 = lm(y ~ time + time:Factor - 1, data = x)
summary(fit2)

这将分别显示输出中每个级别的估计截距和斜率Factor

要根据模型制作线条,您可以使用predict然后反变换到原始比例。假设自然对数转换(并将值添加到原始数据集):

(x$pred = exp(predict(fit1)) )

如果您需要的话,您还可以计算置信区间并将其取幂为原始比例。

exp(predict(fit1, interval = "confidence"))

在组织上,您可能还希望将这些作为列放在原始数据集中,您可以通过多种方式进行操作。最简单的可能是将cbind它们简单地添加到数据集x

于 2013-11-19T15:54:38.630 回答