Linear regression from scratch using QR decomposition

Hi there :slight_smile:

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

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 :slight_smile:

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.

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

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

1 Like