Linear Regression with a Matrix
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
My problem is the following : I've written that script to estimate the residuals in a least square Regression.
function [ epsilon ] = estimate_residual( db, y, first_elem, last_elem, interval)
x = db(:, first_elem:interval:last_elem);
tx = transpose(x);
beta = (tx*x)\(tx*y);
epsilon = y - (x*beta);
plot(e);
end
The residuals is supposed to follow a Normal Distribution ( I know that it does ). But... It doesn't when I use the hypothesis test functions such as chi2gof or jbtest. Do you have any idea where the problem could come from in the script?
Some more information :
x -> 1860*231 y -> 1860*1
Thank you for your answers!
2 comentarios
the cyclist
el 9 de Mayo de 2018
Editada: the cyclist
el 9 de Mayo de 2018
Can you upload a file with x and y?
John D'Errico
el 9 de Mayo de 2018
First of all, the solution you used to compute the regression estimates is a poor one. Just use
beta = x\y;
This is far more numerically stable than what you used. MATLAB was written to know how to solve that class of problem well. So use slash directly.
As far as the question about the residuals, we would need to have that data to be able to provide a good answer.
Respuestas (2)
the cyclist
el 9 de Mayo de 2018
Editada: the cyclist
el 9 de Mayo de 2018
It is not uncommon, when you have a fairly large dataset, to fail such tests. When you plot the residuals, they are certainly not radically different from normally distributed. (See attached.)
It is true that your residuals have a bit of skew, and are somewhat leptokurtic. But I think only a real purist would criticize you for not meeting the model assumption of having normally distributed residuals here.
0 comentarios
Najib Aghenda
el 9 de Mayo de 2018
3 comentarios
John D'Errico
el 9 de Mayo de 2018
Editada: John D'Errico
el 9 de Mayo de 2018
And, as far as following the method of ordinary least squares, you still don't understand that what you did was a poorly written, because you were taught a poor way to solve the problem.
You used the normal equations. Which are not a good numerical solution. Teachers teach what they learned years ago as students. That it is numerical crap, is not their fault. But it is now your fault if you continue to use that form, and to tell others that it is ok to do so.
As far as using
beta = x\y
NO you did not use that. Must I copy in what you yourself wrote?
beta = (tx*x)\(tx*y);
That is very different. It squares the condition number of the matrix x. While I admit that much of the time the difference is not significant if your problem is not ill-conditioned already, when that is an issue, what you DID write WILL be a significant problem.
beta = x\y
IS the correct way to solve that problem, even when the matrix x is not square. So use it.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!