You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Matlab mysterious new error with fitglm and fitlm
2 views (last 30 days)
Show older comments
Hello! What could be going on with this bug? Any help would be appreciated. I used to be able to run fitglm and fitlm in Matlab 2019, but now I get the following errror, including when I use the piece of code from the help documenthttps://www.mathworks.com/help/stats/fitglm.html:
Error using matlab.internal.datatypes.throwInstead (line 7)
The class table has no Constant property or Static method named 'fromScalarStruct'.
Error in struct2table (line 68)
matlab.internal.datatypes.throwInstead(ME,'MATLAB:table:UnequalFieldLengths',message('MATLAB:struct2table:UnequalFieldLengths'));
Error in dataset2table (line 13)
t = struct2table(s,'AsArray',false);
Error in classreg.regr.TermsRegression/handleDataArgs (line 525)
X = dataset2table(X);
Error in LinearModel.fit (line 1184)
[X,y,haveDataset,otherArgs] = LinearModel.handleDataArgs(X,varargin{:});
Error in fitlm (line 121)
model = LinearModel.fit(X,varargin{:});
Error in fitlm_try_debug (line 4)
lm = fitlm(d,'MPG ~ Year + Weight + Weight^2')
21 Comments
Andra Mihali
on 18 Dec 2020
Also I could provide some subset of my data if necessary, but i was wondering why I get the same error even with the standard dataset from the Matlab helper file.
Image Analyst
on 18 Dec 2020
Yes, please provide the data. Also, your link does not work. Please provide your code that was pulled from the help documentation so we can run it, plus the code that you modified to work with your data.
Andra Mihali
on 18 Dec 2020
Edited: Walter Roberson
on 19 Dec 2020
Code:
load carsmall
d = table(MPG,Weight);
d.Year = ordinal(Model_Year);
lm = fitlm(d,'MPG ~ Year + Weight + Weight^2')
Will provide my code as well in a bit, but meanwhile the code from the help yields the error I copy-pasted above:(
Image Analyst
on 18 Dec 2020
It saves time if you read the posting guidelines on the question submission form and fill out all the fields. I'll fill out the Release for you on the right hand side of this page.
This runs fine for me in R2020b
load carsmall
d = table(MPG,Weight);
d.Year = ordinal(Model_Year);
lm = fitlm(d,'MPG ~ Year + Weight + Weight^2')
I get
lm =
Linear regression model:
MPG ~ 1 + Weight + Year + Weight^2
Estimated Coefficients:
Estimate SE tStat pValue
___________________ ____________________ _________________ ____________________
(Intercept) 54.2062873729581 4.71166738346461 11.5046931290593 2.66480529582129e-19
Weight -0.0164036290463721 0.0031249122854486 -5.24930863587977 1.02828937448822e-06
Year_76 2.08865700045782 0.714914020475329 2.92154992158236 0.00441367253657404
Year_82 8.18639613482828 0.815306703812534 10.0408792133648 2.63644864390097e-16
Weight^2 1.5573239444627e-06 4.94540084493951e-07 3.14903481697801 0.00223027408350944
Number of observations: 94, Error degrees of freedom: 89
Root Mean Squared Error: 2.78
R-squared: 0.885, Adjusted R-Squared: 0.88
F-statistic vs. constant model: 172, p-value = 5.52e-41
Perhaps your d is different. Can you attach it in a .mat file?
What does this say:
whos d
Mine says
Name Size Bytes Class Attributes
d 100x2 2802 table
Andra Mihali
on 18 Dec 2020
Edited: Andra Mihali
on 18 Dec 2020
Thank you so much! Am downloading 2020b now. The error is the same as before. Indeed my d is different:
whos d
Name Size Bytes Class Attributes
d 100x3 3918 table
Adam Danz
on 18 Dec 2020
Image Analyst and I must have filled in your Product/Release data at the same time!
I just tested the same commands in r17b and there were no errors. Please attach a mat file containing your inputs to fitlm.
Adam Danz
on 18 Dec 2020
It should work in 19a, too.
What's in the table?
Could you show us the results of
head(d)
varfun(@class,d)
Walter Roberson
on 19 Dec 2020
I have the suspicion that you have a function that is overshadowing the Mathworks version, but at the moment I do not know which function that might be. Experiment with
restoredefaultpath
rehash toolboxcache
and see if the code works properly. If it does then something that you have added to your path is interfering.
Ive J
on 19 Dec 2020
Edited: Ive J
on 19 Dec 2020
Maybe the error comes from
Error in classreg.regr.TermsRegression/handleDataArgs (line 525)
X = dataset2table(X);
Your MATLAB treats d as being a dataset, while you just showed d is a table (can you set a break point and verify if d indeed is treated as a table?)
Also as Adam pointed out, your struct2table throws an error on line 68, while line 83 takes care of this error in R2020b and also mine, R2019b. I couldn't track down any changes to this function in release notes.
load carsmall
d = table(MPG,Weight);
d.Year = ordinal(Model_Year);
lm = fitlm(d,'MPG ~ Year + Weight + Weight^2'); % no error is thrown in R2019b
whos d
Name Size Bytes Class Attributes
d 100x3 3556 table
Maybe you can check also this.
which struct2table -all
Andra Mihali
on 19 Dec 2020
Thank you all so much for all your useful suggestions! Downloading 2020b fixed it, but going back to 2019a was still not running. Walter's suggestion fixed it also for 2019a:)
restoredefaultpath
rehash toolboxcache
Andra Mihali
on 19 Dec 2020
Adam, btw the output to what you suggested was:
head(d)
Undefined function 'head' for input arguments of type 'table'.
>> varfun(@class,d)
Undefined function 'varfun' for input arguments of type 'table'.
Image Analyst
on 19 Dec 2020
Andra, head() was added in R2016b and you have R2019a so you should have head(). What does this say:
which -all head
Andra Mihali
on 20 Dec 2020
Edited: Andra Mihali
on 20 Dec 2020
Post restoring the defaults with
restoredefaultpath
rehash toolboxcache
I get:
which -all head
/Applications/MATLAB_R2019a.app/toolbox/matlab/datatypes/@tabular/head.m % tabular method
/Applications/MATLAB_R2019a.app/toolbox/matlab/bigdata/@tall/head.m % tall method
/Applications/MATLAB_R2019a.app/toolbox/distcomp/parallel/@codistributed/head.m % codistributed method
/Applications/MATLAB_R2019a.app/toolbox/distcomp/gpu/@gpuArray/head.m
Also, to your previous suggestion:
head(d)
ans =
8×3 table
MPG Weight Year
___ ______ ____
18 3504 70
15 3693 70
18 3436 70
16 3433 70
17 3449 70
15 4341 70
14 4354 70
14 4312 70
varfun(@class, d)
ans =
1×3 table
class_MPG class_Weight class_Year
_________ ____________ __________
double double ordinal
Adam Danz
on 20 Dec 2020
...and varfun was introduced in r2013b. Didn't WR's suggestion to restore the path and toolbox cache fix that?
Andra Mihali
on 20 Dec 2020
Edited: Andra Mihali
on 20 Dec 2020
Indeed, WR's suggestion fixed it (my first answer was from before restoring the path and toolboxcache). Thank you all again, relieved now:)
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)