In order to automate the fitlm command I need to select numerical data from a matrix, and matching variable names from another character array. I get an error on fitlm command

9 visualizaciones (últimos 30 días)
% Test script which shows the problem
% Input Data correctly entered in a table
% X and Y are number arrays as shown in table below
X=[114.6986
130.8867
137.2275
158.3874
164.9807
150.4959
153.5719
162.3315
150.4818
150.6757
126.0978
146.8826
108.8821
140.6874
141.9079
141.3292
147.5428
178.0340
147.4099
146.6944
149.0788
136.2100
120.1847
139.2640
130.8207
151.0124
126.2979
128.7861
150.1999
145.1715
124.0876
162.7852
150.6057
110.8681
127.9843
141.6658
135.8723
140.0876
160.0989
124.4767];
Y= [90.1289
113.7593
132.9390
122.6130
136.6224
123.4329
126.8783
157.6137
145.6993
129.0664
111.1469
138.5979
97.5274
134.1083
124.0515
120.4644
140.7517
170.4020
122.8751
127.7062
133.2769
107.5278
119.6132
111.1775
122.9596
143.7457
113.7101
110.3727
132.7626
119.9252
118.1571
149.1560
134.6609
90.6230
108.7946
135.4210
122.6799
103.4755
140.5454
95.2746];
% Variable names have embedded spaces. Example below!
VarNames(4,1:15)='Data 1 '
VarNames = 4×15 char array
' ' ' ' ' ' 'Data 1 '
VarNames(7,1:15)='Data 2 '
VarNames = 7×15 char array
' ' ' ' ' ' 'Data 1 ' ' ' ' ' 'Data 2 '
% Produce the Table
tbl=table(X,Y,'VariableNames',{erase(strtrim(VarNames(4,:))," "),erase(strtrim(VarNames(7,:))," ")})
tbl = 40×2 table
Data1 Data2 ______ ______ 114.7 90.129 130.89 113.76 137.23 132.94 158.39 122.61 164.98 136.62 150.5 123.43 153.57 126.88 162.33 157.61 150.48 145.7 150.68 129.07 126.1 111.15 146.88 138.6 108.88 97.527 140.69 134.11 141.91 124.05 141.33 120.46
% Fit linear regression line
lm=fitlm(tbl,'erase(strtrim(VarNames(OriColumns(i,1),:))," ") ~ erase(strtrim(VarNames(OriColumns(i,2),:))," ")')
Error using classreg.regr.FormulaProcessor>parseStr
Unable to understand the character vector or string scalar 'erase(strtrim(VarNames(OriColumns(i,1),:))," ") ~ erase(strtrim(VarNames(OriColumns(i,2),:))," ")'.

Error in classreg.regr.FormulaProcessor (line 376)
[f.str,f.tree] = parseStr(f,modelSpec);

Error in classreg.regr.LinearFormula (line 48)
f = f@classreg.regr.FormulaProcessor(varargin{:});

Error in classreg.regr.TermsRegression.createFormula (line 720)
formula = classreg.regr.LinearFormula(modelDef,varNames,'',intercept,link);

Error in LinearModel.createFormula (line 1076)
formula = classreg.regr.TermsRegression.createFormula(supplied,modelDef, ...

Error in LinearModel.fit (line 1058)
model.Formula = LinearModel.createFormula(supplied,modelDef,X, ...

Error in fitlm (line 121)
model = LinearModel.fit(X,varargin{:});
fignum=fignum+1;
figure(fignum)
orient Landscape;
set(gcf,'units', 'normalized', 'outerposition', [0 0 1 1]);
plot(lm)

Respuestas (3)

Cris LaPierre
Cris LaPierre el 16 de Mayo de 2023
You haven't share what the values of OriColumns and i are, but either way, you have a character array. It is no longer an executable command. It's just letters and words.
My recommendation would be to capture the result of your expression in a variable before you call fitlm, and pass that variable in as the 2nd input to fitlm.
% Test script which shows the problem
% Input Data correctly entered in a table
% X and Y are number arrays as shown in table below
X=[114.6986
130.8867
137.2275
158.3874
164.9807
150.4959
153.5719
162.3315
150.4818
150.6757
126.0978
146.8826
108.8821
140.6874
141.9079
141.3292
147.5428
178.0340
147.4099
146.6944
149.0788
136.2100
120.1847
139.2640
130.8207
151.0124
126.2979
128.7861
150.1999
145.1715
124.0876
162.7852
150.6057
110.8681
127.9843
141.6658
135.8723
140.0876
160.0989
124.4767];
Y= [90.1289
113.7593
132.9390
122.6130
136.6224
123.4329
126.8783
157.6137
145.6993
129.0664
111.1469
138.5979
97.5274
134.1083
124.0515
120.4644
140.7517
170.4020
122.8751
127.7062
133.2769
107.5278
119.6132
111.1775
122.9596
143.7457
113.7101
110.3727
132.7626
119.9252
118.1571
149.1560
134.6609
90.6230
108.7946
135.4210
122.6799
103.4755
140.5454
95.2746];
% Variable names have embedded spaces. Example below!
VarNames(4,1:15)='Data 1 '
VarNames = 4×15 char array
' ' ' ' ' ' 'Data 1 '
VarNames(7,1:15)='Data 2 '
VarNames = 7×15 char array
' ' ' ' ' ' 'Data 1 ' ' ' ' ' 'Data 2 '
% Produce the Table
tbl=table(X,Y,'VariableNames',{erase(strtrim(VarNames(4,:))," "),erase(strtrim(VarNames(7,:))," ")})
tbl = 40×2 table
Data1 Data2 ______ ______ 114.7 90.129 130.89 113.76 137.23 132.94 158.39 122.61 164.98 136.62 150.5 123.43 153.57 126.88 162.33 157.61 150.48 145.7 150.68 129.07 126.1 111.15 146.88 138.6 108.88 97.527 140.69 134.11 141.91 124.05 141.33 120.46
% Fit linear regression line
i = 1;
OriColumns(1,1) = 4;
OriColumns(1,2) = 7;
modelSpec = erase(strtrim(VarNames(OriColumns(i,1),:))," ") + " ~ " + erase(strtrim(VarNames(OriColumns(i,2),:))," ");
lm=fitlm(tbl,modelSpec)
lm =
Linear regression model: Data1 ~ 1 + Data2 Estimated Coefficients: Estimate SE tStat pValue ________ ________ ______ __________ (Intercept) 48.603 9.4635 5.1359 8.6783e-06 Data2 0.74507 0.075292 9.8958 4.5642e-12 Number of observations: 40, Error degrees of freedom: 38 Root Mean Squared Error: 8.2 R-squared: 0.72, Adjusted R-Squared: 0.713 F-statistic vs. constant model: 97.9, p-value = 4.56e-12
fignum=1;
figure(fignum)
orient Landscape;
set(gcf,'units', 'normalized', 'outerposition', [0 0 1 1]);
plot(lm)

the cyclist
the cyclist el 16 de Mayo de 2023
One way that is close to the syntax you used is this:
lm=fitlm(tbl,sprintf('%s ~ %s',erase(strtrim(VarNames(OriColumns(i,1),:))," "),erase(strtrim(VarNames(OriColumns(i,2),:))," ")))

Branko Celler
Branko Celler el 17 de Mayo de 2023
Thank you! That worked.
Regards
Branko

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by