# Nested data analysis using nlme: Analysis leaves out factor levels

Nested data analysis using nlme: Analysis leaves out factor levels

I have a few questions regarding the analysis of nested data from an experiment. In the study, participants viewed 50 stimuli for 3 durations (50 ms, 500 ms, and 1000 ms, within-subjects, resulting in 150 trials per participant) and provided 3 responses for each stimulus presentation. There were 2 groups of participants (between-subjects conditions), one group saw the stimuli in a darkened version and the other group saw lighter versions of the stimuli. The order of presentation was completely randomized (both regarding the stimuli and their duration of the presentation). The data is currently in long format (each trial is a row with the stimulus id, participant id, gender, duration, condition, and the three responses). What is the best way to analyze this type of data using R?

I have tried a mixed effects model (nlme) with subject id as random factor:

model_lme<-lme(response1 ~ condition * duration, random=~1|subj,data=dat)


In my results, I only get two of the three levels of the factor duration:

Fixed effects: response1 ~ condition * duration
Value  Std.Error   DF   t-value p-value
(Intercept)  4.094293 0.08266924 9979  49.52619  0.0000
condition1 0.122374 0.08266924   65   1.48028  0.1436
duration1  -0.315817 0.02026158 9979 -15.58699  0.0000
duration2  -0.004890 0.02026158 9979  -0.24135  0.8093


Am I using nlme correctly? Why is one level missing?

You are asking three questions in your post. The first one (What is the best way to analyze this type of data using R?) makes little sense, as no one knows what is the question you are trying to answer.

As for using nlme correctly, probably you are doing something wrong. Note that you are not missing one level of duration, as the third level is the baseline compared to which other two are calculated. You are rather missing two interaction levels of condition*duration. I was curious to replicate you analysis with random data, and my output looks like this (ignore the actual numbers):

Fixed effects: response ~ condition * duration
Value Std.Error   DF    t-value p-value
(Intercept)                 6.328458  3.414836 1486   1.853225  0.0640
conditionlight             -5.984724  4.829307    8  -1.239251  0.2504
duration50                 -1.945657  0.087851 1486 -22.147346  0.0000
duration500                -0.869570  0.087851 1486  -9.898285  0.0000
conditionlight:duration50   1.959904  0.124239 1486  15.775209  0.0000
conditionlight:duration500  0.818661  0.124239 1486   6.589375  0.0000


Maybe there is something wrong with the types of your variables (I used strings for condition and duration), but I could not get your output, so I don't know for sure. Try reading this, perhaps.

• Thanks! To clarify: I asked the very general question to check if there is a better way to deal with nested data than my approach. I am basically running three analyses (1 for each response) like the one I posted. Regarding the output: I am very sorry, but I simply missed to post the interaction results from my output. You mention that there is a baseline compared to which the two others are compared. Is this always the last factor level like in your output? How would this output be interpreted? I need to analyze which factor levels are significantly different. Is this possible? – user55987 Nov 2 '14 at 22:13
• I am not sure, if it is always the last factor level that is missed, but it should not make a lot of difference. As for interpretation, I really recommend page 13 of that pdf file. In your case the reasoning will be very similar. For example, the coefficient -1.94 near duration50 (in my table) means that the response is on average lower by -1.94 with duration 50 compared to duration 1000 (the missed baseline level), and the difference is significant. – psarka Nov 3 '14 at 8:12
• The "missing" level is always the first one in levels(yourFactorVariable), i.e., the first value after alphabetical sorting per default. – Roland Nov 3 '14 at 9:42
• Thanks for your comments, I have a much better understanding now. I still have one question regarding the results: is there a way to get pairwise comparisons using nlme? I don't want to compare the factor levels to a baseline, but would also like to get the p-values of the comparisons between all durations. – user55987 Nov 8 '14 at 13:54
• Take a look at testInteractions (from phia package) or glht (from multcomp package). – psarka Nov 10 '14 at 12:43