%
LaTeX source for Sample Session with R\documentclass{article} \usepackage{longtable} \begin{document} \begin{flushleft} {\Large\textbf{Appendix A A Sample session}} \end{flushleft} The following session is intended to introduce you to some features of the \textsf{R} environment. Many features will be unfamiliar and puzzling at first, but this puzzlement will soon disappear. The commands are stored in executable form in the file \texttt{samplesession.r}. \begin{longtable}{ll} & Login and start \textsf{R}. \\ \multicolumn{2}{l}{\texttt{help.start()}} \\ & Start the \textsc{html} interface to on-line help (using a web \\ & browser available at your machine). You should briefly \\ & explore the features of this facility with the mouse. \\ & Iconify the help window and move on to the next part. \\ \multicolumn{2}{l}{\texttt{x <- rnorm(50)}} \\ \multicolumn{2}{l}{\texttt{y <- rnorm(50)}} \\ & Generate two pseudo-random normal vectors of $x-$ \\ & and $y-$coordinates. \\ \multicolumn{2}{l}{\texttt{plot(x,y)}} \\ & Plot the points in the place. A graphics window will \\ & appear automatically. \\ \multicolumn{1}{l}{\texttt{ls()}} & See which \textsf{R} objects are now in the \textsf{R} workspace. \\ \multicolumn{1}{l}{\texttt{rm(x,y)}} & Remove objects no longer needed. (Clean up). \\ \multicolumn{1}{l}{\texttt{x <- 1:20}} & Make $x=(1,2, \dots, 20)$. \\ \multicolumn{2}{l}{\texttt{w <- 1 + sqrt(x)/2}} \\ & A `weight' vector of standard deviations. \\ \multicolumn{2}{l}{\texttt{dummy <- data.frame(x=x, y=x + rnorm(x)*w)}} \\ \multicolumn{1}{l}{\texttt{dummy}} & Make a \textit{data frame} of two columns, $x$ and $y$, and look \\ & at it. \\ \multicolumn{2}{l}{\texttt{fm <- lm(y $\sim$ x, data=dummy)}} \\ \multicolumn{2}{l}{\texttt{summary(fm)}} \\ & Fit a simple linear regression of $y$ on $x$ and look at the \\ & analysis. \\ \multicolumn{2}{l}{\texttt{fm1 <- lm(y $\sim$ x, data=dummy, weight=1/w\^{}2)}} \\ \multicolumn{2}{l}{\texttt{summary(fm1)}} \\ & Since we know the standard deviations, we can do a \\ & weighted regression. \\ \multicolumn{2}{l}{\texttt{attach(dummy)}} \\ & Make the columns in the data frame visible as variables. \\ \multicolumn{2}{l}{\texttt{lrf <- lowess(x,y)}} \\ & Make a nonparametric local regression function. \\ \multicolumn{2}{l}{\texttt{plot(x,y)}} \\ & Standard point plot. \\ \multicolumn{2}{l}{\texttt{lines(x, lrf\$y)}} \\ & Add in the local regression. \\ \multicolumn{2}{l}{\texttt{abline(0, 1, lty=3)}} \\ & The true regression line; (intercept 0, slope 1). \\ \multicolumn{2}{l}{\texttt{abline(coef(fm))}} \\ & Unweighted regression line. \\ \multicolumn{2}{l}{\texttt{abline(coef(fm1), col="red")}} \\ & Weighted regression line. \\ \multicolumn{1}{l}{\texttt{detach()}} & Remove data frame from the search path. \\ \multicolumn{2}{l}{\texttt{fitted(fm), resid(fm),}} \\ \multicolumn{1}{l}{\hspace{2cm}\texttt{xlab=}} & \texttt{"Fitted values",} \\ \multicolumn{1}{l}{\hspace{2cm}\texttt{ylab=}} & \texttt{"Residuals",} \\ \multicolumn{1}{l}{\hspace{2cm}\texttt{main=}} & \texttt{"Residuals vs Fitted",} \\ & A standard regression diagnostic plot to check for \\ & heteroscedasticity. Can you see it? \\ \multicolumn{2}{l}{\texttt{qqnorm(resid(fm), main="Residuals Rankit Plot")}} \\ & A Normal scores plot to check for skewness, kurtosis \\ & and outliers. (Not very useful here.) \\ \multicolumn{2}{l}{\texttt{rm(fm, fm1, lrf, x, dummy)}} \\ & Clean up again. \\ \multicolumn{2}{l}{\quad The next section will look at data from the classical experiment of} \\ \multicolumn{2}{l}{\texttt{Michaelson and Morley to measure the speed of light.}} \\ \multicolumn{2}{l}{\texttt{data(morley)}} \\ \multicolumn{2}{l}{\texttt{mm <- morley}} \\ \multicolumn{1}{l}{\texttt{mm}} & Copy the data to a data frame \texttt{mm}, and look at it. \\ & There are five experiments (column \texttt{Expt}) and each \\ & has 20 runs (column \texttt{Run}) and \texttt{Speed}) is the \\ & recorded speed of light, suitably coded. \\ \multicolumn{2}{l}{\texttt{mm\$Expt <- factor(mm\$Expt)}} \\ \multicolumn{2}{l}{\texttt{mm\$Run <- factor(mm\$Run)}} \\ & Change \texttt{Expt} and \texttt{Run} into factors. \\ \multicolumn{2}{l}{\texttt{attach(mm)}} \\ & Make the data frame visible at position 2 (the default). \\ \multicolumn{2}{l}{\texttt{plot(Expt, Speed, main="Speed of Light Data", xlab="Experiment No.")}} \\ & Compare the five experiments with simple boxplots. \\ \multicolumn{2}{l}{\texttt{fm <- aov(Speed $\sim$ Run + Expt, data=mm)}} \\ \multicolumn{2}{l}{\texttt{summary(fm)}} \\ & Analyze as a randomized block, with `runs' and \\ & `experiments' as factors. \\ \multicolumn{2}{l}{\texttt{fm0 <- update(fm, . $\sim$ . - Run)}} \\ \multicolumn{2}{l}{\texttt{anova(fm0,fm)}} \\ & Fit the sub-model omitting `runs', and compare using a \\ & formal anlaysis of variance \\ \multicolumn{2}{l}{\texttt{detach()}} \\ \multicolumn{2}{l}{\texttt{rm(fm,fm0)}} \\ & Clean up before moving on. \\ \multicolumn{2}{l}{\quad We now look at some more graphical features: contour and image plots.} \\ \multicolumn{2}{l}{\texttt{x <- seq(-pi, pi, len=50)}} \\ \multicolumn{1}{l}{\texttt{y <- x}} & $x$ is a vector of 50 equally spaced values in $-pi\le x\le\pi$. \\ & $y$ is the same. \\ \multicolumn{2}{l}{\texttt{f <- outer(x, y, function(x, y) cos(y)/(1 + x\^2))}} \\ & $f$ is a square matrix, withn rows and columns indexed \\ & by $x$ and $y$ respectively, of values of the function \\ & $\cos(y)/(1+x^2)$. \\ \multicolumn{2}{l}{\texttt{oldopar <- par(no.readonly = TRUE)}} \\ \multicolumn{2}{l}{\texttt{par(pty="s")}} \\ & Save the plotting parameters and set the plotting region \\ & to ``square''. \\ \multicolumn{2}{l}{\texttt{contour(x, y, f)}} \\ \multicolumn{2}{l}{\texttt{contour(x, y, f, nlevels=15, add=TRUE)}} \\ & Make a contour map of $f$; add in more lines for more \\ & detail. \\ \multicolumn{2}{l}{\texttt{fa <- (f-t(f))/2}} \\ & \texttt{fa} is the ``asymmetric part'' of $f$. (\texttt{t()} is transpose). \\ \multicolumn{2}{l}{\texttt{contour(x, y, fa, nint=15)}} \\ & Make a contour plot, \dots \\ \multicolumn{2}{l}{\texttt{par(oldpar)}} \\ & \dots and restore the old graphics parameters. \\ \multicolumn{2}{l}{\texttt{image(x, y, f)}} \\ \multicolumn{2}{l}{\texttt{image(x, y, fa)}} \\ & Make some high density image plots, (of which you can \\ & get hardcopies if you wish), \dots \\ \multicolumn{2}{l}{\texttt{objects(); rm(x, y, f, fa)}} \\ & \dots and clean up before moving on. \\ \multicolumn{2}{l}{\quad\textsf{R} can do complex arithmetic, also.} \\ \multicolumn{2}{l}{\texttt{th <- seq(-pi, pi, len=100)}} \\ \multicolumn{2}{l}{\texttt{z <- exp(1i*th)}} \\ & \texttt{1i} is used for the complex number $i$. \\ \multicolumn{2}{l}{\texttt{par(pty="s")}} \\ \multicolumn{2}{l}{\texttt{plot(z, type="l")}} \\ & Plotting complex arguments means plot imaginary \\ & versus real parts. This \\ & should be a circle. \\ \multicolumn{2}{l}{\texttt{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 \dots \\ \multicolumn{2}{l}{\texttt{w <- ifelese(Mod(w) > 1, 1/w, w)}} \\ & \dots and to map any outside the circle onto their \\ & reciprocal. \\ \multicolumn{2}{l}{\texttt{plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+", xlab="x", ylab="y")}} \\ \multicolumn{2}{l}{\texttt{lines(x)}} \\ & All points are inside the unit circle, but the \\ & distribution is not uniform. \\ \multicolumn{2}{l}{\texttt{w <- sqrt(runif(100)*exp(2*pi*runif(100)*1i)}} \\ \multicolumn{2}{l}{\texttt{plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="=", xlab="x", ylab="y")}} \\ \multicolumn{2}{l}{\texttt{lines(z)}} \\ & The second method uses the uniform distribution. \\ & The points should now look more evenly spaced over \\ & the disc. \\ \multicolumn{2}{l}{\texttt{rm(th, w, z)}} \\ & Clean up again. \\ \multicolumn{1}{l}{\texttt{q()}} & Quit the \textsf{R} program. You will be asked if you want to \\ & save the \textsf{R} workspace, and for an exploratory session \\ & like this, you probably do not want to save it. \\ \end{longtable} \end{document} %