Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Details of lm function in R

Tags:

r

lm

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?

like image 375
John James Pork Avatar asked May 14 '12 11:05

John James Pork


2 Answers

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)
like image 74
Marc in the box Avatar answered Oct 20 '22 21:10

Marc in the box


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

like image 22
Ari B. Friedman Avatar answered Oct 20 '22 21:10

Ari B. Friedman