S-PLUS: An Introductory Session
Next: The Inbuilt Command
Up: Notes on S-PLUS:
A
Previous: Multiple graphics devices
The following session is intended to introduce to you some features of the
S-PLUS environment by using them. Many features of the system will be
unfamiliar and puzzling at first, but this will soon disappear.
- login:
...
ls -a
ls -a .Data
- Login, start your windowing system (ask a demonstrator if you need
help), and check that your working directory has a subdirectory
.Data, which in turn contains the files .First,
.Last and possibly .Audit.
You should also have the file morley.data in your
working directory. If not, see a demonstrator. If you have, proceed.
- Splus -e
- Start Splus with the inbuilt command line editor enabled.
The S-PLUS program begins, with a banner.
- help.findsum(".Data")
- Set things up for the help facility. (Need only be done once for
this directory.)
- help.start()
- Start the help facility. You should briefly explore the features
of this facility with the mouse. Standard X window
conventions apply.
Iconify the help window and move on to the next part.
- motif()
- Turn on the graphics window. You may need to re-position and
re-size to make it convenient to work with both windows.
- x <- rnorm(50)
y <- rnorm(x)
- Generate two pseudo random normal vectors of x- and
y-coordinates.
- hull <- chull(x, y)
- Find their convex hull in the plane.
- plot(x, y)
polygon(x[hull], y[hull], dens=15)
- Plot the points in the plane, and mark in their convex hull.
- objects()
- See which S-PLUS objects are now in the .Data
directory.
- rm(x,y,hull)
- Remove objects no longer needed. (cleanup).
- x <- 1:20
- Make x = (1,2,...,20)
- w <- 1 + sqrt(x)/2
- A `weight' vector of standard deviations.
- dummy <- data.frame(x=x, y= x + rnorm(x)*w)
dummy
- Make a data frame of two columns,x and y,
and look at it.
- fm <- lm(y~x, data=dummy)
summary(fm)
- Fit a simple linear regression of y on x and look
at the analysis.
- fm1 <- lm(y~x, data=dummy, weight=1/w^2)
summary(fm1)
- Since we know the standard deviations, we can do a weighted
regression.
- lrf <- loess(y~x, dummy)
- Make a nonparametric local regression function.
- attach(dummy)
- Make the columns in the data frame visible as variables.
- plot(x, y)
- Standard point plot.
- lines(x, fitted(lrf))
- Add in the local regression.
- abline(0, 1, lty=3)
- The true regression line: (intercept 0, slope 1).
- abline(coef(fm))
- Unweighted regression line.
- abline(coef(fm1),lty=4)
- Weighted regression line.
At any time you can make a hardcopy of the graphics window by clicking on
the Graph section of the window and selecting the Print option.
- detach()
- Remove data frame from the search list.
- plot(fitted(fm), resid(fm),
xlab="Fitted values",
ylab="Residuals", main=
"Residuals vs Fitted")
- A standard regression diagnostic plot to check for
heteroscedasticity. Can you see it?
- qqnorm(resid(fm), main="Residuals Rankit Plot")
- A normal scores plot to check for skewness, kurtosis and
outliers. (Not very useful here.)
- rm(fm,fm1,lrf,x,dummy)
- Clean up again.
The next section will look at data from the classical experiment of
Michaelson and Morley to measure the speed of light.
- !more morley.data
- Optional. Temporarily interrupt S-PLUS and look at the
file. This is a standard way to escape to the operating system.
- mm <- read.table("morley.data")
mm
- Read in the MM data as a data frame, and look at it. There are
five experiments (col. Expt) and each has 20 runs
(col. Run) and sl is the recorded speed of light,
suitably coded.
- attach(mm, 1)
objects()
- Place mm on the top of the search list, (position 1).
- Expt <- factor(Expt)
Run <- factor(Run)
- Change Expt and Run into factors.
- detach(1, save="mm")
attach(mm)
- Save the changes and make the data frame visible at position 2
(the default).
- plot(Expt,Speed, main= "Michaelson Morley Data",
xlab="Experiment No.")
- Compare the five experiments with simple boxplots.
- fm <- aov(Speed~Run+Expt, data=mm)
summary(fm)
- Analyse as a randomized block, with `runs' and `experiments' as
factors.
- fm0 <- update(fm, .~.-Run)
anova(fm0,fm)
- Fit the sub-model omitting `runs', and compare using a formal
analysis of variance.
- detach()
rm(fm, fm0)
- Cleanup before moving on.
We now look at some more graphical features: contour and
3-dimensional perspective plots.
- x <- seq(-pi, pi, len=50)
y <- x
- x is a vector of 50 equally spaced values in
. y is the
same.
- f <- outer(x, y, function(x,y) cos(y)/(1+x^2))
- f is a square matrix, with rows and columns indexed by
x and y respectively, of values of the function
.
- oldpar <- par()
par(pty="s")
- Save the plotting parameters and set the plotting region to
``square''.
- contour(x, y, f)
contour(x, y, f, nint=15, add=T)
- Make a contour map of f; add in more lines for more detail.
- fa <- (f-t(f))/2
- fa is the ``asymmetric part'' of f.
(t() is transpose).
- contour(x, y, fa, nint=15)
- Make a contour, and ...
- par(oldpar)
- ... restore the old graphics parameters.
- persp(x, y, f)
persp(x, y, fa)
image(x, y, f)
image(x, y, fa)
- Make some pretty perspective and high density image plots, (of
which you can get hardcopies if you wish)
- objects()
rm(x,y,f,fa)
- and clean up before moving on.
th <- seq(-pi, pi, len=100)
z <- exp(1i*th)
S-PLUS can do complex arithmetic, also. 1i is
used for the complex number i
par(pty="s")
plot(z, type="l")
Plotting complex arguments means plot imaginary versus real
parts. This should be a circle.
w <- rnorm(100) + rnorm(100)*1i
Suppose we want to sample points within the unit circle. One
method would be to take complex numbers with standard normal real and
imaginary parts ...
w <- ifelse(Mod(w) > 1, 1/w, w)
and to map any outside the circle onto their reciprocal.
plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+",
xlab="x", ylab="y")
lines(z)
All points are inside the unit circle, but the distribution is
not uniform.
w <- sqrt(runif(100))*exp(2*pi*runif(100)*1i)
plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+",
xlab="x", ylab="y")
lines(z)
The second method uses the uniform distribution. The points
should now look more evenly spaced over the disc.
rm(th,w,z)
Cleanup again.
par(oldpar)
Restore standard graphics parameters.
butterfly()
An old favourite. Take a hardcopy if you wish.
rm(oldpar)
Cleanup again.
q()
Quit the S-PLUS program and return to Unix.
Next: The Inbuilt Command
Up: Notes on S-PLUS:
A
Previous: Multiple graphics devices
Erik Moledor
Tue Jan 31 21:02:18 EST 1995