[R] Adding regression lines to each factor on a plot when using ANCOVA

Michael Friendly friendly at yorku.ca
Fri Apr 2 19:07:33 CEST 2010


This is a nice example; thanks for providing it in this form.  I tried 
to trim it down to show fewer groups, but ran into the following errors
that I can't understand:

## keep species 1:6
 > dataset <- subset(dataset, species < 7)
Warning message:
In Ops.factor(species, 7) : < not meaningful for factors

## OK, just subset the rows of dataset to keep species 1:6

 > dataset <- dataset[1:20,]
 > ancova(logBeak ~ logMass       * species, data=dataset)
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
   contrasts can be applied only to factors with 2 or more levels
 > ancova(logBeak ~ logMass       + species, data=dataset)
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
   contrasts can be applied only to factors with 2 or more levels

-Michael


RICHARD M. HEIBERGER wrote:
> ## Steve,
> 
> ## please use the ancova function in the HH package.
> 
> install.packages("HH")
> library(HH)
> 
> 
> ## windows.options(record=TRUE)
> windows.options(record=TRUE)
> # hypothetical data
> beak.lgth <-
> c(2.3,4.2,2.7,3.4,4.2,4.8,1.9,2.2,1.7,2.5,15,16.5,14.7,9.6,8.5,9.1,
>   9.4,17.7,15.6,14,6.8,8.5,9.4,10.5,10.9,11.2,11.5,19,17.2,18.9,
>   19.5,19.9,12.6,12.1,12.9,14.1,12.5,15,14.8,4.3,5.7,2.4,3.5,2.9)
> mass <-
> c(45.9,47.1,47.6,17.2,17.9,17.7,44.9,44.8,45.3,44.9,39,39.7,41.2,
>   84.8,79.2,78.3,82.8,102.8,107.2,104.1,51.7,45.5,50.6,27.5,26.6,
>   27.5,26.9,25.4,23.7,21.7,22.2,23.8,46.9,51.5,49.4,33.4,33.1,33.2,
>   34.7,39.3,41.7,40.5,42.7,41.8)
> ## Make species into a factor
> species <-
> factor(c(1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,
>          8,8,8,8,9,9,9,9,9,10,10,10,11,11,11,11,12,12,12,12,12))
> ## then construct a data.frame with the three variables and the log
> transforms
> dataset <-  data.frame(species, beak.lgth, mass,
>                        logBeak=log10(beak.lgth),
>                        logMass=log10(mass))
> ## default is 7 colors, we need 12
> trellis.par.set("superpose.line",
>   Rows(trellis.par.get("superpose.line"), c(1:6, 1:6)))
> trellis.par.set("superpose.symbol",
>   Rows(trellis.par.get("superpose.symbol"), c(1:6, 1:6)))
> 
> ancova(logBeak ~ logMass       * species, data=dataset)
> ancova(logBeak ~ logMass       + species, data=dataset)
> ancova(logBeak ~ logMass, groups=species, data=dataset)
> ancova(logBeak ~                 species, x=logMass, data=dataset)
> bwplot(logBeak ~ species, data=dataset)
> 
> ## Rich
> 
> 	[[alternative HTML version deleted]]
> 


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list