在R中拟合双指数曲线

时间:2022-07-03 21:24:00

I have a set of data which looks like this:

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

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

And I'd like to fit the following double exponential decay equation to my line

而且我想将以下双指数衰减方程拟合到我的线上

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

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

I'm fine with the graphing etc, just would like to know how to fit the exponential decay to give a line of best fit and give the r2 value for that fit as well as the values for a,b,c and d.

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

Also how do I plot that fit afterwards?

另外我如何在之后绘制合适的图案?

I'd like to know how to do this in R preferably.

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

I have the following code which isn't working correctly

我有以下代码无法正常工作

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

And I'm not sure which values to use for a, b c and d. I get the error Error: object of type 'closure' is not subsettable.

而且我不确定a,b c和d使用哪个值。我得到错误错误:'closure'类型的对象不是子集。

1 个解决方案

#1


0  

Are you sure a double exponential function makes sense? Because that's not what the data supports.

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

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

在R中拟合双指数曲线

Also relevant: Double exponential fit in R

相关:R中的双指数拟合

#1


0  

Are you sure a double exponential function makes sense? Because that's not what the data supports.

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

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

在R中拟合双指数曲线

Also relevant: Double exponential fit in R

相关:R中的双指数拟合