# Linear Regression step by step for newcomers

**URL:** https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621
**Category:** Machine Learning
**Tags:** question
**Created:** [November 1, 2022, 4:32pm UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621 "2022-11-01T16:32:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Troiss](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@Troiss](https://discourse.julialang.org/u/Troiss)
#### Post date: [November 1, 2022, 4:32pm UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/1 "2022-11-01T16:32:53Z")

</div>

Hello. I’m very new to machine and even newer to Julia, as a whole, thought the syntax is still easy to understand. I got a task to train linear regression a set of data (X, y), in which X is the matrix of input vectors, and y is the vector of outputs. The result should be the vector of parameters of linear regression after training (w). What would be the steps for this?

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [November 1, 2022, 7:24pm UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/2 "2022-11-01T19:24:01Z")

</div>

There are multiple methods to do linear regression, the most (statistically) efficient one is ordinary least squares (short OLS; analytically minimizing the sum of squared errors). You can obtain the OLS estimate using just

```julia
X\y

```

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [November 1, 2022, 7:26pm UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/3 "2022-11-01T19:26:55Z")

</div>

There also are registered packages you can use, e.g., [GitHub - st--/LinearRegression.jl: Simple & fast linear regression in Julia](https://github.com/st--/LinearRegression.jl).

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [November 1, 2022, 8:27pm UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/4 "2022-11-01T20:27:20Z")

</div>

```julia
using GLM
data = DataFrame(your_data_here)
lm(@formula(output ~ input1 + input2 + ...), data)

```

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [November 2, 2022, 6:21am UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/5 "2022-11-02T06:21:09Z")

</div>

[https://fluxml.ai/Flux.jl/stable/models/overview/](https://fluxml.ai/Flux.jl/stable/models/overview/)

---

<div class="post-metadata">

### Author: ![Troiss](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@Troiss](https://discourse.julialang.org/u/Troiss)
#### Post date: [November 2, 2022, 8:05am UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/6 "2022-11-02T08:05:28Z")

</div>

I have researched about that package too, but sadly my assignment is having to make the computer learn itself, which would be through MSE and likely gradient descent.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 2, 2022, 8:38am UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/7 "2022-11-02T08:38:18Z")

</div>

Is this a homework problem? If so make sure to mention this so people can consider academic integrity issues in responding.

That said you seem to be in luck either way because the link @mcreel posted gives you a worked example of fitting a linear regression in Flux.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [November 2, 2022, 9:00am UTC](https://discourse.julialang.org/t/linear-regression-step-by-step-for-newcomers/89621/8 "2022-11-02T09:00:44Z")

</div>

This sounds like homework to me, but with all the information out there, the Flux documentation seems like a fair source to use.
