Question

I'm trying to fit a sigmoid to some dose-response data. When I use nls outside of ggplot it works fine. When I use it inside geom_smooth, it throws the following error:

Error in numericDeriv(form[[3L]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model

Sample data looks like this:

x=c(1e-12, 1e-08, 1e-07, 1e-06, 1e-05, 1e-04)
y=c(8.161304, 11.191990, 13.453300, 14.633020, 16.227570, 15.480510)
myData<-data.frame(x=x,y=y)
myEquation=y ~ min+((max-min)/(1+10^(ec50-log10(x))))
startingGuess<-list(ec50=-8, min=8, max=15)

The calls are:

fitModel = nls(myEquation,myData, start = startingGuess,trace=TRUE)

and:

ggplot(data=myData,aes(x=x,y=y)) + 
  geom_point() + 
  scale_x_log10() + 
  geom_smooth(method="nls",formula = myEquation,start = startingGuess,trace=TRUE)    

Am I doing something incorrectly?

Was it helpful?

Solution

When you change your scale, the formula also needs to be changed. Here is a possible solution, although I somehow cannot get confidence intervals to work.

myEquation=y ~ min+((max-min)/(1+10^(ec50-(x))))
ggplot(data=myData,aes(x=x,y=y))+geom_point()+scale_x_log10()+
  geom_smooth(method="nls", formula = myEquation, start = startingGuess, se=FALSE) 

UPDATE: Apparently the reason why confidence intervals do not work, is because standard errors are not currently implemented in predict.nls. Therefore ggplot also cannot display confidence intervals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top