[R] compare linear regressions

Holger Taschenberger Holger.Taschenberger at mpi-bpc.mpg.de
Tue Nov 8 14:52:05 CET 2011


Hi,

        I'm trying to compare two linear regressions. I'm using the
following approach:
##################
xx<-1:100
df1 <- data.frame(x = xx, y = xx * 2 + 30 + rnorm(n=length(xx),sd=10), g = 1)
df2 <- data.frame(x = xx, y = xx * 4 + 9  + rnorm(n=length(xx),sd=10), g = 2)
dta <- rbind(df1, df2)
dta$g <- factor(dta$g)
plot(df2$x,df2$y,type="l",col="red")
lines(df1$x,df1$y,col="blue")
summary(lm(formula = y ~ x + g + x:g, dta))
##################
I learned that the coefficients (g2 and x:g2) tell me about the
differences in intercept and slope and the corresponding p-values.

Now I'm trying to do the same except that there should be no intercept
term:
##################
xx<-1:100
df1 <- data.frame(x = xx, y = xx * 2 + rnorm(n=length(xx),sd=10), g = 1)
df2 <- data.frame(x = xx, y = xx * 4 + rnorm(n=length(xx),sd=10), g = 2)
dta <- rbind(df1, df2)
dta$g <- factor(dta$g)
plot(df2$x,df2$y,type="l",col="red")
lines(df1$x,df1$y,col="blue")
summary(lm(formula = y ~ x - 1 + x:g, dta))
##################
I assume that the last line is the correct way to specify a linear model
without intercept. But I'm not certain about that. Can someone please
confirm?

Thanks a lot,
        Holger



More information about the R-help mailing list