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

标签 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。

最佳答案

我相信当您使用响应的自然对数变换拟合模型时,您只需要允许分组变量 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

关于r - 如何使用 ggplot2 和线性逼近拟合和绘制指数衰减函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453861/

相关文章:

python - 一像素宽曲线的曲率

r - smooth.Pspline stat_smooth 包装器(在 ggplot2 中)

python - 即使我迭代地改变初始猜测,scipy curve_fit 也不会收敛

function - 在 gnuplot 中拟合分段函数

用 dplyr 无条件替换数据框中的值

r - 一个图中的两个 ggplots,靠得很近

r - 在 ggplot2 中添加多级 x 标签

css - 调整 Shiny 进度条的大小并将其居中

r - Dplyr 多个管道动态变量?

c++ - R CMD SHLIB 'undefined reference' 并且不为 SWIG 生成的 R 包装器生成 .so/.dll