# How do I setup up repeated measures data for analysis with nlme()?

I'm trying to transition to R from using SPSS. In the past, I've setup my data in the wide format for my repeated measures ANOVAs in SPSS. How do I need to setup my data to run nlme()? The data is balanced. It has within-subjects variables of trial type(3 levels) each measured on 8 times, and a between-subjects factor with 2 levels. 2 separate analyses will be run; one with response time as the DV and another with accuracy as the DV. I know the data has to be in a long format but I'm not sure how the columns should be arranged. Should 1 column be the subject id, another the trial type, another the time, and then 1 for the DV? Does this matter? Any points in the right direction would be greatly appreciated. Thanks.

-

Yes, you need to set up your data so that each grouping factor, dependendent variable and covariate corresponds to a column and every row contains one observation (i.e. long format): Everything that you enter into the model formulas for the random and fixed parts has to be a column in your data set.

You can use reshape to get your data from wide to long format and back.

If you are transitioning to R and do a lot of mixed models, try to get a copy of Pinheiro/Bates "Mixed-Effects Models in S and S-PLUS", it's a comprehensive reference from the guys who wrote nlme with a lot of worked examples.

-

It's unlikely reshape will help you out if you're coming straight from SPSS files because you not only need it set up differently, you probably need different data than you were using in SPSS. In the standard repeated measures analysis you enter means of each individual condition for each subject into the analysis but for nlme you enter each individual data point. You'll have to go back to the raw data files. If those have a data point on each line and columns to identify the condition of each point then you can just basically concatenate each file together, identifying the separate subjects along the way. Something like the following would work in that case

fList <- list.files('myDataF/')
dList <- lapply(fList, function(f) {x <- read.table(paste('myDataF', f, sep = '/'), header = TRUE)
x\$subj <- f
return(x)})
myData <- do.call(rbind, dList)


(this could be even simpler if you knew that there was exactly the same number of lines in each file)

-
The analysis is being done on means of response times and accuracies that are collected into 8 bins based on an individual's ordered response times for each trial type. For this type of analysis, will nlme() work on means rather than the data points that compose the mean? –  Matt Dec 9 '10 at 18:59
Get the raw data. The analysis works on the raw data the went into generating the means. Otherwise, you'll get a result that looks suspiciously like your original ANOVA. –  John Dec 9 '10 at 20:01

NLME relies on a "univariate" as opposed to "multi-variate" data structure. See the description below, copied from my response to another question here:

Data manipulation in R for functional data analysis

As to how you would get the data into R and into one of these formats, we'd need to know more about what your input file looks like and the format that it is in. However, here are some general tips on formatting the type of data that you have for analysis in any system.

Singer (Applied Longitudinal Data Analysis) suggests two generally useful layouts for the statistical analysis of longitudinal data: the person-level (mutlivariate) structure or the person-period (univariate) structure. The latter is generally preferrable for a number of reasons.

The person-level data structure (or the multivariate format) contains one row of data for each observational unit (such as persons) and a variable for each measurement period. Age would not be included in the data set and would be implicit in the levels of your time factor (e.g., in a repeated measures ANOVA). This structure can lead naturally to summaries that aren't very meaningful, is less efficient than it could be, and cannot account for your unequally spaced observations (differing age intervals between observations) or time-varying covariates.

That data setup might look something like this...

Mutlivariate
ID  Gender  height1 height2 height3 height4 height5
1   Boy     76.2   74.6   78.2   77.7     76
2   Boy     80.4   78.0   81.8   80.5     80
3   Boy     83.3   82.0   85.4   83.3     83
4   Girl    96.0   94.9   97.1   98.6     96
5   Girl    87.7   90.0   89.6   90.3     89
6   Girl    85.7   86.9   87.9   87.0     86


A preferable layout is often the person-period layout (or the univariate format) where each individual has a record for each time for which they were observed. The person-period dataset has a number of advantages. First, it leads to more natural summaries of the data, e.g. getting an average by group, by time or by group and time is now straight forward. Second, the dataset will accommodate entry of unequal intervals in the time dimension, such as you have here. In addition, if you have them, you could add columns for any other demographic covariates and these could differ over time. Also, data in this format is prepared for modern analytical techniques such as multilevel modeling. Finally, the univariate data structure is consistent with good practice in database design and normalization, increasing efficiency and making it appropriate for the typical query structure.

The univariate layout would look something like this...

Univariate
ID  Age Gender  Height
1   1   Boy     76.2
1   1.5 Boy     74.6
1   3   Boy     78.2
1   5   Boy     77.7
2   1   Girl    80.4
2   1.5 Girl    81.8
2   3   Girl    80.5
2   5   Girl    80
3   1   Boy     115.8
3   1.5 Boy     112.3
3   3   Boy     111.0
3   5   Boy     104.1

-