Second R workshop

Mike Hammond
U. of Arizona
  1. Last time
    1. Basic interaction: q(), help(), etc.
    2. Importing data: data.frame(), fix(), read.table().
    3. Objects: ls(), summary(), rm().
    4. Accessing a dataframe: x$column, x[[n]], x[,n], x[n,], attach(), detach(), search().
    5. Basic stats: mean(), sd(), var().
    6. Manipulating dataframes: tapply(), t(), transform(), aggregate(), as.factor(), is.factor(), cut().
    7. Plotting: plot(), barplot(), hist(), interaction.plot(), etc.
    8. Big stats: aov() and lm().
    9. Scripting: function() {}.
  2. An example
    1. More data: tai.txt
      td <- read.table('tai.txt', header=T)
      summary(td)
      attach(td)
      tapply(response, list(native, nasal), mean)
      plot(response ~ nasal)
      summary(aov(response ~ native * nasal +
         Error(subject/nasal)))
      summary(aov(response ~ native * nasal +
         Error(item/native)))
      
  3. Other Tests
    1. chisq.test() — χ2 test.

      total number of words1,026,595
      words beginning with [mn]62,415
      words beginning with [bd]77,262

      chisq.test(c(62415, 77262))
      
    2. t.test() — Student's t-test.
      t.test(response ~ nasal, data=td,
         subset=td$native=='no')
      
  4. Fixing Problems
    1. bad1.txt
    2. bad2.txt
    3. bad3.txt
  5. Adding and Loading Libraries/Packages
    1. Any of 1402 packages can be downloaded from CRAN directly and then installed on the command line (not recommended).
      R CMD INSTALL /path/to/pkg_version.tar.gz
      
    2. Packages can be downloaded and installed from within R (not recommended).
      install.packages(c('packagename.tar.gz'))
      
    3. Packages can be downloaded and installed from the appropriate menu in the GUI versions of R.
    4. We'll download and install languageR.
    5. library(packagename) — loading packages.
      library(lme4)
      library(languageR)
      
    6. data(name, package='somepackage') — reads a dataset from some package.
    7. Some interesting packages:
      1. corpora — Statistics for corpus linguists.
      2. languageR — Package for Harald Baayen's book on statistics for linguists.
      3. matlab — MATLAB emulation package.
      4. neural — Neural Networks.
      5. neuralnet — Training of neural networks.
      6. Rcmdr — R Commander. A platform-independent basic-statistics GUI (graphical user interface) for R.
      7. sound — Basic functions for dealing with wav files and sound samples.
      8. tm — Text mining package.
      9. xtable — Export tables to LaTeX or HTML.
      10. zipfR — Statistical models for word frequency distributions.
  6. Linear Mixed Effects Models
    1. lmer() — function from the lme4 package to calculate a linear mixed effects model.
    2. pvals.fnc() — function from the languageR package to calculate p-values from a linear mixed effects model.
      summary(lmer(response ~ size * sonority +
         (1|subject) + (1|item), sn))
      pvals.fnc(lmer(response ~ size * sonority +
         (1|subject) + (1|item), sn))$fixed
      
  7. Using R in papers
    1. postscript() — export a plot as postscript.
      postscript('filename.ps')
      plot(age ~ weight, data=g)
      dev.off()
      
    2. Exporting a plot using the GUI menus.
    3. xtable() — converts an R object into something that can be printed as LaTeX or as HTML.
      library(xtable)
      g.lm <- lm(age ~ weight)
      print(xtable(g.lm))
      print(xtable(g.lm), type='HTML')
      
    4. R CMD Sweave filename.Rnw — converts a sweave file into a LaTeX file. For example: swtest.Rnw can be converted to swtest.pdf.
      R CMD Sweave swtest.Rnw
      latex swtest
      latex swtest
      dvipdf swtest
      
  8. More Links
    1. http://www.r-project.org Main R site. R can be downloaded from here, lots and lots of documentation, tutorials, etc.
    2. http://www.statistik.lmu.de/~leisch/Sweave/ Sweave homepage.
    3. http://dingo.sbs.arizona.edu/~psycol/tools/?v=1 R links from Adam & Andy.
    4. http://dingo.sbs.arizona.edu/~hammond/Rwkshp/ URL for workshop materials.