Introduction


Introduction | Getting help | Common Commands | Types of objects | Input and output of data | Programs | Some arithmetic commands | Plotting data| Customizing your session


INTRODUCTION

S is a statistical package developed at AT & T's Bell Laboratories. S-Plus is an extension of this statistical language produced by the StatSci Division of MathSoft in Seattle.

S-plus is a versatile package which can be used for analyzing data using the pre-packaged software, and also has its own programing laguage, which can be used to write your own routines. The comments I have written here apply to S-PLUS version 6. Splus is case sensitive, it matters whether the letters is lower or uppercase.

To start Splus, login in one of the Linux computers of the department, open a terminal window (Konsole) by clicking in

Then, you have a shell prompt. Then you type "Splus" and hit return. Then, you will be in Splus and you will have the sign >. If a command extends over one line of input, the prompt changes to the plus sign: +. To exit Splus, type q(). Next, there is an example of a session.

agate% Splus 
S-PLUS : Copyright (c) 1988, 2000 MathSoft, Inc.
S : Copyright Lucent Technologies, Inc.
Version 6.0 Release 1 for Linux 2.2.12 : 2000
Working data will be in /home/arcones/MySwork
> a_c(1,2,3) 
> a
[1] 1 2 3
> b_c(scan())
1: 12 13 15
4: 
> b
[1] 12 13 15
> c1_a*b
> c1
[1] 12 26 45
y <- c("a", "b", "d")
> q()


GETTING HELP

In general you have online help using
>  help("filename") 
or
> ?filename
You can get a separate window with the help information doing:
>  help("filename",window=T) 
You can general information on Splus by doing:
>  help.start() 
You get all Functions and Datasets.


COMMON COMMANDS

>ls()
>objects()
List objects in your working directory. This can be used with pattern matching using unix regular expressions.

>rm (x)
Remove object x. This can also take pattern arguments in the form of regular expressions. Unless you remove an object, Splus will keep the object from one sesion to another.

>length(x)
Returns the length (number of elements) of the object x.

>dim(x)
Returns the dimensions of an object. In the case of matrices, the first element is the number of rows in the matrix and the second element is the number of columns.

>nrow(x)
Return the number of rows in a matrix.

>ncol(x)
Return the number of columns in a matrix.

> history()
Allows to repeat a previous used command.

> again()
Repeat the last used command.

> options()
See the list of options.

>!
Enter a Linux command. For example,

>!lpr
Enter the Linux command lpr.


TYPES OF OBJECTS

There are different types of objects in Splus, such as: vectors, factors, matrices, data frames and lists.

A vector is a sequence of real numbers or categorical variables. For example:

> a2_c(12,14,16)
> a2
[1] 12 14 16
>  a3_c("TX","NY","TN","CA")
> a3
[1] "TX" "NY" "TN" "CA"   
It is possible to name a vector
> heights <- c(160,140,155,170)
> names(heights) <- c("Ned","Jill","Pat","Ronnie")
> heights
 Ned Jill Pat Ronnie
 160  140 155    170
Alternatively, you can do
> people<- c("Ned","Jill","Pat","Ronnie")
> names(heights) <- people
> heights
 Ned Jill Pat Ronnie
 160  140 155    170  
To create a matrix, you can do:
> a2_c(42, 66, 40, 62, 42, 71, 43, 79, 42, 67)
> kids_matrix(a2,ncol=2,byrow=T)
> kids
     [,1] [,2]
[1,]   42   66
[2,]   40   62
[3,]   42   71
[4,]   43   79
[5,]   42   67 
To name the rows and columns of a matrix, you can do as follow:
> dimnames(kids)_list(c("Tom","Fred","Sue","Donna","Lee"), 
+ c("Height","Weight"))
> kids
      Height Weight 
  Tom     42     66
 Fred     40     62
  Sue     42     71
Donna     43     79
  Lee     42     67 
If you do not want to name the columns, you can use NULL:
> dimnames(kids)_list(c(NULL),c("Height","Weight"))
> kids
     Height Weight 
[1,]     42     66
[2,]     40     62
[3,]     42     71
[4,]     43     79
[5,]     42     67


INPUT OF DATA

To input data in one variable, we can do a_c(1,2,3) or a<-c(1,2,3) or c(1,2,3)->a. In any case, we get a 1*3 vector. In this case a is the name of the variable, could be anything else. The part c( ) is standard. c( ) means combine. To see the variable just type the name of the variable. The [1] above means the first row. You also can input data using the scan functions. It will keep getting data until you leave one blank line. You can read data from a file that you have in the unix system. Suppose that you have a file in Unix called data with 12 13 14 15. To read this file in the object b:

>  b_c(scan("data"))
> b
[1] 12 13 14 15 
You also can input data by doing
> a_c(1,2,3) 
> a
[1] 1 2 3
> b_c(scan())
1: 12 13 15
4: 
Doing this you can create variables which will stay in your directory from one session to another.

A simple way to output Splus objects to an ASCII file is to use the function "sink()". For example, the following commands

>sink("outfile") 
>mydata 
>grades
>sink()
forward output that usually appear on screen to the file "outfile". The last "sink()" stop the output to file "outfile", and resumes the output to screen. To change a coordinate of a vector, you can do "a[i]<- k" i is the number of coordinate, k is the number to appear in that coordinate. For, example,
> a
[1] 42 52 48 58  4  5  4  3
> a[3]<- -2
> a
[1]  42  52  -2  58   4   5   4   3
> a[3]_-4
> a
[1]  42  52  -4  58   4   5   4   3


PROGRAMS

Splus is often run interactively so that each command/function is interpreted and executed by computer immediately once issured. However, you can also execute a sequence of functions as a batch by putting them in a file, say "myprogram", and
>source("myprogram")
For example, the program finds the 95 % confidence interval of a binomial distribution
x_115
n_300
p_x/n
z_qnorm(0.975)
c1[1]_p-z*sqrt(p*(1-p)/n)
c1[2]_p+z*sqrt(p*(1-p)/n)
dfr1_2*(n+1-x)
dfr2_2*x
q1_qf(.975,df1=dfr1,df2=dfr2)
c2[1]_(x/(x+((n+1-x)*q1)))
dfr1_2*(x+1)
dfr2_2*(n-x)
q2_qf(.975,df1=dfr1,df2=dfr2)
c2[2]_((x+1)*q2)/(n-x+((x+1)*q2)) 
If the program is large, one may wish to put the execution in a batch mode from outside Splus. That is, at Unix prompt, do
Splus BATCH myprogram outfile 
where "outfile" is the name of the file where the output of the program goes.

The following commands create a program to find (n choose k)

> comb_function(n,k)
+ {if (k==0)
+ {1}
+ else{(prod(c(n-k+1:k)))/(prod(c(1:k)))}
+ }
> comb(10,2)
[1] 45       


SOME ARITHMETIC COMMANDS

These are several functions of the type you can find in a calculator: cos, sin, tan, cosh, sinh, tanh, acos, asin, atan, acosh, asinh, atanh, abs, exp, gamma, lgamma, log, log10, log(x, base=), sqrt ,abs, pmax, pmin, max, min, trunc, ceiling, floor.

a+b (a-b, a*b, a/b) is a vector with the sum (or whatever) of the coordinates of a and b. To get this in a new vector, we can do d_a+b or d<-a+b


PLOTTING DATA

You can open a graphics window with the (Splus) command:
> motif()
or
> trellis.device()
Yo need to open the graphics window before asking for the graph of something. Some basic graphics commands are: barplot, boxplot, contour, coplot, dotchart, faces, hist, pairs, persp, pie, ,plclust, plot, qqnorm, qqplot, tsplothist(x) plot(x,y), usa(). The command
> usa()
gives the USA map. To quit graphs you do
> dev.off()  


CUSTOMIZING YOUR SESSION

You can customize your Splus session by changing the .First file:
> .First <-
+    function()
+ {
+ options(editor="/opt/local/bin/emacs")
+ length(50)
+ digits=17
+ }    


Comments to: Miguel A. Arcones

Go to main homepage: