2016-12-04 64 views
2

的geom_smooth不工作我想下面的代碼獲取非線性迴歸不指定任何特定功能:NLS方法ggplot

library(drc) 
head(heartrate) 
# pressure rate 
#1 50.85 348.76 
#2 54.92 344.45 
#3 59.23 343.05 
#4 61.91 332.92 
#5 65.22 315.31 
#6 67.79 313.50 

library(ggplot2) 
ggplot(heartrate, aes(pressure, rate)) + 
    geom_point() + 
    geom_smooth(method="nls", formula = rate ~ pressure) 

但它給了我以下警告:

Warning message: 
Computation failed in `stat_smooth()`: 
object of type 'symbol' is not subsettable 

的情節僅以點顯示。沒有繪製迴歸線。

我該如何解決這個問題?

回答

1

method = "nls"所需的東西幾乎與stats::nls()的參數相同。您需要給出一個包含變量和參數的非線性模型公式,並且se = FALSEreference: R mailing)。

ggplot(heartrate, aes(x = pressure, y = rate)) + # variable names are changed here 
    geom_point() + 
    geom_smooth(method="nls", formula = y ~ x, 
       method.args = list(start = c(x = 1)), se = F, colour = "green3") + 
    geom_smooth(method="nls", formula = y ~ a * x, 
       method.args = list(start = c(a = 1)), se = F, colour = "blue") + 
    geom_smooth(method="nls", formula = y ~ a * x + b, 
       method.args = list(start = c(a = 1, b = 1)), se = F, colour = "red") 

enter image description here