Hi,
The example dataset only shows 1 level for ID.
chm
? id site plot rx rxg rxl t w???? l spp inid inih d09? d10? d11? d12 h09 h10
1? 1? C-H 2002? 1?? 1? Mn N N 14.55? ac? 9.6 74.5 9.6 13.0 13.5 14.2? 96 109
2? 1? C-H 2002? 1?? 1? Mn N N 14.55? ac? 7.4 69.5 6.0? 9.8?? NA 10.7? 72? 77
3? 1? C-H 2003? 1?? 1? Mn N N 13.34? ac? 6.0 66.7 5.7? 8.0 11.0 10.1? 75? 80
4? 1? C-H 2003? 1?? 1? Mn N N 13.34? ac? 7.1 75.4 7.5 13.0 11.8 12.3 101 113
5? 1? C-H 2003? 1?? 1? Mn N N 13.34? ac? 7.5 57.5 6.9?? NA?? NA?? NA? 58? NA
6? 1? C-H 2008? 2?? 1? Mc N N 11.63? ac? 5.7 71.5 6.0? 7.7?? NA? 8.5? 84? 89
? h11 h12
1 102 110
2? 76? 92
3? 79? 91
4 115 128
5? NA? NA
6? 84? 86
Using another dataset:
data(warpbreaks)
warpbreaks2<- warpbreaks
warpbreaks2$id<-rep(1:6,9)
warpbreaks2$id<- factor(warpbreaks2$id)
set.seed(45)
?warpbreaks2$inih<- rnorm(54,70)
?str(warpbreaks2)
#'data.frame':??? 54 obs. of? 5 variables:
# $ breaks : num? 26 30 54 25 70 52 51 26 67 18 ...
# $ wool?? : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
# $ tension: Factor w/ 3 levels "L","M","H": 1 1 1 1 1 1 1 1 1 2 ...
# $ id???? : Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6 1 2 3 4 ...
# $ inih?? : num? 70.3 69.3 69.6 69.3 69.1 ...
library(lme4)
lmList(breaks~wool+tension+ inih|id,data=warpbreaks2)
#Call: lmList(formula = breaks ~ wool + tension + inih | id, data = warpbreaks2)
#Coefficients:
#? (Intercept)????? woolB?? tensionM?? tensionH????? inih
#1?? -170.9470? -6.634896? -7.214566? -9.376711? 2.930854
#2??? 247.7175?? 5.363119? -1.958410 -10.652740 -3.183826
#3?? -304.1479 -13.483864 -28.437529 -26.656611? 5.120942
#4?? -641.4617? -5.273322? -9.876885 -16.222166? 9.670360
#5??? 476.4729? -6.296008 -10.416367 -16.337461 -6.256378
#6??? 335.3178 -14.219225 -17.714808 -13.488733 -4.054001
#Degrees of freedom: 54 total; 24 residual
#Residual standard error: 12.52028
warpbreaks3<- warpbreaks2
?warpbreaks3$id<- as.numeric(as.character(warpbreaks3$id))
?lmList(breaks~wool+tension+ inih|id,data=warpbreaks3)
#Call: lmList(formula = breaks ~ wool + tension + inih | id, data = warpbreaks3)
#Coefficients:
#? (Intercept)????? woolB?? tensionM?? tensionH????? inih
#1?? -170.9470? -6.634896? -7.214566? -9.376711? 2.930854
#2??? 247.7175?? 5.363119? -1.958410 -10.652740 -3.183826
#3?? -304.1479 -13.483864 -28.437529 -26.656611? 5.120942
#4?? -641.4617? -5.273322? -9.876885 -16.222166? 9.670360
#5??? 476.4729? -6.296008 -10.416367 -16.337461 -6.256378
#6??? 335.3178 -14.219225 -17.714808 -13.488733 -4.054001
#
#Degrees of freedom: 54 total; 24 residual
#Residual standard error: 12.52028
A.K.
----- Original Message -----
From: "Belair, Ethan D" <ebelair at purdue.edu>
To: "r-help at R-project.org" <r-help at r-project.org>
Cc:
Sent: Thursday, May 16, 2013 11:05 AM
Subject: [R] Error: contrasts can be applied only to factors with 2 or more
levels
I have a dataframe that I am attempting to analyze using the lmList() function in package lme4. I'm using this funciton to select which parameters in this model wqarrant random effects. I have a subset of the data below. I input data and change class of necessary items so that I have several factors and several numeric variables. I then run lmList using id as a grouping factor, which produces an error. This only occurs using id as grouping factor, other IV's work fine. R says contrasts can only be applied to factors of 2 or more levels. The entire data set contains 48 levels of id, this subset contains 3, yet the error persists.
I'm relatively new to R and this is my first question posted. I apologize if I've not followed the posting rules exactly, I'm trying to do so, but find coding quite difficult.
Thank you in advance for any help.