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
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
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
(this could be even simpler if you knew that there was exactly the same number of lines in each file)
NLME relies on a "univariate" as opposed to "multi-variate" data structure. See the description below, copied from my response to another question here:
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...
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...