2
votes

I am trying following code to get non-linear regression without specifying any particular function:

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)

But it gives me following warning:

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

The plot is shown only with points. No regression line is plotted.

How can I correct this problem?

1

1 Answers

1
votes

The things needed by method = "nls" are almost the same as arguments of stats::nls(). You need to give a nonlinear model formula including variables and parameters, and se = FALSE (reference: 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