#### R resource file for Session 0
#### Regression Methods Mini-Course
#### HLP Lab, University of Rochester
#### Author:  Austin F. Frank
#### May 27, 2008

## The goal of this session is to make sure all participants are
## comfortable entering commands and interpreting results in the R
## environment.

## The problems and data sets used in this session have been selected
## to be useful for demonstrating R concepts, not for their utility in
## teaching statistical concepts.

######## Interacting with R and R files

### Lesson 1:  Characters that follow a # will never cause R to do
### anything.  # starts a "comment", which is a way of annotating
### source code without changing the way the code runs.

### Lesson 2:  R commands can be entered at the command line.

### Lesson 3:  R commands can be sent from a file to the command line.
2 + 2

### Lesson 4:  Variables can be defined using a right arrow or a left
### arrow.
a <- 4
5 -> b
a <- b

### lesson 5:  Functions are called using the name of the function and
### a list of arguments enclosed in parentheses
citation()                              # no arguments

citation(package='lme4')                # one optional argument
citation(pack='lme4')                   # same thing

length()                                # fails, missing required
                                        # argument
length(letters)                         

### lesson 6:  Extra functionality for R comes in "packages" (also
### called "libraries").  Packages need to be installed and loaded
### before they can be used.

## install a package
install.packages("ISwR", dependencies=TRUE)

## load a library
library(ISwR)

## update packages to the newest versions
## update.packages()                       # don't actually run this now

### lesson 7:  An R session keeps track of two different environments:
### the current directory, and the R workspace.
## find out what directory we're working in
getwd()

## look at the contents of the current directory
dir()

## change the working directory
## setwd("~/")                             # don't actually run this now

## look at the R workspace
ls()                                    # same as objects()

## remove object from the workspace
rm(a)                                   # same as remove(a)
ls()

## useful but VERY POWERFUL VERY DANGEROUS command for completely
## clearing the workspace
rm(list=ls(all = TRUE))                 # USE WITH CAUTION
ls()

### Lesson 8:  R objects and the R workspace can be saved and reloaded
### later
a <- 4
b <- 5
a_z <- letters
ls()

save(a, file="a.RData")
save.image(file = "tmp.RData")

rm(list=ls(all=TRUE))
ls()
load("a.RData")
ls()
a
load("tmp.RData")
ls()
a_z

######## Getting help
### Lesson 9:  you can always get help, even if you don't know which
### command you want help with
## If you know the name of the command
help(ls)                                # same as ?ls
help("?")                               # same as ?"?"

## If you remember part of the name
apropos("dir")

## If you know a relevant word
help.search("directory")

## If you know a relevant word and which package you're interested in
help.search("mcmc", package = "lme4")

## If you want general help about a particular package
help(package="lme4")

## If you just want to know which data sets are available from a
## particular package
data(package="lme4")

## If you want to search R mailing lists in addition to the
## documentation
RSiteSearch("lmer p values")

######## Loading data
## Lesson 10:  use functions that start with the word "read" to load
## data that wasn't saved in an R-specific format
apropos("read")

## read.csv() is probably the most commonly used
help("read.csv")

## read.delim() is good for tab-delimited files.
help("read.delim")

## If you have a file where fields are delimited by something other
## than a comma or a tab, use the sep argument to read.delim()

## read.csv() and read.delim() are just variants of the more general
## function, read.table().  For an even more general way of loading
## data, look at scan().

## For more read functions, use the foreign package
help(package="foreign")

######## Data structures
### For all data structures, we want to know how to access a single
### element and specific subsets of elements.  Where appropriate, we
### want to access entire rows and columns.  We want to access data
### structures by numbered dimensions, and by name when possible.
### We also want general methods to explore unfamiliar data structures
### or data sets.

### Lesson 11:  use str() and class() to explore data structures
## class() returns the kind of thing that this is a list of
class(letters)

## str() shows the elements of the vector in order and gives the range
## of the indeces
str(letters)

###### Vectors
### Lesson 12:  vectors are ordered 1-dimensional lists of numbers
## use c() to combine elements into a vector
i <- c(1, 2, 3)                         # same as c(1:3)
m <- c(1, "b", 3, "d")

### Lesson 13:  access vectors with [[ and [
## get the 1st through third elements of the vector
letters[1:3]
## second and third elements 
letters[c(2,3)]                         
## get just the third element
letters[[3]]                
## exclude the first and second elements
letters[c(-1, -2)]

###### Arrays
### Arrays are vectors with named dimensions
i
names(i) <- c("first", "second", "third")
i.array <- as.array(i)
class(i.array)
str(i.array)
### use dimnames to see names of dimensions
dimnames(i.array)
### access the array by dimension name
i[c("first", "second")]

###### Matrices
### Matrices work just like arrays

###### Data frames
### This will be the interactive part, I guess

###### S4 objects
### used to store lots of results from complex methods
data(sleepstudy)

m <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
m
str(m)                                  # ahhhhhh!
slotNames(m)
m@ranef                                 
ranef(m)
