2017-04-22 46 views
1

我想通過散點圖數據繪製邏輯趨勢線,但是,我不知道該如何繼續。我搜索了網絡,發現需要3個參數的函數,但我不知道如何找到這些參數。任何幫助將不勝感激。r通過散點圖擬合邏輯曲線

數據:

x   y 
1 0 36.4161850 
2 0 94.2196532 
3 0 94.7976879 
4 0 98.2658960 
5 0 97.1098266 
6 250 40.4624277 
7 250 41.0404624 
8 250 23.6994220 
9 250 48.5549133 
10 250 61.2716763 
11 500 5.7803468 
12 500 3.4682081 
13 500 0.5780347 
14 500 2.8901734 
15 500 0.0000000 
16 750 0.0000000 
17 750 0.0000000 
18 750 0.0000000 
19 750 0.0000000 
20 750 0.0000000 

dummy <- structure(list(x = c("0", "0", "0", "0", "0", "250", "250", "250", 
"250", "250", "500", "500", "500", "500", "500", "750", "750", 
"750", "750", "750"), y = c(36.4161849710983, 94.2196531791908, 
94.7976878612717, 98.2658959537572, 97.1098265895954, 40.4624277456647, 
41.0404624277457, 23.6994219653179, 48.5549132947977, 61.271676300578, 
5.78034682080925, 3.46820809248555, 0.578034682080925, 2.89017341040462, 
0, 0, 0, 0, 0, 0)), reshapeLong = structure(list(varying = structure(list(
    Proportion = c("m0.perc", "m250.perc", "m500.perc", "m750.perc" 
    )), .Names = "Proportion", v.names = "Proportion", times = c("m0.perc", 
"m250.perc", "m500.perc", "m750.perc")), v.names = "Proportion", 
    idvar = "id", timevar = "Distance"), .Names = c("varying", 
"v.names", "idvar", "timevar")), .Names = c("x", "y"), row.names = c(NA, 
-20L), class = "data.frame") 

什麼我的目標是啓動高,低兩端,鏡像「S」,如果你喜歡,通過散點圖數據的邏輯曲線。

plot(y~x, data = dummy) 

enter image description here

感謝所有幫助

+0

從看https://en.wikipedia.org/wiki/Logistic_function,似乎這將是一個良好的開端。 (函數(L,k,x0,x)L /(1 + exp(-k *(x-x0)))的函數來看看函數的適當參數符號/形狀。繪圖(f(100,-0.01,300,1:600),type =「l」)'(不是非常結構化的方法!)。嘗試擬合'minpack.lm :: nlsLM(y_L /(1 + exp(-k *(x_x0))),start_c(L = 100,k = -0.1,x0 = 300),data =虛擬)'。 [記得做'dummy $ x < - as.numeric(dummy $ x)',因爲它當前是字符] – user2957945

+0

感謝您的回覆。但是,你的答案令我感到困惑。第一部分確實繪製了一個圖表,但它並沒有覆蓋這個散點圖上。你的答案的第二部分適合模型,但不適合任何東西? – FlyingDutch

+0

你寫道你想要擬合曲線,但需要估計參數。上面評論中的非線性模型估計這些。初始函數f和plot用於查找nlsLM函數的啓動參數。要做最後的情節,你可以在模型上使用預測,類似於你做線性迴歸時擬合線的方式。 – user2957945

回答

1

drc(劑量反應曲線)可能會有所幫助。

您可以估算具有3個或4個參數的連續數據的邏輯曲線。該函數自動爲優化算法找到不錯的起始值(與例如nls相反)。它也有簡單的繪圖方法。

下面是一個帶3個參數(參數fct = L.3())的示例。第四個參數是較低的漸近線,固定爲0.使用四參數模型估計較低的漸近線。

> dummy$x <- as.numeric(dummy$x) 
> 
> library("drc") 
> mL <- drm(y ~ x, data = dummy, fct = L.3(), type = "continuous") 
> summary(mL) 

Model fitted: Logistic (ED50 as parameter) with lower limit fixed at 0 (3 parms) 

Parameter estimates: 

       Estimate Std. Error t-value p-value 
b:(Intercept) 0.013938 0.010315 1.351208 0.1943 
d:(Intercept) 86.789553 10.417186 8.331382 0.0000 
e:(Intercept) 248.714704 30.029077 8.282463 0.0000 

Residual standard error: 

14.61229 (17 degrees of freedom) 

> plot(mL, type = "all") 
> 

enter image description here

+0

這看起來像一個簡單的解決方案。但是,爲什麼x軸會發生變化?它可以像原始軸一樣可視化,從0到800米運行? – FlyingDutch

+0

這是因爲在生態毒理學中,x軸是產品的劑量並且通常用對數標度表示。所以x的日誌比例是默認值。使用它在原始尺度上繪製它:'plot(mL,type =「all」,log ='')'並且查看'?plot.drc'以獲得此方法的幫助。 – Gilles