statistics/R

Is my analysis problematic? A simulation-based example

Posted on Updated on

This post is in reply to a recent question on in ling-R-lang by Meredith Tamminga. Meredith was wondering whether an analysis she had in mind for her project was circular, causing the pattern of results predicted by the hypothesis that she was interested in testing. I felt her question (described below in more detail) was an interesting example that might best be answered with some simulations. Reasoning through an analysis can, of course, help a lot in understanding (or better, as in Meredith’s case, anticipating) problems with the interpretation of the results. Not all too infrequently, however, I find that intuition fails or isn’t sufficiently conclusive. In those cases, simulations can be a powerful tool in understanding your analysis. So, I decided to give it a go and use this as an example of how one might approach this type of question.

Results of 16 simulated priming experiments with a robust priming effect (see title for the true relative frequency of each variant in the population).
Figure 1: Results of 16 simulated priming experiments with a robust priming effect (see title for the true relative frequency of each variant in the population). For explanation see text below.

Read the rest of this entry »

Going full Bayesian with mixed effects regression models

Posted on Updated on

Thanks to some recently developed tools, it’s becoming very convenient to do full Bayesian inference for generalized linear mixed-effects models. First, Andrew Gelman et al. have developed Stan, a general-purpose sampler (like BUGS/JAGS) with a nice R interface which samples from models with correlated parameters much more efficiently than BUGS/JAGS. Second, Richard McElreath has written glmer2stan, an R package that essentially provides a drop-in replacement for the lmer command that runs Stan on a generalized linear mixed-effects model specified with a lme4-style model formula.

This means that, in many cases, you simply simply replace calls to (g)lmer() with calls to glmer2stan():

library(glmer2stan)
library(lme4)
lmer.fit <- glmer(accuracy ~ (1|item) + (1+condition|subject) + condition, 
                  data=data, family='binomial')
summary(lmer.fit)
library(glmer2stan)
library(rstan)
stan.fit <- glmer2stan(accuracy ~ (1|item) + (1+condition|subject) + condition, 
                       data=data, family='binomial')
stanmer(stan.fit)

There’s the added benefit that you get a sample from the full, joint posterior distribution of the model parameters

Read on for more about the advantage of this approach and how to use it.

Using plyr to get intimate with your data

Posted on Updated on

I gave a short tutorial [pdf slides] at the LSA summer institute on one of my favorite R packages: plyr (another brilliant Hadley Wickham creation). This package provides a set of very nice and semantically clean functions for exploring and manipulating data. The basic process that these functions carry out is to split data up in some way, do something to each piece, and then combine the results from each piece back together again.

One of the most common tasks that I use this for is to do some analysis to data from each subject in an experiment, and collect the results in a data frame. For instance, to calculate the mean and variance of each subject’s reaction time, you could use:

ddply(my.data, "subject.number", function(d) {
  return(data.frame(mean.RT=mean(d$RT), var.RT=mean(d$RT)))
})

Plyr also provides a whole host of convenience functions. For instance, you could accomplish the same thing using a one-liner:

ddply(my.data, "subject.number", summarise, mean.RT=mean(RT), var.RT=var(RT))

There are lots more examples (as well as more background on functional programming in general and the other use cases for plyr) in the slides [pdf] (knitr source is here, too).

Script for obtaining error intervals from lmer

Posted on

I thought this was worth reposting from ling-R-lang: Sven Hohenstein from the University of Potsdam prepared this script to obtain CIs for, e.g., bar charts from lmer() output. I haven’t tried it yet, but it looks like it will be useful.

Knit from the command line

Posted on

Knitr is a great way to combine document markup (Latex, Markdown, HTML, etc.) with R code for data analysis and visualization. It pulls out the chunks of R code, runs them, and re-inserts the results into the document source (usually a .tex file), which can then be compiled as usual. Normally you would call it from an R console (or use something like RStudio), but what if you want to call it from the command line, like latex?  Here’s a little shell script that I use to automate the knitting of .Rnw files (combining R and Latex): knit.sh.

It call knit() inside R, then runs pdflatex on the resulting file. It is very simple to use (you must of course have the knitr package installed in R):
knit.sh awesomefile.Rnw

This would produce awesomefile.pdf (as well as the intermediate file awesomefile.tex, and the extracted R commands, awesomefile.R). You might even rename the script as knit and put it somewhere on your search path (maybe /usr/local/bin/) to be even more fancy.

HLP lab will be at the LSA 2013 summer institute

Posted on

Come join us in Ann Arbor, MI for the 2013 Summer Institute of the Linguistic Society of America. You can follow the institute on facebook.

Victor Ferreira and I will be organizing a workshop on How the brain accommodates variability in linguistic representations (more on that soonish). I will be teaching a class on regression and mixed models and I am sure a bunch of other folks from the lab will be there, too.

 

 

New R library for multilevel modeling

Posted on

This might be of interest to many of you. MLwiN, a software package for multilevel modeling developed at Bristol that includes functions beyond those present in, e.g., lmer, now has an interface for R (kinda like WinBugs, etc.), so that you can continue to use R while taking advantage of the powerful tools in MLwiN. The package is called R2MLwiN. For more details, see below.

Dear all,
We are pleased to announce a new R package, R2MLwiN (Zhang et al. 2012)
that allows R users access to the functionality within MLwiN directly from
within the R package. This package has been developed as part of the e-STAT
ESRC digital social research programme grant along with the Stat-JR package.
See <http://www.bristol.ac.uk/cmm/software/r2mlwin/> for more details
including examples taken from the book MCMC Estimation in MLwiN.

Feedback gratefully received by either me or Zhengzheng Zhang (Z.Zhang@bristol.ac.uk).

Best wishes,
  Bill Browne.