5
$\begingroup$

I'm learning linear regression in Udacity as a beginner. I know statsmodels.regression.linear_model.OLS() needs an intercept but why do you set 1 as intercept?

Even though we set that value, the fit result shows different values as the intercept.

Then what does setting 1 mean? Also do we usually use 1 for this setting?

$\endgroup$
0
3
$\begingroup$

I don't know the python function/method you are referring to. But you may be confusing that the 1 you add is to your variables/feature such that it is multiplied by the intercept parameter in your parameter vector. In other words, 1 is rather added to your features and is NOT the value of your intercept.

$\endgroup$
5
  • $\begingroup$ Thank you for your answer and you're right about I'm confused. Then any number is ok instead of 1? $\endgroup$
    – Yuki.U
    Mar 8 '19 at 0:28
  • $\begingroup$ Technically you could use any number, but it is convention to use 1. It makes things nicer. $\endgroup$
    – Tom
    Mar 8 '19 at 10:21
  • $\begingroup$ I see...! My mind became clear! Thank you for your help. $\endgroup$
    – Yuki.U
    Mar 8 '19 at 14:51
  • 3
    $\begingroup$ You can't use zero for an intercept... $\endgroup$
    – Sycorax
    Sep 1 '20 at 5:33
  • $\begingroup$ @Yuki.U If you use a number different from 1, interpretation becomes much harder. Intercept often is the "baseline". So if you use 1, the beta0 indicates the baseline level. If you would use, e.g. 0.5, then beta0 would be twice the baseline. So you would need to calculate what the actual baseline is, if you are interested in that value. For just predictions or significance testing, it doesn't matter at all. $\endgroup$
    – LiKao
    Sep 1 '20 at 8:25
3
$\begingroup$

I do not know Python, but as you can readily illustrate in R, setting the value of the intercept to 1 is really just a convention (a useful one, though, of course, allowing us to interpret the intercept as the expected effect when $x=0$).

n <- 10
y <- rnorm(n)             # some random data
x <- rnorm(n)
intercept <- rep(1,n)     # a "hand-made" intercept

lm(y~x)                   # the default in R which includes an intercept
lm(y~intercept+x-1)       # removing the default intercept with -1 and re-adding it manually as another regressor
lm(y~I(2*intercept)+x-1)  # removing the default intercept with -1 and re-adding 2 as a constant term

Output:

> lm(y~x)

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept)            x  
   -0.07813      0.55086  


> lm(y~intercept+x-1)

Call:
lm(formula = y ~ intercept + x - 1)

Coefficients:
intercept          x  
 -0.07813    0.55086  


> lm(y~I(2*intercept)+x-1)

Call:
lm(formula = y ~ I(2 * intercept) + x - 1)

Coefficients:
I(2 * intercept)                 x  
        -0.03907           0.55086 

As you can see, the first two regressions are exactly the same (as fully expected), and the third has the same coefficient on x, and exactly half the coefficient on the constant term, to account for the effect that we have multiplied that by two.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.