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
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. >
Thank you. I was confused because the output of the line "summary(lm(formula =..." reads: "Coefficients: (1 not defined because of singularities)", which did not look like a normal message (which can safely be ignored) to me. --Holger On Tue, 8 Nov 2011 15:06:28 +0100 Peter Konings <peter.l.e.konings at gmail.com> wrote:> On Tue, Nov 8, 2011 at 2:52 PM, Holger Taschenberger < > Holger.Taschenberger at mpi-bpc.mpg.de> wrote: > <snip> > > > 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? > > > Yes, that's true. See chapter 11 of the "Introduction To R" manual that was > installed with R for an overview of model specification in R. > > HTH > Peter.
Maybe Matching Threads
- Null-Hypothesis
- Compare two dataframes
- Best way/practice to create a new data frame from two given ones with last column computed from the two data frames?
- merging or joining 2 dataframes: merge, rbind.fill, etc.?
- as.data.frame.matrix() returns an invalid object