John James Pork
John James Pork

Reputation: 181

Details of lm function in R

If in R I use the line:

linear <- lm(y~x-1)

R will find a regression line passing by the origin.

My question is, the origin is x=0 or the lowest of the x values?

FOr example if my x values are 1998 to 2011, the fitted line will pass by 1998 or the year 0?

Upvotes: 5

Views: 5592

Answers (2)

Ari B. Friedman
Ari B. Friedman

Reputation: 72731

As @Marcinthebox points out, it will go through the origin. To see it graphically:

x <- seq(-5,5)
y <- 3*x+rnorm(length(x))
fit.int <- lm(y~x)
fit <- lm(y~x-1)
summary(fit)

plot(y~x,xlim=c(-.1,.1),ylim=c(-.1,.1))
abline(fit,col="red")
abline(fit.int,col="blue")
abline(h=0)
abline(v=0)

plot of origin

Upvotes: 4

Marc in the box
Marc in the box

Reputation: 11995

With "-1" in the equation, the slope will go through the origin. You can see this by predicting the value at x=0:

x <- 1998:2011
y <- 3*x+rnorm(length(x))
fit <- lm(y~x-1)
summary(fit)
newdata <- data.frame(x=0:10)
predict(fit,newdata)

Upvotes: 10

Related Questions