Statlab Workshop on S-PLUS
=10 cellspacing=0 width=100%>Yahoo! is not affiliated with the authors of this page or responsible for its content.
Statlab Workshop on S-PLUS
Statlab Workshop on S-PLUS
Instructor: Marios Panayides
January 31, 2003
1
General Outline
This basic workshop will mainly focus on the S programming language through the S-PLUS soft-
ware package. In particular, it will be outlined as follows:
S-PLUS Software and its Gui capabilities.
The Introduction to the S-PLUS programming commands.
Basic Graphical capabilities and Trellis graphics.
Basic Statistical Implementation of the S-PLUS Language
The attachment of Libraries and Databases.
S-PLUS and other software compatibility.
Workshop Summary.
2
Yes, there is the Gui Tool Bar and Menu
S-PLUS has a Graphical User Interphase with scroll down menus and toolbars for data analysis
and manipulation. The help Demo provided in the workshop will point out the major directions
for learning the click and drop menus. The only gui that will be discussed thoroughly in class is
the Object Explorer.
1
3
Introduction to S-PLUS Object-Oriented language
The majority of operations in S-PLUS result in the creation of objects. These objects are then
the main focus of further analysis The nouns where the language refers to and S-PLUS
programming builds up.
You can access the S-PLUS programming language either using the Script Window or the
Command Window. The Script Window is more structural as it allows you to save your script and
run incremental commands whereas the Command window is interactive and is used for immediate
command execution. Both of them will be shown in this workshop.
The Command window if it is not already opened by default can be accessed using the prompt
icon on the main S-PLUS toolbar. The Script Window is opened like any le through the File
New
or File
Open
if you already have saved it as a script le in the main menu.
3.1
Learning Resources
Before starting the intro to the S-PLUS language we should get familiarized with the help les and
the help menu for it. This is found either under the main menu Help
Available help
Language
Reference or
by typing ?<the command you want to search help for> in the command or script
windows. I suggest the use of the rst option.
In addition, extra help can be found from books and manuals that are available either here in
the Statlab or from the current S-PLUS companys web side at:
http://www.insightful.com/support/documentation.asp.
3.2
Objects
All the commands in S-PLUS are either expressions or assignments. Expressions usually call the
objects and assignments create them. To assign a value to a name, thus creating an object, use the
operator < or or even = (for S-PLUS version 6.1).
CAUTION 1: DO NOT USE THE UNDERSCORE FOR NAMES.
CAUTION 2: S-PLUS IS CASE SENSITIVE.
CAUTION 3: TRY NOT TO USE THE = AS IT MIGHT BE CONFUSING AND CAUSE
PROBLEMS. -WE WILL SEE AN EXAMPLE LATER ON-.
2
The most common objects in the S-PLUS language are: Vectors, Arrays, Lists, Factors, Data
frames, and Functions
We will proceed by listing commands for each object. NOTE: # can be
used before personal comments is the S language.
3.2.1
Vectors
Vectors can either have numeric, character or logical strings. They are assigned using the con-
catenation FUNCTION or otherwise called call FUNCTION c.
x <- c(1,2,3,4,5,6) # Assignment of numeric vectors.
y <- c(2,4,8,16,32,64)
3:10 # the colon operator produces a numeric vector of sequential integers
x+y
# Vector arithmetic is done element by element
x*y
sqrt(x) # basic functions tend to work on each element of a vector
seq(1:4) * rep(3,5) # CAUTION: S-PLUS DOES NOT RECYCLE THE SHORTEST VECTOR
# TO MATCH LENGTHS IN VERSION 6
# A character vector:
n<-c("red","orange","yellow","green","blue","purple")
# Examples of logical vectors:
x < 2
x <= 3 & x>1
x ==3
n == "red"
x[1]
# Vectors can be addressed by the element number
x[1:2]
# A vector of element numbers will return a vector
x[-1]
# A negative element number will return all but the
# elements given
x[x> 4] # A logical vector will return the elements that match up
# the true elements
y [ (x < 3) | (x>5)]
3
names(x)# A vector can have names
names(x) <- n
x["red"]
seq(from=1,to=10,by=.1) # produces regular sequences
rep(3,6) #produces repeated patterns
rep(x,3)
length(x) # returns the number of elements of the vector
sum(y)/length(y)#The mean of the vector x
mean(x) # is also the mean of the vector x
b=seq(4,7) # CAUTION: WHEN USING THE = SIGN INSTEAD OF THE ==
a_seq(1,4) # THE FIRST IS AN ASSIGNING OPERATOR WHEREAS THE ==
a[b==4]
# IS A LOGICAL OPERATOR.
a[b=4]
# NOTICE THE DIFFERENCE
3.2.2
Matrices and Arrays
Matrices and Arrays have similar characteristics to vectors
m <- matrix(y,2,3) # Assignment of a numeric matrix
t(m) # matrix transpose
mm <- matrix(y,3,2,byrow=T) # New assignment
dim(mm)
dim(mm) <- c(1,6)
mm
dim(mm)<-c(3,2)
#An array is an extension of a matrix of length(dim)>=3
a <- array(c(1:8, 11:18, 111:118), dim = c(2,4,3)) # Array
arr <- 1:8
dim(arr) <- c(2,2,2)
m[1,2] #accessing matrices is similar to vectors
m[1,] #you can specify all the row or columns by leaving it blank
#rbind and cbind add rows and columns to matrices
4
rbind(1:2,mm)
cbind(1:3,mm)
cbind(matrix(4:8,2,2),matrix(rep(6,4),2)) # Note: One dimension
rbind(matrix(4:8,2,2),matrix(rep(6,4),2)) # See help on matrix
# Some Arithmetics
m + x # adding a vector to a matrix does element by element addition
# the vector is matched to the matrix going down the columns first
m + m # matrices with the same dimensions can be added
m + m == 2*m # logical matrices can be generated
m + mm # CAUTION: ONLY MATRICES OF THE SAME DIMENSIONS CAN BE ADDED
m*m # * also works element by element (not as you may expect)
m %*% mm # %*% is the matrix multiplication operator
matrix(c(1,3,3,1),2,2) %*% c(2,4) # Inner product ALSO
c(3,1) %*% c(2,2) #Vectors are made into rows or columns as necessary
m <- matrix(c(1,7,2,3),2,2)
solve(m)# "solve" FUNCTION inverts Square matrices
m%*%solve(m)#Verification
3.2.3
Lists
A list is a collection of items of different types and very useful in S-PLUS.
ll<-list(vec1 = x, vec2 = c(3, 49, 54),l.matrx=matrix(c(T, T, F, T),2,2),
state = c("Maine", "New Hampshire", "Massachusetts","Vermont"))
ll[1:3]#The first three elements of ll
ll[1]#The first element, CAUTION: IT IS A LIST
ll[[1]]#The first element, CAUTION: A VECTOR
ll$vec1 # using the $ sign,you recall the element of the list which has
# the name as it appears after the $ sign
ll$vec2[3] # vec2 is a vector
ll$state[ll$l.mat]# the name of an element can be abbreviated
unlist(ll)#The FUNCTION "unlist" converts a list into a vector
5
unlist(ll, use.names=F) # converts into a vector without the names
3.2.4
Factors
A factor is a special kind of a vector that is really useful when a categorical variable is considered.
sex <- rep(c("Male","Female"),c(6,4)) #Produces a character vector
sex.fac <- factor(sex)# changes it to a factor object
sex.fac# notice that the quotes are not printed
print.default(sex.fac) #The underlining vector
levels(sex.fac)# the levels of the vector
sum(sex.fac2 == "Male")# a numeric vector under condition
sex.fac1_factor(sex,levels=c("Male", "Female")) #change the levels
levels(sex.fac)#The first level gets the value 1
3.2.5
Data frames
Data are stored in S-PLUS usually as Data frames.
dat.frm<-data.frame(age=c(12,32,43,55,43,55),sex=c(rep("M",3),rep("F",3)))
dat.frm[1,] # data frames can be addressed as either a matrix
dat.frm$sex # or a list
#Using rbind and cbind for adding observations and columns
rbind(dat.frm,data.frame(age=43,sex="M"))
cbind(dat.frm,data.frame(Novalue=rep("NA",6)))
dat.frm$old <- dat.frm$age > 40#OR new columns can be assigned
CAUTION: UNLESS OTHERWISE SPECIFIED THE data.frame COMMAND CONVERTS
CHARACTER AND LOGICAL COLUMNS TO FACTORS.
#with the I() command vectors retain their original mode (i.e. character)
dat.frm1_data.frame(age=c(12,32,43,55,43,55),sex=I(c(rep("M",3),rep("F",3))))
dat.frm1$sex
dat.frm$sex
#notice the difference
6
3.2.6
Functions
We have used functions before in assigning objects. More generally, functions have input argu-
ments that can either be entered in order in which they occur or be specied by the arguments
name. For example:
seq(1,10,2)#No names, the arguments must be in the right order
seq(by=2, from=1, to=10)#with names, the order doesnt matter
seq(1,10,length=5)#mixing order and names
seq(from=1,by=3,length=12)#arguments not given are set to default values
# A number of other useful basic functions are :table(x)for tabulations
# apply(x,y,..) is used to apply a function to sections of an array
# tapply(x,y,..), lapply(x,...) and sapply(x,..) apply a function to a list
Out of personal experience S-PLUS has a lot of built in functions that can not be learned in one
workshop. Actually, they can not be learned in a lot of workshops. Everyday use of the package
is needed for you to get to know the different functions and the help language menu is your best
guidance.
It is extremely convenient to create your own functions especially for operations repeated fre-
quently. This is done using the command:
fname<-function(<AnyArguments>){<the functions body>}
Example 1:
Range<-function(vec)
{ # the body of the function is enclosed in curly brackets
minvec<-min(vec)
maxvec<-max(vec)
c(minvec,maxvec)#note that the last (unassigned) object
#listed is the output of the function
}
Range(1:20)#We call the function with its n