[R] Error: contrasts can be applied only to factors with 2 or more levels

John Kane jrkrideau at inbox.com
Thu May 16 18:33:49 CEST 2013


Beautiful first question.  

333 
ch.list <- lmList(h12~spp+t+w+l+inih|id, data=mydata)
Should that not be data= chm


John Kane
Kingston ON Canada


> -----Original Message-----
> From: ebelair at purdue.edu
> Sent: Thu, 16 May 2013 15:05:35 +0000
> To: r-help at r-project.org
> 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 at 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.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list