【问题标题】:Fitting with ggplot2, geom_smooth and nls适合 ggplot2、geom_smooth 和 nls
【发布时间】:2014-09-21 17:32:18
【问题描述】:

我正在尝试用方程拟合指数衰减函数(类似 RC 的系统)的数据:

我的数据在以下数据框中:

dataset <- data.frame(Exp = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6), t = c(0, 0.33, 0.67, 1, 1.33, 1.67, 2, 4, 6, 8, 10, 0, 33, 0.67, 1, 1.33, 1.67, 2, 4, 6, 8, 10, 0, 0.33, 0.67, 1, 1.33, 1.67, 2, 4, 6, 8, 10), fold = c(1, 0.957066345654286, 1.24139015724819, 1.62889151698633, 1.72008539595879, 1.82725412314402, 1.93164365299958, 1.9722929538061, 2.15842019312484, 1.9200507796933, 1.95804730344453, 1, 0.836176542548747, 1.07077717914707, 1.45471712491441, 1.61069357875771, 1.75576377806756, 1.89280913889538, 2.00219054189937, 1.87795513639311, 1.85242493827193, 1.7409346372629, 1, 0.840498729335292, 0.904130905000499, 1.23116185602517, 1.41897551928886, 1.60167656534099, 1.72389226836308, 1.80635095956481, 1.76640786872057, 1.74327897001172, 1.63581509884482))

我有 3 个实验(Exp:4、5 和 6)数据,我想将每个实验拟合到给定的方程。

我已经设法通过对我的数据进行子集化并使用 nls 计算的参数来完成实验

test <- subset(dataset,Exp==4)
fit1 = nls(fold ~ 1+(Vmax*(1-exp(-t/tau))),
  data=test,
  start=c(tau=0.2,Vmax=2))
ggplot(test,aes(t,fold))+
  stat_function(fun=function(t){1+coef(fit1)[[2]]*(1-exp(-t/coef(fit1)[[1]]))})+
  geom_point()

但如果我尝试使用此代码直接在完整数据集上使用 geom_smooth 函数

d <- ggplot(test,aes(t,fold))+
   geom_point()+
   geom_smooth(method="nls", 
     formula='fold~1+Vmax*(1-exp(-t/tau))',
     start=c(tau=0.2,Fmax=2))
print(d)

我收到以下错误:

Error in model.frame.default(formula = ~fold, data = data, weights = weight) : 
  variable lengths differ (found for '(weights)')
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

我的语法有什么问题吗?我会让这个工作,以便在dataset 上使用相同的功能,并使用组来适应每个 Exp 级别。

【问题讨论】:

    标签: r ggplot2 curve-fitting


    【解决方案1】:

    有几个问题:

    1. formulanls 的参数,您需要将公式对象而不是字符传递给它。
    2. ggplot2 将 yx 传递给 nls 而不是 foldt
    3. 默认情况下,stat_smooth 会尝试获取置信区间。这在 predict.nls 中没有实现。

    总结:

    d <- ggplot(test,aes(x=t, y=fold))+ 
             #to make it obvious I use argument names instead of positional matching
      geom_point()+
      geom_smooth(method="nls", 
                  formula=y~1+Vmax*(1-exp(-x/tau)), # this is an nls argument, 
                                                    #but stat_smooth passes the parameter along
                  start=c(tau=0.2,Vmax=2), # this too
                  se=FALSE) # this is an argument to stat_smooth and 
                            # switches off drawing confidence intervals
    

    编辑:

    ggplot2 主要更新到版本 2 后,您需要:

    geom_smooth(method="nls", 
                  formula=y~1+Vmax*(1-exp(-x/tau)), # this is an nls argument
                  method.args = list(start=c(tau=0.2,Vmax=2)), # this too
                  se=FALSE)
    

    【讨论】:

    • start=c(tau=0.2,Vmax=2),不起作用。根据geom_smooth 文档,您可以将 start=c(tau=0.2,Vmax=2)` 传递给method.args,但它也不起作用。 ggplot 2.0.0
    • 看来se=FALSE 是必不可少的。请参阅:stackoverflow.com/questions/13840658/…
    • @msakya 这适用于method.args = list(start=c(tau=0.2,Vmax=2))
    • 只是为了分享:我的 x 轴范围从 -3 到 +1(log10 刻度); y 轴范围从 -100 到 0(百分比刻度)。设置 method.args = list(start=c(tau=-1,Vmax=2)) 非常适合数据。
    猜你喜欢
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2017-07-29
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多