Getting started
S+ can be found on howard.psych.nwu.edu which is a computer in the psychology department. S+ can be accessed from any computer in one of two ways. If the computer is connected to the Northwestern ACNS network, one can connect to howard.psych.nwu.edu via Telnet by opening a connection to that location. Alternatively, one can use a modem to dial in to the ACNS network (the #s are 467-1500, 467-2300, and 467-2301). In both cases, you need an account on howard before you can use S+.
Once you have successfully logged onto howard (by entering your userid and password), you can run S+ by typing Splus at the prompt (UNIX is case-sensitive so you need to make sure the first letter is capitalized). You should see the following on your screen:
howard.psych.nwu.edu % Splus
S-PLUS : Copyright (c) 1988, 1995 MathSoft, Inc.
S : Copyright AT&T.
Version 3.3 Release 1 for Sun SPARC, SunOS 4.1.x : 1995
Working data will be in .Data
>
The greater-than symbol (>) is the prompt in S+. Once you are at this point you can begin doing analyses.
Example 1: One-Way Between-Subjects ANOVA
Consider Appendix I for data from an experiment where individual subjects were given one of three possible dosages of a drug, and their alertness level was subsequently measured. Before this data can be analyzed, it must be entered in a format that S+ can understand. You can do this in two different ways:
1. Converting ASCII text files to S+ data files. The first way is to create and save an ASCII (text) file containing all the data onto your howard account. You can create this file using UNIX text editors such as emacs, vi, or qedit. If you are uncomfortable or unfamiliar with these editors, you can create the text file by using your favorite word processor, saving the file in ASCII format, and FTPing it to your howard account. Your text file should look something like the following:
Dosage Alertness
a 30
a 38
a 35
a 41
a 27
a 24
b 32
b 26
b 31
b 29
b 27
b 35
b 21
b 25
c 17
c 21
c 20
c 19
Notice it is not necessary to include the Observation column because in S+, that column is redundant: every individual line in an S+ data file must be an individual observation (this is true even for within-subjects designs; this differs from Systat where, in such designs, individual lines are individual subjects).
Once you have entered the data, save the file as ex1.txt in your home directory. Now enter S+ by typing Splus as previously mentioned. To convert the ASCII text file into an S+ data file, use the command read.table as follows:
> ex1 _ read.table("ex1.txt",header=T)
The first term ex1 indicates the name of the S+ data file you are creating. The first argument of read.table is the name of the ASCII text file you are converting (in this case, ex1.txt). The second argument (header=T) tells S+ to convert the first row of the text file into column headings.
You can also add column headings during the conversion process. Imagine if your ex1.txt text file did not have the first row (i.e., no row that contains "dosage" and "alertness"). Your conversion command in this case would be:
> ex1 _ read.table("ex1.txt",col.names=c("Dosage","Alertness"))
The second argument tells S+ to add the appropriate column headings to the data file.
2. Entering data files within S+. Alternatively, you can enter data directly into S+ without the hassle of converting text files. This, however, is recommended only for small data sets. The first thing you must do is create the constituent lists that will comprise the final data file. In this case, the two lists are Dosage and Alertness . To create these two lists, you use the scan command:
> Dosage _ scan(what="")
1: a a a a a a b b b b b b b b c c c c
19:
> Alertness _ scan()
1: 30 38 35 41 27 24 32 26 31 29 27 35 21 25 17 21 20 19
19:
>
Arguments to scan indicate what type of symbol S+ expects the list to be composed of. For Dosage , these are characters (e.g., "a", "b" and "c") so you need the what argument to indicate this. The default symbol type for scan is a numeral, so for Alertness , using the what argument is unnecessary.
To combine these two lists into a data file, you must use the data.frame command as follows:
> ex1 _ data.frame(Dosage,Alertness)
The command takes as arguments the names of the lists that will comprise the data file.
When the > prompt appears again, S+ has succeeded in creating the data file (by whichever method you have chosen to use). You can look to see what data files are in your working data directory (usually stored under .Data in your home directory) with the command objects. To view the contents of data files, simply type the name of the file. These two commands are illustrated in the following:
> objects()
[1] ".Last.value" "ex1"
> ex1
Dosage Alertness
1 a 30
2 a 38
3 a 35
4 a 41
5 a 27
6 a 24
7 b 32
8 b 26
9 b 31
10 b 29
11 b 27
12 b 35
13 b 21
14 b 25
15 c 17
16 c 21
17 c 20
18 c 19
>
Look over the data and make sure it has been entered properly. Now you are ready to run your ANOVA.
The ANOVA command is aov:
> aov.ex1 _ aov(Alertness~Dosage,ex1)
It is important to note the order of the arguments. The first argument is always the dependent variable (Alertness ). It is followed by the tilde symbol (~) and the independent variable(s). The final argument for aov is the name of the data file that is being analyzed. aov.ex1 is the name of the file you want the analysis stored under. This general format will hold true for all ANOVAs you will conduct.
The results of the ANOVA can be seen with the summary command:
> summary(aov.ex1)
Df Sum of Sq Mean Sq F Value Pr(F)
Dosage 2 426.25 213.125 8.78866 0.002977278
Residuals 15 363.75 24.250
>
Example 2. Two-Way Between-Subjects ANOVA
Appendix II contains data from an experiment in which alertness level of male and female subjects was measured after they had been given one of two possible dosages of a drug. Thus, this is a 2X2 design with the factors being Gender and Dosage . Create a data file ex2 containing this data using one of the two methods prescribed above.
Run the analysis:
> aov.ex2 _ aov(Alertness~Gender*Dosage,ex2)
Notice that there are two independent variables in this example, separated by an asterisk *. The asterisk indicates to S+ that the interaction between the two factors is interesting and should be analyzed. If interactions are not important, replace the asterisk with a plus sign (+).
The summary should look like the following:
> summary (aov.ex2)
Df Sum of Sq Mean Sq F Value Pr(F)
Gender 1 76.5625 76.5625 2.951807 0.1114507
Dosage 1 5.0625 5.0625 0.195181 0.6664956
Gender:Dosage 1 0.0625 0.0625 0.002410 0.9616567
Residuals 12 311.2500 25.9375
>
Although this example only contains 2 independent factors, the general procedure will work for any between-subjects design.
Example 3. One-Way Within-Subjects ANOVA
Five subjects are asked to memorize a list of words. The words on this list are of three types: positive words, negative words and neutral words. Their recall data by word type is displayed in Appendix III. Note that there is a single factor (Valence ) with three levels (negative, neutral and positive). In addition, there is also a random factor Subject . Create a data file ex3 that contains this data. Again it is important that each observation appears on an individual line!
Run the analysis:
> aov.ex3 _ aov(Recall~Valence+Error(Subject/Valence),ex3)
Because Valence is crossed with the random factor Subject (i.e., every subject sees all three types of words), you must specify the error term for Valence , which in this case is Subject by Valence . Do this by adding the term Error(Subject/Valence) to the factor Valence , as shown above.
The summary will look like:
> summary(aov.ex3)
Error: Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Residuals 4 105.0667 26.26667
Error: Valence %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Valence 2 2029.733 1014.867 189.1056 1.841029e-07
Residuals 8 42.933 5.367
>
The analysis of between-subjects factors will appear first (there are none in this case), followed by the within-subjects factors. Note that the p value for Valence is displayed in exponential notation; this occurs when the p value is extremely low, as it is in this case (approximately .00000018).
Example 4. Two-Way Within-Subjects ANOVA
Appendix IV contains the data from an experiment where five subjects were tested on their recall of words of differing valences. There were two different memory tasks: free or cued recall. Thus, there were 2 independent factors: Valence (3 levels) and Task (2 levels). Again, Subject serves as a random factor. Enter the data into a file entitled ex4 and run the following analysis:
> aov.ex4 _ aov(Recall~(Task*Valence)+Error(Subject/(Task*
Valence)),ex4)
In this example, Subject is crossed with both Task and Valence , so you must specify three different error terms: one for Task , one for Valence and one for the interaction between the two. Fortunately, S+ is smart enough to divide up the within-subjects error term properly as long as you specify it in your command. Your summary should look like:
> summary(aov.ex4)
Error: Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Residuals 4 349.1333 87.28333
Error: Task %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Task 1 30.00000 30.00000 7.346939 0.0535083
Residuals 4 16.33333 4.08333
Error: Valence %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Valence 2 9.80000 4.900000 1.459057 0.2882501
Residuals 8 26.86667 3.358333
Error: Task:Valence %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Task:Valence 2 1.40000 0.700000 0.2906574 0.7553437
Residuals 8 19.26667 2.408333
>
Example 5. Between/Within (Mixed) Design
Now it's time to get serious. Appendix V contains the data of an experiment with 18 subjects, 9 males and 9 females. Each subject is given one of three possible dosages of a drug. All subjects are then tested on recall of three types of words (positive, negative and neutral) using two types of memory tasks (cued and free recall). There are thus 2 between-subjects variables: Gender (2 levels) and Dosage (3 levels); and 2 within-subjects variables: Task (2 levels) and Valence (3 levels). Type the data into a file called ex5 and run the following analysis:
> aov.ex5 _ aov(Recall~(Task*Valence*Gender*Dosage)+Error(Subject/
(Task*Valence))+(Gender*Dosage),ex5)
Notice that you must segregate between- and within-subjects variables in your command. In the above example, I have put the within-subjects factors first with the within-subjects error term, followed by the between-subjects factors. Your summary should be:
> summary(aov.ex5)
Error: Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Gender 1 542.259 542.2593 5.685273 0.0344865
Dosage 2 694.907 347.4537 3.642850 0.0580330
Gender:Dosage 2 70.796 35.3981 0.371129 0.6976042
Residuals 12 1144.556 95.3796
Error: Task %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Task 1 96.33333 96.33333 39.86207 0.0000387
Task:Gender 1 1.33333 1.33333 0.55172 0.4719052
Task:Dosage 2 8.16667 4.08333 1.68966 0.2256661
Task:Gender:Dosage 2 3.16667 1.58333 0.65517 0.5369720
Residuals 12 29.00000 2.41667
Error: Valence %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Valence 2 14.68519 7.342593 2.998110 0.0688235
Valence:Gender 2 3.90741 1.953704 0.797732 0.4619330
Valence:Dosage 4 20.25926 5.064815 2.068053 0.1166313
Valence:Gender:Dosage 4 1.03704 0.259259 0.105860 0.9793491
Residuals 24 58.77778 2.449074
Error: Task:Valence %in% Subject
Df Sum of Sq Mean Sq F Value Pr(F)
Task:Valence 2 5.38889 2.694444 1.319728 0.2859109
Task:Valence:Gender 2 2.16667 1.083333 0.530612 0.5949870
Task:Valence:Dosage 4 2.77778 0.694444 0.340136 0.8481544
Task:Valence:Gender:Dosage 4 2.66667 0.666667 0.326531 0.8573727
Residuals 24 49.00000 2.041667
>
In all of the above analyses, factors were assumed to be categorical. Categorical variables are differentiated from continuous ones in S+ by their being coded as characters (i.e., "Jim" or "A" or "Pos"). Numerals are reserved for continuous variables. This differs from Systat where you can use either.
To quit out of S+, type q().
Appendix I. Data for Example 1.
Observation Dosage Alertness
1 a 30
2 a 38
3 a 35
4 a 41
5 a 27
6 a 24
7 b 32
8 b 26
9 b 31
10 b 29
11 b 27
12 b 35
13 b 21
14 b 25
15 c 17
16 c 21
17 c 20
18 c 19
Appendix II. Data for Example 2.
Observation Gender Dosage Alertness
1 m a 8
2 m a 12
3 m a 13
4 m a 12
5 m b 6
6 m b 7
7 m b 23
8 m b 14
9 f a 15
10 f a 12
11 f a 22
12 f a 14
13 f b 15
14 f b 12
15 f b 18
16 f b 22
Appendix III. Data for Example 3.
Observation Subject Valence Recall
1 Jim Neg 32
2 Jim Neu 15
3 Jim Pos 45
4 Victor Neg 30
5 Victor Neu 13
6 Victor Pos 40
7 Faye Neg 26
8 Faye Neu 12
9 Faye Pos 42
10 Ron Neg 22
11 Ron Neu 10
12 Ron Pos 38
13 Jason Neg 29
14 Jason Neu 8
15 Jason Pos 35
Appendix IV. Data for Example 4.
Observation Subject Task Valence Recall
1 Jim Free Neg 8
2 Jim Free Neu 9
3 Jim Free Pos 5
4 Jim Cued Neg 7
5 Jim Cued Neu 9
6 Jim Cued Pos 10
7 Victor Free Neg 12
8 Victor Free Neu 13
9 Victor Free Pos 14
10 Victor Cued Neg 16
11 Victor Cued Neu 13
12 Victor Cued Pos 14
13 Faye Free Neg 13
14 Faye Free Neu 13
15 Faye Free Pos 12
16 Faye Cued Neg 15
17 Faye Cued Neu 16
18 Faye Cued Pos 14
19 Ron Free Neg 12
20 Ron Free Neu 14
21 Ron Free Pos 15
22 Ron Cued Neg 17
23 Ron Cued Neu 18
24 Ron Cued Pos 20
25 Jason Free Neg 6
26 Jason Free Neu 7
27 Jason Free Pos 9
28 Jason Cued Neg 4
29 Jason Cued Neu 9
30 Jason Cued Pos 10
Appendix V. Data for Example 5.
Obs Subject Gender Dosage Task Valence Recall
1 A M A F Neg 8
2 A M A F Neu 9
3 A M A F Pos 5
4 A M A C Neg 7
5 A M A C Neu 9
6 A M A C Pos 10
7 B M A F Neg 12
8 B M A F Neu 13
9 B M A F Pos 14
10 B M A C Neg 16
11 B M A C Neu 13
12 B M A C Pos 14
13 C M A F Neg 13
14 C M A F Neu 13
15 C M A F Pos 12
16 C M A C Neg 15
17 C M A C Neu 16
18 C M A C Pos 14
19 D M B F Neg 12
20 D M B F Neu 14
21 D M B F Pos 15
22 D M B C Neg 17
23 D M B C Neu 18
24 D M B C Pos 20
25 E M B F Neg 6
26 E M B F Neu 7
27 E M B F Pos 9
28 E M B C Neg 4
29 E M B C Neu 9
30 E M B C Pos 10
31 F M B F Neg 7
32 F M B F Neu 5
33 F M B F Pos 9
34 F M B C Neg 9
35 F M B C Neu 8
36 F M B C Pos 7
37 G M C F Neg 23
38 G M C F Neu 22
39 G M C F Pos 23
40 G M C C Neg 25
41 G M C C Neu 23
42 G M C C Pos 22
43 H M C F Neg 14
44 H M C F Neu 14
45 H M C F Pos 13
46 H M C C Neg 15
47 H M C C Neu 17
48 H M C C Pos 17
49 I M C F Neg 15
50 I M C F Neu 16
51 I M C F Pos 16
52 I M C C Neg 14
53 I M C C Neu 17
54 I M C C Pos 18
55 J F A F Neg 12
56 J F A F Neu 11
57 J F A F Pos 13
58 J F A C Neg 15
59 J F A C Neu 14
60 J F A C Pos 15
61 K F A F Neg 22
62 K F A F Neu 20
63 K F A F Pos 23
64 K F A C Neg 25
65 K F A C Neu 22
66 K F A C Pos 21
67 L F A F Neg 14
68 L F A F Neu 15
69 L F A F Pos 13
70 L F A C Neg 12
71 L F A C Neu 16
72 L F A C Pos 15
73 M F B F Neg 15
74 M F B F Neu 14
75 M F B F Pos 16
76 M F B C Neg 15
77 M F B C Neu 18
78 M F B C Pos 21
79 N F B F Neg 12
80 N F B F Neu 10
81 N F B F Pos 14
82 N F B C Neg 15
83 N F B C Neu 14
84 N F B C Pos 17
85 O F B F Neg 18
86 O F B F Neu 17
87 O F B F Pos 19
88 O F B C Neg 22
89 O F B C Neu 22
90 O F B C Pos 21
91 P F C F Neg 22
92 P F C F Neu 22
93 P F C F Pos 24
94 P F C C Neg 23
95 P F C C Neu 25
96 P F C C Pos 25
97 Q F C F Neg 18
98 Q F C F Neu 17
99 Q F C F Pos 18
100 Q F C C Neg 17
101 Q F C C Neu 19
102 Q F C C Pos 19
103 R F C F Neg 19
104 R F C F Neu 17
105 R F C F Pos 19
106 R F C C Neg 22
107 R F C C Neu 21
108 R F C C Pos 20