[R] Comparing two regression line slopes

Benedikt Niesterok KleinerHaifisch at gmx.net
Wed Apr 15 11:04:45 CEST 2009


Hello R users,
I've used the following help:

> "Comparing two regression line slopes"
> I knew the method based on the following statement :
> t = (b1 - b2) / sb1,b2
> where b1 and b2 are the two slope coefficients and sb1,b2 the pooled  
> standard error of the slope (b)
> 
> which can be calculated in R this way:
>  > df1 <- data.frame(x=1:3, y=1:3+rnorm(3))
>  > df2 <- data.frame(x=1:3, y=1:3+rnorm(3))
>  > fit1 <- lm(y~x, df1)
>  > s1 <- summary(fit1)$coefficients
>  > fit2 <- lm(y~x, df2)
>  > s2 <- summary(fit2)$coefficients
>  > db <- (s2[2,1]-s1[2,1])
>  > sd <- sqrt(s2[2,2]^2+s1[2,2]^2)
>  > df <- (fit1$df.residual+fit2$df.residual)
>  > td <- db/sd
>  > 2*pt(-abs(td), df)
Using my data I finally get the value of the test, which is: 2.245e-7.
Do my slopes differ significantly now?
Thanks for help,  Benedikt
 

--




More information about the R-help mailing list