[R] Drawing a loess line

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Feb 4 00:44:24 CET 2008


hits=-2.6 testsºYES_00
X-USF-Spam-Flag: NO

On Sun, 2008-02-03 at 22:26 +0100, Marcin Kozak wrote:
> Thanks a lot, it works.
> 
> Any ideas what's going on here?
> 
> y<-c(1.75, 1.41, 1.96, 1.03, 2.38, 2.19, 1.81, 1.91, 1.47, 2.25, 1.53,
> 2.79, 2.54, 2.36, 2.65,
> 2.69, 2.58, 3.27, 3.52, 2.93)
> x<-c(0.59, 0.49, 0.65, 0.41, 0.84, 0.87, 0.69, 0.72, 0.67, 0.93, 0.76,
> 1.04, 0.87, 0.92, 0.92,
> 1.04, 0.94, 1.15, 1.13, 1.09)
> plot(x,y,xlim=c(0.4,2.5))
> fit<-loess(y~x)
> lines(predict(fit), col="blue")

You aren't giving lines the y-axis values to plot at. And if you did
specify both x and y coordinates, it would be a mess as the values of x
are not increasing - try this to see what I mean:

lines(predict(fit)~x, col="blue")

Better to predict for new observations, spaced evenly across the
observed range of x

First create the new x values we want to predict at. We create a data
frame with a variable 'x':

newx <- data.frame(x = seq(from = min(x), to = max(x), length = 100))

Next we do the plotting:

lines(predict(fit, newdata = newx) ~ x, data = newx, col = "blue") 

Fit these two lines in, in place of your call to lines.

Is that what you wanted?

G

> 
> Marcin
> 
> On Feb 3, 2008 9:37 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> > Try this:
> >
> > cars.lo <- loess(dist ~ speed, cars)
> > with(cars, plot(speed, dist))
> > lines(predict(cars.lo), col="blue")
> >
> >
> >
> >
> > On 03/02/2008, Marcin Kozak <nyggus at gmail.com> wrote:
> > > Dear all,
> > >
> > > To draw a lowess line on a plot was a piece of cake; to draw a loess
> > > line, however, seems not that easy. Is the loess plotting implemented
> > > at all in relation to the loess function, or do I have to look in
> > > add-on packages?
> > >
> > > Thanks,
> > > Marcin
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
> > --
> > Henrique Dallazuanna
> > Curitiba-Paraná-Brasil
> > 25° 25' 40" S 49° 16' 22" O
> >
> 
> 
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list