I'm trying to implement a cost function for linear regression withouth intercept.

I tried understanding what "no intercept" means, but not found anything which could reassure me. I have a dataset that I scale in R with scaled_data <- scale(data).

As I'm scaling, does this mean that my linear regression does not rely on an intercept?

  • $\begingroup$ That means you are fitting a linear function to the data that goes exactly through the origin of the coordinate system. Intercept is where the regression function hits the y-axis and 'no intercept' means hit the y-axis at 0. $\endgroup$
    – Nechoj
    Dec 2, 2021 at 15:21
  • $\begingroup$ You should probably do something like lm(cost ~ -1 + predictor1 + predictor2, data) $\endgroup$
    – Ben Bolker
    Dec 2, 2021 at 15:32
  • $\begingroup$ Related: stats.stackexchange.com/questions/208341/… $\endgroup$ Dec 3, 2021 at 11:13

1 Answer 1


The formula of the linear regression is,


where the coefficient $\beta_0$ is the intercept in the model. This can be written in matrix notation as,

$$y = X\beta+\varepsilon$$

where we are making a slight abuse of notation, because in order to include the $\beta_0$ in this formula, we are writing the vector $$\beta=(\beta_0,\beta_1,\ldots,\beta_p)$$ and we are writing the matrix $$X=(1,x_1,\ldots,x_p)$$

So if you want to implement a cost function for a linear regression model without intercept, you just need to remove $\beta_0$ from the vector $\beta$ and remove the vector $1$ from the matrix $X$.

Now, using the already available implementation of linear regression in R, this can be done, as suggested in the comments, by setting

lm(cost ~ -1 + predictor1 + predictor2, data)

The -1 there means that you do not want an intercept in the model. The result of this however is very different from the one you would obtain if you scale the data.

Scaling the data means that your data is centered, and the intercept of models built using centered data is $0$. In this case, you would obtain the same result with a non intercept model.

But if you build a model without an intercept in a dataset that is not scaled, you will force your regression line to go through the value (0,0), and then the result of a model with intercept and one without intercept will be different.

  • $\begingroup$ Alright so when we wish to write a linear regression model with intercept; given a dataset in matrix form we would add a $\beta_{0}$ column. If we do this and apply lm(), we get a model with intercept. If we don't add the $\beta_{0}$ column we get a model withouth intercept? Regarding the scaling, it reshapes our data to be oriented around 0 rather then the intercept $\beta_{0}$? And since then $\beta_{0} = 0$, we can simply leave this term out of the equation $\beta = (\beta_{0}=0 => remove), \beta_{1}, ..., \beta_{p}$. Is that correct? $\endgroup$
    – OLGJ
    Dec 5, 2021 at 12:00
  • $\begingroup$ Yes, that is correct. $\endgroup$ Dec 6, 2021 at 10:13
  • $\begingroup$ Got it! Thank you Álvaro! :) $\endgroup$
    – OLGJ
    Dec 7, 2021 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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