How to run S-Plus on the Mathematics Network Leemis Fall, 2008 S-Plus is an interpreted, object-oriented statistical software package capable of doing classical and modern statistical computations, data manipulation, and custom graphics. The package was originally written at AT&T Bell Labs and is now owned by Insightful Corp. Entering S-Plus Splus starts S-Plus Splus -e starts S-Plus with ability to repeat commands with up arrow Splus -g starts S-Plus in a GUI Exiting S-Plus q() exits S-Plus Executing a file of commands source("fn") executes the commands in filename fn Sending output to a file sink("fn") sends output to filename fn rather than the screen Comments and help # comment comment for documentation help(command) help on command [type help.start() first on the math network] Suspending S-Plus ^Z suspends S-Plus (fg returns) Sample session > # 1. WORKING WITH SCALARS > > 2 + 2 > > 2 + 3 / 5 > > (2 + 3) / 5 > > sqrt(2) > > (sin(8) + exp(4)) / 3 > > gamma(3.5) > > # 2. WORKING WITH VECTORS > > x <- c("Hi there", 3.6, 4) > > x > > x <- scan() > # Enter ends input > x > > x <- c(5, -6, 8, 0, -2, 3) > > x > > x[3] > > x[3:6] > > x[c(3, 6)] > > x[-4] > > x[-4:-6] > > x[-c(2,5)] > > # 2A. Logical subscripts > > x[x > 0] > > x[x > mean(x)] > > y <- x[x > mean(x)] > > x[x < 0] <- 0 > > name _ c("Caxton", "Fielding", "Stuart", "Brown", "Larson") > age _ c(28, 43, 34, 48, 37) > salary _ c(32000, 47000, 36000, 62000, 45000) > shoe _ c(10.5, 12.0, 9.5, 10.0, 11.0) > > shoe[name == "Larson"] > > name[age < 35 & salary < 35000] > > # 2B. Functions on one vector > > # sum(x), prod(x), sort(x), min(x), max(x), range(x), length(x), mean(x), > # var(x), sin(x), floor(x), ceiling(x), log(x), log10(x), gamma(x), > # cumsum(x), cummax(x), ... > > x <- c(5, -6, 8, 0, -2, 3) > > max(x) > > mean(x) > > sqrt(var(x)) > > sort(x) > > gamma(x) > > mode(x) > > length(x) > > # 2C. Functions on two vectors > > y <- c(rep(1, 3), 8:9, 17) > > pmax(x, y) > > cor(x, y) > > # 2D. Probability functions > > pnorm(-1.5) > > qnorm(0.98) > > dnorm(1) > > rnorm(40) > > x <- rpois(40, 2.6) > > pchisq(20, 16) > > set.seed(6) > > rbinom(100, 1, 1 / 2) > > # 2E. Writing your own functions > > hyp <- function(a, b) { > temp <- a ^ 2 + b ^ 2 > sqrt(temp) > } > > hyp(3, 4) > > # QUIZ. For a vector x with numerical entries. > # > # 1. Find the mean of the positive elements of x > # 2. Find the variance of the negative elements of x > # 3. Find the sum of the elements of x, excluding the largest and > # smallest > # 4. Find the subscripts of the positive elements of x > > > # 3. MATRICES > > xmat <- matrix(1:20, 4, 5) > > length(xmat) > > dim(xmat) > > mode(xmat) > > ymat <- matrix(runif(20), 5, 4, byrow = T) > > xmat + matrix(20:1, 4, 5) > > t(xmat) > > rbind(xmat, t(ymat)) > > zmat <- xmat %*% ymat > > sum(diag(zmat)) > > eigen(zmat) > > xmat[2, 3] > > xmat[2, ] > > xmat[, 3] > > xmat[2:4, ] > > mean(xmat[1:3, ] > 15) > > # 4. ARRAYS > > a <- array(rpois(60, 3.3), dim = c(3, 4, 5)) > > a[1, 2, 3] > > a[ , , 5] > > a[1, ,4] > > # 4A. The apply function > > b <- array(1:6, dim = c(2, 3)) > > apply(b, 1, sum) > > apply(b, 2, sum) > > apply(b, c(1, 2), sqrt) > > # 5. Data frames > > # 6. Programming > > for (i in 1:17) print(c(i, i ^ 2, i ^ 3)) > > x = 3 > if (x >= 0) print("x is nonnegative") else print("x is negative") > > > # 7. Built-in data sets > > state.x77 > > state.abb > > state.x77[,8] > > sort(state.x77[,8]) > > state.x77[8,] > > plot(state.x77[,6], state.x77[,7], > xlab = "Percent HS Grads", ylab = "Number days of frost") > > cu.dimensions > bladder > cereal.attitude > > # 8. Built-in statistical procedures > > x _ c(1986, 1987, 1988, 1989, 1990) > y _ c(2.13, 2.35, 2.60, 2.86, 3.23) > reg _ lsfit(x, y) > reg > > fit _ lm(y ~ x) > fit > summary(fit) > > # 9. Graphics > > plot(x, y) > > temp <- rnorm(300) > > hist(temp) > > usa() > > tsplot(rain.nyc1, main = "NYC Rain Data", ylab = "Rain in inches") > > qqnorm(auto.stats[, "Weight"], ylab = "Weight") > > contour(switzerland) > > persp(switzerland) > > image(voice.five) > > brush(state.x77) > > q() > > # QUIZ SOLUTIONS > > mean(x[x > 0]) > var(x[x < 0]) > sum(sort(x)[-c(1, length(x))]) > y <- c(1:length(x)) > y[x > 0]