[R] compare linear regressions

R. Michael Weylandt michael.weylandt at gmail.com
Tue Nov 8 15:05:47 CET 2011


Yes, adding "-1" or "+0" both return a lm without an intercept term.

Michael

On Tue, Nov 8, 2011 at 8:52 AM, Holger Taschenberger
<Holger.Taschenberger at mpi-bpc.mpg.de> wrote:
> 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
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list