2017-08-31 226 views
3
test <- 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)) 

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 
      method.args = list(start=c(tau=0.2,Vmax=2)), # this too 
      se=FALSE) 

我找到這個網站在這裏的代碼,但我不知道如何在geom_smooth改變method="nls"method = "nlsLM",因爲原來的「NLS」在設置起始值時對我來說真的是一個大問題。 ggplot2中geom_smooth的方法有沒有辦法從cran中使用軟件包? 感謝如何使用method =「nlsLM」(在封裝minpack.lm)在geom_smooth

+1

請有禮貌地在你找到該代碼的地方添加一個鏈接。 – Roland

回答

1

這也可能是最好保持在一個單獨的數據幀的nls結果,並分別繪製了兩個項目:

ggplot() + 
    geom_point(aes(x=t, y=fold), data = test) + 
    geom_line(aes(...), data = my.nls.results) 
+1

所以這意味着很難直接使用geom_smooth來適應nlsLM?感謝您的替代方法 –

2

你似乎沒有嘗試任何事情。你可以簡單地做一下這樣的事情:

library(ggplot2) 
library(minpack.lm) 
d <- ggplot(test,aes(x=t, y=fold))+ 
    geom_point()+ 
    geom_smooth(method="nlsLM", 
       formula=y~1+Vmax*(1-exp(-x/tau)), 
       method.args = list(start=c(tau=0.2,Vmax=2)), 
       se=FALSE) 
print(d) 
#works 

請注意,收斂問題並沒有一個簡單的萬能解決方案。有時候,minpack可以提供幫助,但通常它會給你一個不合適的地方,nls有助於引發錯誤。

+0

實際上,我沒有嘗試過,但我得到了一個警告: 警告消息:'stat_smooth()'中的計算失敗:變量'(jac)'的無效類型(列表)' –

+0

我甚至嘗試過minpack。 lm :: nlsLM,但它不工作,所以我在這裏搜索,但仍然沒有任何東西,然後我順便提出了問題 –

+0

,我使用nls並嘗試了「非線性迴歸與R」方法,並獲得了正確的開始值,感謝您的提醒。我只想知道是否可以使用geom_smooth –