[R] Didactic example and doubt: how to compare two regression line slopes

Jose Claudio Faria joseclaudio.faria at gmail.com
Fri Apr 10 15:24:46 CEST 2009


Hi,

I read almost all I found in prior R-Help list about How to compare
two regression line slopes.

So, I made a didactic example to illustrate a solution cited by Ben Bolker:
===============================================
Subject:	Re: [R] How to compare two regression line slopes
From:	Ben Bolker (bol... at ufl.edu)
Date:	Jan 27, 2009 1:52:20 pm
List:	org.r-project.r-help
===============================================

I would like to know the opinion of other specialists.
I'm needing do apply this solution in a real (and experimental)
situation and the
data are "like" iris data.

# libraries
library(ggplot2)

# a example based in iris data (nonsense)
qplot(Petal.Width, Sepal.Length,
      data=iris,
      facets= . ~ Species,
      geom=c('point', 'smooth'),
      se=F,
      method='lm',
      formula=y ~ x)

# Function to test slope
t.slope <- function(dF) {
  fit <- lm(y~x*f, data=dF)
  coef(summary(fit))["x:f2",]
}

# setosa vs. versicolor
x  <- subset(iris, Species != 'virginica', sel=Petal.Length)[,]
y  <- subset(iris, Species != 'virginica', sel=Petal.Width)[,]
f  <- factor(rep(1:2, each=50))
dF <- data.frame(x, y, f)
t.slope(dF)

# setosa vs. virginica
x  <- subset(iris, Species != 'versicolor', sel=Petal.Length)[,]
y  <- subset(iris, Species != 'versicolor', sel=Petal.Width)[,]
f  <- factor(rep(1:2, each=50))
dF <- data.frame(x, y, f)
t.slope(dF)

# versicolor vs. virginica
x  <- subset(iris, Species != 'setosa', sel=Petal.Length)[,]
y  <- subset(iris, Species != 'setosa', sel=Petal.Width)[,]
f  <- factor(rep(1:2, each=50))
dF <- data.frame(x, y, f)
t.slope(dF)

Thanks,
-- 
///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\
Jose Claudio Faria
Estatistica - prof. Titular
UESC/DCET/Brasil
joseclaudio.faria at gmail.com
joseclaudio.faria at terra.com.br
///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\




More information about the R-help mailing list