配合ggplot2,geom_smooth和nls [英] Fitting with ggplot2, geom_smooth and nls

查看:452
本文介绍了配合ggplot2,geom_smooth和nls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用等式来拟合指数衰减函数(RC类似系统)的数据:



我的数据位于以下数据框中:

 数据集<  -  data.frame( Exp = c(4,4,4,4, 1,2,3,4,5,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,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.75576​​377806756,1.89280913889538,2.00219054189937,1.87795513639311,1.85242493827193,1.7409346372629,1,0.840498729335292,0.9041309050004 99,1.23116185602517,1.41897551928886,1.60167656534099,1.72389226836308,1.80635095956481,1.766040786872057,1.74327897001172,1.63581509884482))

我有3实验(实验: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))$ (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()+
ge om_smooth(method =nls,
formula ='fold-1 + Vmax *(1-exp(-t / tau))',
start = c(tau = 0.2,Fmax = 2) )
print(d)

我得到以下错误:

  model.frame.default中的错误(公式=〜fold,data = data,weights = weight):
可变长度不同'(权重)')
另外:警告信息:
1:在min(x)中:没有非缺少参数min;返回Inf
2:在max(x)中:没有非缺少参数为max;返回-Inf

我的语法有什么问题吗?为了在数据集上使用相同的函数,并使用组在每个Exp级别有一个适合值,我会让这个工作。




  1. / code>是 nls 的参数,您需要传递一个公式对象给它,而不是一个字符。

  2. ggplot2将 y x 传递给 nls 而不是 fold t

  3. 默认情况下, stat_smooth 尝试获取置信区间。这在 predict.nls 中没有实现。

总结: / p>

  d < -  ggplot(test,aes(x = t,y = fold))+ 
#很显然,我使用参数名称而不是位置匹配
geom_point()+
geom_smooth(method =nls,
formula = y〜1 + Vmax *(1-exp(-x / tau)),#这是一个nls参数,
#but stat_smooth沿着
start = c(tau = 0.2,Vmax = 2)传递参数,#this也是
se = FALSE) #这是stat_smooth的参数,
#关闭了绘制置信区间

编辑:



在版本2的主要ggplot2更新之后,您需要:

  geom_smooth(method =nls,
formula = y〜1 + Vmax *(1-exp(-x / tau)),#这是一个nls参数
方法.args = list(start = c(tau = 0.2,Vm ax = 2)),#this too
se = FALSE)


I am trying to fit data on an exponential decay function (RC like system) with equation:

My data are on the following dataframe:

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

I have 3 experiment (Exp: 4, 5 and 6) data I want to fit each experiment on the given equation.

I have managed to do it for of the experiment by subsetting my data and using the parameter calculated by 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()

But if I try to use the geom_smooth function directly on the full dataset with this code

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)

I get the following error:

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

Is there anything wrong with my syntax? I would have this one working in order to use the same function on the dataset and using group to have one fit per Exp level.

解决方案

There are several problems:

  1. formula is a parameter of nls and you need to pass a formula object to it and not a character.
  2. ggplot2 passes y and x to nls and not fold and t.
  3. By default, stat_smooth tries to get the confidence interval. That isn't implemented in predict.nls.

In summary:

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

Edit:

After the major ggplot2 update to version 2, you need:

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)

这篇关于配合ggplot2,geom_smooth和nls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆