I receive error when trying to use a simple OLS function?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
First I receive this error:
And then when converting my variable 'iota' into a table, I get the following error:
Can someone please provide some information on the following problem?
Getting very nervous for my exam in the beginning of the new year.
3 Kommentare
Antworten (1)
dpb
am 26 Dez. 2019
Bearbeitet: dpb
am 26 Dez. 2019
"...certainly nothing should be out of context here. "
Well, other than the missing function, the error says line 33 and there's only some 10-15 lines in the image and no line number so, yes, there is some lack of context here.
But, w/ the ols function, the meaning of the error regarding ctranspose is clear--the expression data(:,2), etc., returns a table of the given column, not the underlying data array and it doesn't make sense to try to transpose a table.
If you're going to use a table variable as the underlying data type, use the dot notation to return it instead; or if it is more convenient programmatically to use subscripts, dereference the table with the "curlies" {}.
We also don't have your table and you didn't show us its structure so we don't know if it has variable names or not...would make sense to create some when you read it if they aren't in the input file, but I'll just use the default VarN that readtable creates if they are not provided.
data=readtable('yourfile');
LHS=data.Var2;
RHS=[ones(height(data),1) data.Var3 data.Var4];
b=ols(LHS,RHS);
Alternatively, to illustrate the array notation RHS could be written as
RHS=[ones(height(data),1) data{:,3:4}];
NB: the {} to dereference the table.
NB2: MATLAB has the builtin \ backslash operator to solve OLS problems that uses more robust numerical methods than direct matrix solution as your ols function does. I'd strongly recommend using it instead.
See doc mldivide for details...
2 Kommentare
dpb
am 26 Dez. 2019
"Thank you very much, the code you provided seems to work. This was a great help and much appreciated. " -- Answer moved to comment--dpb
dpb
am 26 Dez. 2019
No problem, glad to be of assistance. If above does solve the problem, please ACCEPT the Answer to indicate so to others if nothing else...
Siehe auch
Kategorien
Mehr zu Elementary Math finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!