MATLAB Answers

I receive error when trying to use a simple OLS function?

4 views (last 30 days)
LouLou
LouLou on 26 Dec 2019
Edited: dpb on 26 Dec 2019
First I receive this error:
Screenshot 2019-12-26 at 12.21.01.png
And then when converting my variable 'iota' into a table, I get the following error:
Screenshot 2019-12-26 at 12.21.33.png
Can someone please provide some information on the following problem?
Getting very nervous for my exam in the beginning of the new year.

  3 Comments

dpb
dpb on 26 Dec 2019
I can't find a MATLAB function ols searching the TMW website; link or source???
You pulled error messages out of context with nowhere near enough code to have a clue as to what line N might actually be so we have really nothing to go on...
Start with the first...if we can see what you're trying to use has for instructions, that would go a long way.
LouLou
LouLou on 26 Dec 2019
This is the ols function.
Screenshot 2019-12-26 at 14.20.26.png
I have posted all the code I have written, so certainly nothing should be out of context here.
LouLou
LouLou on 26 Dec 2019
I was not trying to be rude, and certainly expect you aren't either. I do not know why it has written line 33. This is what I get when I try to run the code. Screenshot 2019-12-26 at 15.48.20.png

Sign in to comment.

Answers (1)

dpb
dpb on 26 Dec 2019
Edited: dpb on 26 Dec 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 Comments

dpb
dpb on 26 Dec 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
dpb on 26 Dec 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...

Sign in to comment.

Sign in to answer this question.

Tags


Translated by