如何使用ggplot2和线性近似拟合和绘制指数衰减函数

时间:2013-10-18 15:53:21

标签: r ggplot2 curve-fitting

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

复制此内容以获取一些示例数据:

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(感谢this example),但尝试在线性空间中拟合多个参数时失败。

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))

enter image description here

以下是我的尝试:

## 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))

enter image description here 所以我的问题是如何使用R,ggplot2和线性逼近来拟合指数衰减函数?有一个answer in SO,其中@Joe Kington表明这是可能的并提供Python代码。不幸的是我不懂Python。

1 个答案:

答案 0 :(得分:6)

我认为,当您使用响应的自然对数变换拟合模型时,您只需要允许分组变量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到数据集{{1}}。