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" <ebel...@purdue.edu> To: "r-help@R-project.org" <r-help@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. > library(lme4) > chm <- dput(head(chm)) structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"), class = "factor"), site = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("C-H", "M"), class = "factor"), plot = structure(c(2L, 2L, 3L, 3L, 3L, 8L), .Label = c("2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035", "2036", "2037", "2038", "2039", "2040", "2041", "2042", "2043", "2044", "2045"), class = "factor"), rx = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"), rxg = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "6"), class = "factor"), rxl = structure(c(5L, 5L, 5L, 5L, 5L, 3L), .Label = c("C", "H", "Mc", "McH", "Mn", "MnH"), class = "factor"), t = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"), w = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"), l = c(14.55, 14.55, 13.34, 13.34, 13.34, 11.63), spp = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("ac", "rm", "ro", "sm"), class = "factor"), inid = c(9.6, 7.4, 6, 7.1, 7.5, 5.7), inih = c(74.5, 69.5, 66.7, 75.4, 57.5, 71.5), d09 = c(9.6, 6, 5.7, 7.5, 6.9, 6 ), d10 = c(13, 9.8, 8, 13, NA, 7.7), d11 = c(13.5, NA, 11, 11.8, NA, NA), d12 = c(14.2, 10.7, 10.1, 12.3, NA, 8.5), h09 = c(96, 72, 75, 101, 58, 84), h10 = c(109, 77, 80, 113, NA, 89), h11 = c(102, 76, 79, 115, NA, 84), h12 = c(110, 92, 91, 128, NA, 86)), .Names = c("id", "site", "plot", "rx", "rxg", "rxl", "t", "w", "l", "spp", "inid", "inih", "d09", "d10", "d11", "d12", "h09", "h10", "h11", "h12"), row.names = c(NA, 6L), class = "data.frame") > chm$id = as.factor(chm$id) > chm$spp = as.factor(chm$spp) > chm$h12 = as.numeric(chm$h12) > str(chm) > ch.list <- lmList(h12~spp+t+w+l+inih|id, data=mydata) Thanks! Ethan [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.