Stat 518 - Fall 2000 - S-Plus Templates

Class Notes from 9/13/00
Copy, Paste, or Print


The Basics of S-plus:

Unlike SAS where you have to hit a key to run the program, S-Plus runs what you tell it to immediately. It is therefore important to keep a copy of the code you will be running in some other file, in general some word processor when using a PC.

Another difference between S-Plus and SAS is that S-Plus is run from the various Sun Sparcs in the department of mathematics and statistics. To use them from a PC you must thus remote login using the program X-Win32. When you start up X-Win32, nothing at first will happen except for the bar appearing at the bottom of the screen. Selecting one of the sessions there will call up an X-window into which you need to answer the prompt for your login name and password. These will generally NOT be the same as the login and password for the PC.

At the heart of S-Plus are the various objects that you enter. At any time (when you are in S-Plus) you can type in the function objects() to see which ones you have entered previously. Presumably there aren't any there right now. Splus also has many built in objects, most of which are functions. It saves these objects permanently (until you erase them). For this to work though, you need to create a .Data directory in UNIX. This can be done (before starting S-Plus), by typing:

mkdir .Data

Once we have done that, we can now start S-Plus. Simply type Splus at the unix prompt. You can now type objects() to see if anything is already there or not. One of the simplest types of objects to enter is simply a vector. Try typing:

x<-c(5,4,3,2)

The <- is the command that assigns what ever is on the left to the name that is on the right. The c indicates that an array or vector is going to be entered. Simply typing x now will return those four values. Typing x[3] will return the third value, mean(x) will return the mean, and objects() will show you that you now have an object called x.

Say we wished to enter the data set we used last time into S-Plus. We would use the line:


satdata<-scan("",what=list(state="",verbal=0,math=0,pct=0))

S-Plus then comes back with a prompt to enter the data. We can now just cut and paste the block into it. Because UNIX tends to be unhappy if you try to cut and paste a whole bunch at one time, it is probably better to do it in to chunks. Paste the first chunk in by clicking with both mouse buttons, make sure there is a prompt showing, and then paste the second half in and hit return until it gets out of scan mode. Now, typing satdata will show you that the data set is in there.

The SAT data presented in the August 31st, 1999 New York Times:


Ala. 561 555 9
Alaska 516 514 50
Ariz. 524 525 34
Ark. 563 556 6
Calif. 497 514 49
Colo. 536 540 32
Conn. 510 509 80
Dela. 503 497 67
D.C. 494 478 77
Fla. 499 498 53
Ga. 487 482 63
Hawaii 482 513 52
Idaho 542 540 16
Ill. 569 585 12
Ind. 496 498 60
Iowa 594 598 5
Kan. 578 576 9
Ky. 547 547 12
La. 561 558 8
Maine 507 507 68
Md. 507 511 65
Mass. 511 511 78
Mich. 557 565 11
Minn. 586 598 9
Miss. 563 548 4
Mo. 572 572 8
Mont. 545 546 21
Neb. 568 571 8
Nev. 512 517 34
N.H. 520 518 72
N.J. 498 510 80
N.M. 549 542 12
N.Y. 495 502 76
N.C. 493 493 61
N.D. 594 605 5
Ohio 534 538 25
Okla. 576 560 8
Ore. 525 525 53
Pa. 498 495 70
R.I. 504 499 70
S.C. 479 475 61
S.D. 585 588 4
Tenn. 559 553 13
Texas 494 499 50
Utah 570 565 5
Vt. 514 506 70
Va. 508 499 65
Wash. 525 526 52
W.Va. 527 512 18
Wis. 584 595 7
Wyo. 546 551 10

We have now made a "data-frame" where each of the four columns goes with a different variable. We can refer to the four columns by using satdata$state, satdata$verbal, satdata$math, or satdata$pct. The following code will give us many of the results we got looking at this data set in SAS. But first we'll begin by giving all of the variables shorter names.


v<-satdata$verbal

m<-satdata$math
p<-satdata$pct

summary(p)

hist(p)

qqnorm(p)
qqline(p)
Unfortunately S-Plus removed the interactive help menus, but you can still find out more about changing the histogram for example by typing help(hist). The code below performs a linear regression to predict v from m. It is often best to put the regression output into an object though instead of simply running it.



vmregress<-lm(v~m)

vmregress

attributes(vmregress)

vmregress$residuals

qqnorm(vmregress$residuals)
qqline(vmregress$residuals)
Finally, this is the code that would be used for making the confidence interval for the variance of the verbal scores.

alpha<-.05

df<-length(v)-1
clow <- df*var(v)/qchisq(1-alpha/2,df)
chi <- df*var(v)/qchisq(alpha/2,df)
c(clow,chi)


Notes on Copying, Pasting, and Printing Graphics:

In general, you will want to type your programs in MS Word or notepad, and then copy and paste them into the X-window. To copy from the X-window, simply highlight the text you want with the left mouse button. To paste into the X-window, simply hit both mouse buttons simultaneously.

To print a graphic, you need to set it to the right printer. Otherwise the default is the printer that the SUN machine is actually in. (Probably 310 LeConte.) To print to the printer LEX-3 in 303A, you would need to go to the "Options" menu, and choose "Printing..." In the command box, make it say lp -dlex3  Now simply click the "Print" button, and note that it should say at the bottom of the graphic window that it has printed. Simply hit close to make that window go away. Note that you can't view or print the graphics if you are using telnet instead of X-windows.