STA 261S, Spring 2004, Computing Information

This page provides some information about statistical computing for STA 261S in Spring, 2004. Computing is needed for some of the homework in this course; it is optional, but is recommended to increase understanding.

For statistical computing, I recommend the free-software package "R", which is discussed below. (Other statistical computing packages may also be used; e.g. on CQUEST you can also run minitab, Splus, or sas. I will not discuss these here, but minitab is discussed in Appendix B of the textbook.)

To run R on CQUEST, type:

/u/jeff/R
Even better, to obtain R for your own computer (absolutely free!!), go to
http://probability.ca/cran/
and, under "Precompiled Binary Distributions", download R for your computer type (Linux, Mac, Windows, ...). [For Windows users, select the "base" package, and then download and run the install file "rw1081.exe".]

Examples of using R:

Once you have installed R on your computer (or are running it on CQUEST), then you can type R commands after the R prompt ("> ").

Simple arithmetic may be done directly, including simple expressions like

3 + 4
or more complicated expressions like
3.7 + 11 * exp(2) - 17^2 + sqrt(5) / 3
Variables may be assigned by typing "=", e.g. to assign the variable "w" the value 7.2, type:
w = 7.2
Alternatively, the "=" may be replaced by "<-" (i.e. "less-than" followed by "dash"), as in "w <- 7.2".

Lists of data may be entered directly using the "c( )" function, for example:

x = c(2, 4, 1.7, -3)
Once this is done, then typing just "x" will output x as a list (i.e. vector).

Once x is equal to a list, then its mean (x-bar), variance (s^2), and standard deviation (s) can be computed:

mean(x)
var(x)
sd(x)
[Of course, "sd(x)" is the same as "sqrt(var(x))".] If you wish, you can assign these values to other variables, e.g. "mu = mean(x)", "s = sd(x)", etc.

Lists can themselves be operated on. In the example above, "x^2" would produce the list (4, 16, 2.89, 9), so that e.g. "mean(x^2)" would give 7.9725. Equivalently, you could first do "z = x^2", and then "mean(z)" would also be 7.9725.

R also has excellent plotting features. For example, "plot(x)" will plot the individual values of x above, while "hist(x)" will display a histogram. Also, "pie(x^2)" will display a pie-chart of x^2 (or any other non-negative list). Try them! [If you prefer, you can save your plot as a pdf file, by typing "pdf()", then "plot(x)" (or whatever), and then "dev.off()". If you want a jpeg or postscript file instead of pdf, then type "jpeg()" or "postscript()" instead of "pdf()".]

R can also generate random values. For example, to generate 50 i.i.d. draws from a standard normal distribution, type "rnorm(50)". To save them as a list called "y", type "y = rnorm(50)". Then you can compute their mean etc. by "mean(y)", "var(y)", "sd(y)", etc.

To generate from other distributions, use e.g. "rbinom(50, 10, 0.3)" for the Binomial(50, 0.3) distribution, "rpois(5, 14)" for the Poisson(5), "rexp(5, 0.3)" for Exponential(0.3), etc. To compute cdf's, use e.g. "pnorm(-1.2)" for the standard normal, "pexp(3, 0.3)" for Exponential(0.3), "pchisq(74.22, 100)" for the chisquared(100) distribution, "pt(2.228, 10)" for the t(10) distribution, "pf(5.7, 2, 10)" for the F(2, 10) distribution, etc.; for densities use "dnorm(-1.2)", "dexp(3, 0.3)", etc.

Once you have typed "y = rnorm(50)", then you can e.g. get a histogram of this sample by typing

hist(y)
To then e.g. get a second histogram, of a fresh sample, overlaid on the first, in a different colour, you could type
hist(rnorm(50), add=TRUE, border=2)

When typing commands in R, you can use the up-arrow key to retrieve previous commands, and the left-arrow key to edit a command that you've already typed.

R has lots of built-in help available. For example, to find out more about the "hist" function, simply type "help(hist)" or "?hist" after the R prompt. Or, for an interactive R-help web interface, type "help.start()".

R has a huge number of features and options not mentioned here. [In fact, R is a full-featured programming language, too!] There is lots of documentation about R available on the web, see e.g. here or here or here or here or here.

When you are done with your computations, type "q()" to quit R.

NOTE: I will add more information to this page if/when it is needed to solve the computer exercises in the homework for STA 261S.


[Return to STA261 main page.]