在R中拟合双指数曲线

时间:2018-03-15 11:22:03

标签: r exponential

我有一组看起来像这样的数据:

Time    Al Biochar  Al FeO  Cu Biochar  Cu FeO
 0      0.218223461 0.218223461      12.39823125     12.39823125
0.5    0.1395087   0.041177135      0.00543732      3.759749493
1      0.08415793  -0.134641447    -12.38861634    -4.177991174
1.5   -0.005332069 -0.316522561    -24.78366292    -12.52075324
2     -0.060324192 -0.500248756    -37.17868817    -20.59065175
2.5    -0.087457366 -0.635370352    -49.57529656    -28.45255875
3      -0.128805357 -0.800601678    -61.9718953 -37.06576867
4   -0.189998798    -0.900340101    -74.36721169    -45.37149157
5   -0.264429401    -1.015069379    -86.76336658    -56.68657815
6.5 -0.303092011    -1.111173198    -99.15624929    -67.18844927

我想将以下双指数衰减方程拟合到我的行

  

y = a * exp(-bx)+ c * exp(-dx)

我对绘图等很好,只是想知道如何拟合指数衰减以给出最佳拟合线并给出该拟合的r2值以及a,b的值, c和d。

此外,我如何在之后绘制适合的情节?

我想知道如何在R中做到这一点。

我有以下代码无法正常使用

mydata <- read.csv("B5cumul.csv")
fitData <- data.frame(mydata$Time, mydata$Al.Biochar)
plot(mydata$Time, mydata$Al.Biochar, type="b", 
     xlab="Time (hour)", ylab="Al removal (mg/L)")
# a is plateau. b is the amplitude of fast phase, r1 is the fast constant. 
# (y[1]-a-b) is the amplitude of slow phase, r2 is the slow constant.
f = function (a, r1, r2, b) {
  a + (b * exp(-(r1 * mydata$Time))) + ((mydata$Al.Biochar -a-b) * exp(-(r2 * x)))
}
  fit <- nls(f, data=fitData, start=list(a=0.25, r1=0.05, r2=1e-5, b=0.22), 
       algorithm="port")

我不确定a,b c和d使用哪些值。我收到了错误 错误:类型为&#39;关闭的对象&#39;不是子集表。

1 个答案:

答案 0 :(得分:0)

你确定双指数函数有意义吗?因为那不是数据所支持的。

df <- read.table(
    text = "Time    'Al Biochar'  'Al FeO'  'Cu Biochar'  'Cu FeO'
 0      0.218223461 0.218223461      12.39823125     12.39823125
0.5    0.1395087   0.041177135      0.00543732      3.759749493
1      0.08415793  -0.134641447    -12.38861634    -4.177991174
1.5   -0.005332069 -0.316522561    -24.78366292    -12.52075324
2     -0.060324192 -0.500248756    -37.17868817    -20.59065175
2.5    -0.087457366 -0.635370352    -49.57529656    -28.45255875
3      -0.128805357 -0.800601678    -61.9718953 -37.06576867
4   -0.189998798    -0.900340101    -74.36721169    -45.37149157
5   -0.264429401    -1.015069379    -86.76336658    -56.68657815
6.5 -0.303092011    -1.111173198    -99.15624929    -67.18844927", header = T)


df %>%
    gather(what, value, 2:5) %>%
    mutate(unit = ifelse(grepl("Cu", what), "ug/L", "mg/L")) %>%
    ggplot(aes(Time, value, colour = what)) + 
        geom_point() + 
        facet_wrap(~ unit, scales = "free")

enter image description here

也相关:Double exponential fit in R