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

4 次查看(过去 30 天)
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 个评论
LouLou
LouLou 2019-12-26
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 2019-12-26
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

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2019-12-26
编辑:dpb 2019-12-26
"...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 个评论
dpb
dpb 2019-12-26
"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 2019-12-26
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by