[R] a question on plotting nonlinear regression

Mark Seeto markseeto at gmail.com
Mon Jul 25 00:17:07 CEST 2011


garciap wrote:
> 
> Hi to all the people,
> 
> I'm having a trouble when trying to plot a quadratic function. I have the
> code:
> 
> regression<-nls(Survival~beta1+beta2*PI+beta3*PI^2, data=cubs,
> start=list(beta1 = 1, beta2 = 1, beta3 = 1))
> plot(Survival~PI,data=cubs, ylab="Survival", xlab="PI")
> lines(cubs$PI, fitted(regression))
> 
> but the plotted line is not the quadratic one, rather it appears a number
> of lines whose meaning I do not know. It seems to be an stupid trouble,
> but I tried a number of alternatives and nothing works.
> 
> Regards,
> 
> pablo
> 

The problem could be that cubs$PI is not sorted. You could try

plot(Survival~PI,data=cubs, ylab="Survival", xlab="PI")
lines(sort(PI), fitted(regression)[order(cubs$PI)]) 

Also, you could use linear regression with lm instead of using nls.

Regards,
Mark


--
View this message in context: http://r.789695.n4.nabble.com/a-question-on-plotting-nonlinear-regression-tp3690828p3691143.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list