Attachment 'session_0.R'

Download

   1 #### R resource file for Session 0
   2 #### Regression Methods Mini-Course
   3 #### HLP Lab, University of Rochester
   4 #### Author:  Austin F. Frank
   5 #### May 27, 2008
   6 
   7 ## The goal of this session is to make sure all participants are
   8 ## comfortable entering commands and interpreting results in the R
   9 ## environment.
  10 
  11 ## The problems and data sets used in this session have been selected
  12 ## to be useful for demonstrating R concepts, not for their utility in
  13 ## teaching statistical concepts.
  14 
  15 ######## Interacting with R and R files
  16 
  17 ### Lesson 1:  Characters that follow a # will never cause R to do
  18 ### anything.  # starts a "comment", which is a way of annotating
  19 ### source code without changing the way the code runs.
  20 
  21 ### Lesson 2:  R commands can be entered at the command line.
  22 
  23 ### Lesson 3:  R commands can be sent from a file to the command line.
  24 2 + 2
  25 
  26 ### Lesson 4:  Variables can be defined using a right arrow or a left
  27 ### arrow.
  28 a <- 4
  29 5 -> b
  30 a <- b
  31 
  32 ### lesson 5:  Functions are called using the name of the function and
  33 ### a list of arguments enclosed in parentheses
  34 citation()                              # no arguments
  35 
  36 citation(package='lme4')                # one optional argument
  37 citation(pack='lme4')                   # same thing
  38 
  39 length()                                # fails, missing required
  40                                         # argument
  41 length(letters)                         
  42 
  43 ### lesson 6:  Extra functionality for R comes in "packages" (also
  44 ### called "libraries").  Packages need to be installed and loaded
  45 ### before they can be used.
  46 
  47 ## install a package
  48 install.packages("ISwR", dependencies=TRUE)
  49 
  50 ## load a library
  51 library(ISwR)
  52 
  53 ## update packages to the newest versions
  54 ## update.packages()                       # don't actually run this now
  55 
  56 ### lesson 7:  An R session keeps track of two different environments:
  57 ### the current directory, and the R workspace.
  58 ## find out what directory we're working in
  59 getwd()
  60 
  61 ## look at the contents of the current directory
  62 dir()
  63 
  64 ## change the working directory
  65 ## setwd("~/")                             # don't actually run this now
  66 
  67 ## look at the R workspace
  68 ls()                                    # same as objects()
  69 
  70 ## remove object from the workspace
  71 rm(a)                                   # same as remove(a)
  72 ls()
  73 
  74 ## useful but VERY POWERFUL VERY DANGEROUS command for completely
  75 ## clearing the workspace
  76 rm(list=ls(all = TRUE))                 # USE WITH CAUTION
  77 ls()
  78 
  79 ### Lesson 8:  R objects and the R workspace can be saved and reloaded
  80 ### later
  81 a <- 4
  82 b <- 5
  83 a_z <- letters
  84 ls()
  85 
  86 save(a, file="a.RData")
  87 save.image(file = "tmp.RData")
  88 
  89 rm(list=ls(all=TRUE))
  90 ls()
  91 load("a.RData")
  92 ls()
  93 a
  94 load("tmp.RData")
  95 ls()
  96 a_z
  97 
  98 ######## Getting help
  99 ### Lesson 9:  you can always get help, even if you don't know which
 100 ### command you want help with
 101 ## If you know the name of the command
 102 help(ls)                                # same as ?ls
 103 help("?")                               # same as ?"?"
 104 
 105 ## If you remember part of the name
 106 apropos("dir")
 107 
 108 ## If you know a relevant word
 109 help.search("directory")
 110 
 111 ## If you know a relevant word and which package you're interested in
 112 help.search("mcmc", package = "lme4")
 113 
 114 ## If you want general help about a particular package
 115 help(package="lme4")
 116 
 117 ## If you just want to know which data sets are available from a
 118 ## particular package
 119 data(package="lme4")
 120 
 121 ## If you want to search R mailing lists in addition to the
 122 ## documentation
 123 RSiteSearch("lmer p values")
 124 
 125 ######## Loading data
 126 ## Lesson 10:  use functions that start with the word "read" to load
 127 ## data that wasn't saved in an R-specific format
 128 apropos("read")
 129 
 130 ## read.csv() is probably the most commonly used
 131 help("read.csv")
 132 
 133 ## read.delim() is good for tab-delimited files.
 134 help("read.delim")
 135 
 136 ## If you have a file where fields are delimited by something other
 137 ## than a comma or a tab, use the sep argument to read.delim()
 138 
 139 ## read.csv() and read.delim() are just variants of the more general
 140 ## function, read.table().  For an even more general way of loading
 141 ## data, look at scan().
 142 
 143 ## For more read functions, use the foreign package
 144 help(package="foreign")
 145 
 146 ######## Data structures
 147 ### For all data structures, we want to know how to access a single
 148 ### element and specific subsets of elements.  Where appropriate, we
 149 ### want to access entire rows and columns.  We want to access data
 150 ### structures by numbered dimensions, and by name when possible.
 151 ### We also want general methods to explore unfamiliar data structures
 152 ### or data sets.
 153 
 154 ### Lesson 11:  use str() and class() to explore data structures
 155 ## class() returns the kind of thing that this is a list of
 156 class(letters)
 157 
 158 ## str() shows the elements of the vector in order and gives the range
 159 ## of the indeces
 160 str(letters)
 161 
 162 ###### Vectors
 163 ### Lesson 12:  vectors are ordered 1-dimensional lists of numbers
 164 ## use c() to combine elements into a vector
 165 i <- c(1, 2, 3)                         # same as c(1:3)
 166 m <- c(1, "b", 3, "d")
 167 
 168 ### Lesson 13:  access vectors with [[ and [
 169 ## get the 1st through third elements of the vector
 170 letters[1:3]
 171 ## second and third elements 
 172 letters[c(2,3)]                         
 173 ## get just the third element
 174 letters[[3]]                
 175 ## exclude the first and second elements
 176 letters[c(-1, -2)]
 177 
 178 ###### Arrays
 179 ### Arrays are vectors with named dimensions
 180 i
 181 names(i) <- c("first", "second", "third")
 182 i.array <- as.array(i)
 183 class(i.array)
 184 str(i.array)
 185 ### use dimnames to see names of dimensions
 186 dimnames(i.array)
 187 ### access the array by dimension name
 188 i[c("first", "second")]
 189 
 190 ###### Matrices
 191 ### Matrices work just like arrays
 192 
 193 ###### Data frames
 194 ### This will be the interactive part, I guess
 195 
 196 ###### S4 objects
 197 ### used to store lots of results from complex methods
 198 data(sleepstudy)
 199 
 200 m <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
 201 m
 202 str(m)                                  # ahhhhhh!
 203 slotNames(m)
 204 m@ranef                                 
 205 ranef(m)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-04-22 12:55:33, 200.0 KB) [[attachment:R-Refcard.pdf]]
  • [get | view] (2021-04-22 12:55:33, 7.2 KB) [[attachment:cake.tab]]
  • [get | view] (2021-04-22 12:55:33, 5.8 KB) [[attachment:session_0.R]]
  • [get | view] (2021-04-22 12:55:33, 3.9 KB) [[attachment:sleepstudy.csv]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

MoinMoin Appliance - Powered by TurnKey Linux