Jarrod Morrice
2010-Mar-11 14:51 UTC
[R] Different results for different order of factor levels?
Dear R community, I am a newbie to R and I am using lme() to analyzed a two way repeated measures ANCOVA on some data I have gathered. In producing some graphs based on the fixed effects I noticed that I get vary different results depending on how I order my levels in my factor statement (see code below). Now, I have read that different models treat data from the factor class different for ordered vs non-ordered factors but I am unsure as to how this works for lme(). Shown below is a set of code used to get the coefficients for two different orders of factor levels. THE CODE USED TO GET THE COEFFICIENTS: #Abbr = Subject #PDSI = covariate #PDSI2 = PDSI^2 (To fit a polynomial) #Set the Level order i.levels<-c("Mixed","Short","Tall","NoType") #I am not interested in this level for my comparison data.sub <- data[which(data$indep != "NoType"),] #Convert the data into factors data$indep<-factor(data$indep, levels = i.levels) #Run the model F <- lme(response~indep+PDSI+PDSI2+indep:PDSI+indep:PDSI2, random = ~PDSI|Abbr, data = data.sub, method = "ML") #Get the coefficients for the fixed effects summary(F)$coefficients$fixed THE OUTCOME: > i.levels<-c("Tall","Short","Mixed","NoType") > summary(F)$coefficients$fixed (Intercept) indep1 indep2 PDSI PDSI2 indep1:PDSI 115.20850196 2.13074177 -18.64650516 2.32042307 -0.46510460 0.09965080 indep2:PDSI indep1:PDSI2 indep2:PDSI2 0.90607575 0.01106839 -0.28827352 Other order: > i.levels<-c("Mixed","Short","Tall","NoType") > summary(F)$coefficients$fixed (Intercept) indep1 indep2 PDSI PDSI2 indep1:PDSI 115.2085020 16.5157634 -18.6465052 2.3204231 -0.4651046 -1.0057266 indep2:PDSI indep1:PDSI2 indep2:PDSI2 0.9060757 0.2772051 -0.2882735 Also, as I am writing this I found another peculiarity. The order of the first declaration of the levels also matters somehow. To get the above results, I ran a program that ran the first order of i.levels and immediately following this code is a duplicate code with the second order of i.levels specified. I ran them both at the same time. When I ran the code with Mixed, Short, Tall, NoType first, both orders produced the same coefficients. But if I ran the Tall,Short,Mixed,NoType first then I got the above results. I am even more lost now. Any insight would be greatly appreciated. Thanks Jarrod [[alternative HTML version deleted]]