University of Minnesota, Twin Cities School of Statistics Stat 3011 Rweb Textbook (Wild and Seber)
This the example from class (Wednesday, Sept. 20) of drawing a smooth "trend" curve on a scatter plot, like the curves in the figures on p. 109 in Wild and Seber.
plot
command is just like any other plot command.
It draws the box, the axes, and the points. The curve is added later.
library
tells R (Rweb) to make available the commands
from a library. The ksmooth
function
on-line help is in the modreg
library
on-line help which is found under the "Packages" link on the
R documentation main page.
It can also be found by searching for "ksmooth" after following the
"Search Engine & Keywords" link on that same page.
Some libraries are there by default, others aren't. If you try a function
and R (Rweb) says "Error" ... "couldn't find", then either you spelled
the function wrong or you need a library
command.
ksmooth
fits a "smooth" curve to the scatter
plot. The function lines
draws the line on the plot (the already
existing plot made by the plot
command).
The value of the "smooth" curve at a particular x value (call it x0 is the average of the y values in a "window" of width 3.0 centered at x0, that is, the average of all the y values corresponding to x values such that |x - x0| < 1.5.
The value of the "bandwidth" (= box width) of 3.0 was chosen by trial and
error to get the prettiest picture. The argument kernel="box"
says to use a straight average of the y values in the window.
This the same data as the previous example, but a different smoother.
Everything just like the previous example except that we use
supsmu
(the "super smoother") rather than ksmooth
.
The operation of supsmu
is too complicated to explain.
One virtue is that it needs no arguments other than the x
and y
vectors it is to operate on. Another virtue is that
it draws prettier curves.