[R] add non-linear line

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jan 17 18:31:57 CET 2007


On 1/17/2007 12:24 PM, Duncan Murdoch wrote:
> On 1/17/2007 10:42 AM, Matt Sakals wrote:
>> I am trying to plot a non-linear trend line along with my data. I am  
>> sure the solution is simple; any help greatly appreciated.
>> 
>> Recent and historic attempts:
>> fit = nls((sd~a+b*exp(-c/time)+d*geology), start=list(a=1, b=1, c=10,  
>> d=-1), model=TRUE)
>> plot(time, sd, col=3, main = "Regression", sub=formula(fit))
>> lines(time, fitted.values(fit), lwd=2)
>> 
>> #tt = seq(from=0, to=200, by=10)
>> #lines(time, predict(fit, time=tt))
>> #lines(time, predict(fit))
> 
> Your model has two independent variables, time and geology.  You won't 
> get a simple trend line unless the value of geology is kept fixed.  You 
> could do that with something like this code:
> 
> lines(time, predict(fit, newdata=data.frame(time=time, geology=0)))

One other suggestion:  in the new data, make sure your "time" variable 
is sorted into increasing order, or the lines will be a mess.

Duncan Murdoch



More information about the R-help mailing list