2016-12-28 49 views
0

的NLS assymptote功能的geom_smooth我有以下數據框:如何得到使用SSasympOff

df1<- structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("ALT01"), class = "factor"), Nets = 1:18, Cumulative.spp = c(12L,13L, 15L, 17L, 17L, 17L, 17L, 19L, 19L, 19L, 19L, 20L, 22L, 22L, 22L, 22L, 22L, 22L)), .Names = c("Site", "Nets", "Cumulative.spp"), row.names = c(NA, 18L), class = "data.frame") 

,我試圖讓使用此功能的geom_smooth響應GGPLOT2情節:

Model1<-nls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0), data = df1) 

通常如果我有這樣的模式:

Model2 <- lm(Cumulative.spp ~ I(log(Nets), data = df1) 

我嘗試了兩種方法

方法1

我這樣做:

library(ggplot2) 

ggplot(df1, aes(x=Nets, y = Cumulative.spp)) + geom_point() + geom_smooth(method="lm", formula=y~log(x), fill="blue", fullrange=T) 

enter image description here

但是當我嘗試做的assymptote同它不工作:

ggplot(df1, aes(x=Nets, y = Cumulative.spp)) + geom_point() + geom_smooth(method="nls", formula=y~SSasympOff(x, A, lrc, c0), color="blue", fullrange=T) 

但我得到這個錯誤和這個情節:

Warning message: 
Computation failed in `stat_smooth()`: 
$ operator is invalid for atomic vectors 

enter image description here

方法2

我試圖預測比原先的數據幀得到一個置信區間和區間使用geom_line在預測值和geom_ribbon,但是當我做

predict(Model1, df1, interval = "confidence") 

但我沒有得到置信區間,只有t他預測值

任何幫助,將不勝感激

+0

爲什麼不是更快只需使用'geom_line'添加預測值,然後使用'geom_ribbon'添加間隔? – bouncyball

+0

嗨@bouncyball我試圖這樣做,但我沒有得到間隔,我試着預測(Model1,df1,interval =「confidence」),就像我在'predict.nls'文檔中找到的那樣,但是它不會給我一個置信區間,我會將此添加到我在問題中嘗試的內容。 –

+0

你也許可以使用bootstrap百分比方法,然後......沒有意識到用'nls'獲得置信區間會非常困難。我的錯誤 – bouncyball

回答

1

我想,既然我提出了一個引導的方法我會證明。在這種情況下,我們將增加殘差(see Wikipedia for more information)。我對使用nls不太熟悉,所以有人可能會提出(有效的)理論反對意見。

B <- 2500 # bootstrap iterations, big number 
pred_mat <- matrix(0, nrow = 18, ncol = B) # initialize matrix 
# extract residuals and predictions from original model 
resids <- residuals(Model1) 
preds <- predict(Model1) 
df1$Pred <- preds 
for(i in 1:B){ 
    # bootstrapped dependent variable 
    new_y <- preds + sample(resids, replace = TRUE) 
    df1$NewY <- new_y 
    # fit model 
    Model_Boot <- nls(NewY ~ SSasympOff(Nets, A, lrc, c0), data = df1) 
    # extract predictions 
    pred_mat[,i] <- predict(Model_Boot) 
} 

# add 2.5% and 97.5% percentile intervals to df1 
df1 <- cbind(df1, t(apply(pred_mat, 1, FUN = function(x) quantile(x, c(.025, .975))))) 
# rename appropriately 
names(df1)[6:7] <- c('lower','upper') 

# make plot 
ggplot(df1, aes(x = Nets))+ 
    geom_point(aes(y = Cumulative.spp))+ 
    geom_line(aes(y = Pred))+ 
    geom_ribbon(aes(ymin = lower, ymax = upper), 
       alpha = .2, fill = 'blue') 

enter image description here

0

我發現這種方式做到這一點,我認爲這是一個有點比bouncyballs一個更簡單,但我會讓大家判斷更好的答案,但我會給予好評bouncyball的答案。

我發現這個old post關於它,但我找不到as。LM功能NLS2中,我發現這個link與功能,我決定用as.lm.nls功能

ggplot(df1, aes(x=Nets, y = Cumulative.spp)) + geom_point() + geom_line(y = predict(as.lm.nls(Model1), interval = "confidence")[,1]) + geom_ribbon(ymax = predict(as.lm.nls(Model1), interval = "confidence")[,3], ymin = predict(as.lm.nls(Model1), interval = "confidence")[,2], fill = "blue", alpha = 0.5) 

,我得到這個結果比使用引導方法

enter image description here

+0

不幸的是,'as.lm.nls'函數對nls2 0.1.3中的nls進行線性化似乎產生了很多支持請求,因爲它不能以某些人所期望的方式工作。這不是'as.lm.nls'實現的缺陷,而是對許多人的整個主題如何工作的一個誤解,所以我認爲最好刪除這個函數。 –

+0

@ G.Grothendieck我喜歡它的工作方式,它比引導更容易,我從上面的鏈接中取出它。你能否推薦一個關於線性化NLS的數學過程的引用,我想了解更多。 –

+0

Doug Bates有一本書對此進行了討論。 –