# Linear regression from scratch using QR decomposition

**URL:** <https://discourse.julialang.org/t/linear-regression-from-scratch-using-qr-decomposition/71363>\
**Category:** General Usage\
**Tags:** linearalgebra, regression, qr\
**Created:** [November 12, 2021, 5:31am UTC](https://discourse.julialang.org/t/linear-regression-from-scratch-using-qr-decomposition/71363 "2021-11-12T05:31:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![valperez](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@valperez](https://discourse.julialang.org/u/valperez)\
**Post date:** [November 12, 2021, 5:31am UTC](https://discourse.julialang.org/t/linear-regression-from-scratch-using-qr-decomposition/71363/1 "2021-11-12T05:31:26Z")

</div>

Hi there 🙂

I’m trying to fit a polinomial regression of grade 10 from scratch. First things first, imagine I have a dataset with just two columns, x and y. I want to fit a polinomial of grade 10 but I don’t want to use the fit functions in the Julia packages, I want to build it myself. Therefore, I try to use the QR decomposition with column pivoting because that is what I have in hand.

n = size(data, 1) #number of rows  
k = 10 # degree of the polinomial

x = data.x

X = Array{Float64}(undef, n, k + 1) #building an empty matrix  
X[:, 1] = ones(n) #first column is made of only ones

for i = 1:k #filling the matrix  
X[:, i + 1] = x.^i  
end

F = qr(X, Val(true)) #getting the QR decomposition

According to me, then I have to solve for R’ \beta = Q’Py but the dimesions of Q and P don’t match. I’m not sure what I’m doing wrong, if somebody pls would help me .

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 12, 2021, 5:43am UTC](https://discourse.julialang.org/t/linear-regression-from-scratch-using-qr-decomposition/71363/2 "2021-11-12T05:43:49Z")

</div>

You can make your code legible by surrounding it with a triple backtick fence. And if you add just a bit of information your question will be self-contained so we can try running your code.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 12, 2021, 6:23am UTC](https://discourse.julialang.org/t/linear-regression-from-scratch-using-qr-decomposition/71363/3 "2021-11-12T06:23:01Z")

</div>

I may be getting my math wrong, but I find `\beta = P(R\Q'y)`

---

<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 12, 2021, 6:27am UTC](https://discourse.julialang.org/t/linear-regression-from-scratch-using-qr-decomposition/71363/4 "2021-11-12T06:27:08Z")

</div>

I think this has been discussed as extensively as possible here:

> [@Efficient way of doing linear regression](https://discourse.julialang.org/t/efficient-way-of-doing-linear-regression/31232):
>
> Hello, I need an efficient way to performs linear regression because I have to fit several segments in a big for loops which takes time to achieve. In the past, their was a linreg function which has been depreciated. Currently I am using polyfit to do a polynomial fit of order 1, for each segment. Is there a more efficient way to do this ?
