R workshop: handout #3

Mike Hammond
U. of Arizona
  1. Plotting
    1. plot() — generic plotting function; lots and lots of options.
      plot(age ~ gender) 
      plot(age,weight) 
      plot(age ~ cut(weight,3))
      
    2. barplot() — makes a barplot, without whiskers, lots and lots of options.
      barplot(tapply(age,gender,mean),xlab="Gender", 
         col=c('sienna','violet'),names.arg=c('female','male'))
      
    3. colors() — lists all available named colors.
    4. hist() — makes a histogram from a list of numbers
    5. interaction.plot() — creates an interaction plot for two factors.
      interaction.plot(gender,major,age)
      
  2. Anova
    1. aov() — creates an anova object which can be examined for the usual information with summary(). The tricky part is the anova formula. Examples:
      • aov(x ~ f1 * f2 + Error(sub/f1))x is a vector representing a response, f1 is a factor distinguishing items, f2 is a factor distinguishing subjects, and sub is the subjects factor itself. The * includes interactions.
      • aov(x ~ f1 * f2 + Error(itm/f2)) — here everything is the same except that itm is the items factor.
    2. The example here is in the twosn.txt file. These are fictionalized results for a wellformedness task. There are two factors defined for items: sonority (rise or fall) of the onset cluster, and size (2 or 3) of the onset cluster.
      sn <- read.table('twosn.txt',header=T) 
      summary(sn) 
      attach(sn) 
      tapply(response,list(sonority,size),mean) 
      summary(aov(response ~ sonority * size + 
         Error(subject/(sonority * size)))) 
      summary(aov(response ~ sonority * size + 
         Error(item))) 
      interaction.plot(sonority,size,response) 
      interaction.plot(sonority,size,response,ylim=c(1,7)) 
      with(sn[sn$size=='three',],summary(aov(response ~
         sonority + Error(subject/sonority))))
      detach(sn)
      
  3. Another anova example
    1. Here's another dataset, this time with multiple subject groups.
      tai <- read.table('tai.txt',header=T) 
      summary(tai) 
      attach(tai) 
      tapply(response,list(place,nasal,native),mean)
      summary(aov(response ~ place * nasal * native + 
         Error(subject/(place * nasal)))) 
      summary(aov(response ~ place * nasal * native +
         Error(item/native))))
      detach(tai)
      
  4. Regression
    1. lm() — does a linear regression. One also needs to enter a formula, but it's simpler.
    2. lm(x ~ y + z + ...)x, y, z, etc. are vectors of numbers (not factors).
      attach(g) 
      sumary(lm(age ~ weight))
      
  5. Scripting
    1. A simple function:
      mh <- function (x) {
         paste("Hey, ", x, ", this is R!", sep="")
      }
      
    2. Here's a really useful function that I wrote. Load it into R like this:
      source('getF.R')
      
      You can then invoke it like this:
      s.aov <- aov(response ~ sonority * size + 
         Error(subject/(sonority * size))) 
      getF(s.aov,'sonority',1)
      getF(s.aov,'size',1)
      getF(s.aov,'sonority:size',1)
      i.aov <- aov(response ~ sonority * size + 
         Error(item)) 
      getF(i.aov,'sonority',2)
      getF(i.aov,'size',2)
      getF(i.aov,'sonority:size',2)
      
  6. Linear mixed effects
    1. You need to load an optional package to use the lmer() function: languageR.
    2. Choose package installer from the Packages & Data menu. Then click the Get List button. Choose languageR from the list. Finally, make sure the install dependencies checkbox is checked, and click the Install Selected button.
    3. Here's what you do for the sn data:
      library(languageR)
      sn.lmer <- lmer(response ~ size * sonority + (1 | item) +
         (1 | subject), data=sn)
      sn.p <- pvals.fnc(sn.lmer)
      sn.p$fixed
      
    4. Here's what you do for the tai data:
      tai.lmer <- lmer(response ~ place * nasal * native +
         (1 | item) + (1 | subject), data=tai)
      tai.p <- pvals.fnc(tai.lmer)
      tai.p$fixed