Why does feval produce the error 'cannot automatically convert a double variable to categorical values'?

29 ビュー (過去 30 日間)
Hi,
I'm trying to fit a linear regression with a categorical and a continuous variable as dependent variables, and a continuous variable as the response. I then wanted to plot those on a scatter, with the regression lines drawn on. I was following the example given on this page: https://uk.mathworks.com/help/stats/regression-with-categorical-covariates.html
While that code works, mine doesn't seem to - I can create the model, plot the scatter, but when it comes to plotting the fitted regression lines, I get several errors, the first being 'cannot automatically convert a double variable to categorical values'.
Thanks in advance for your help!
This is my code:
%create the model
tbl=array2table(sctrdata(:,2:4),'VariableNames',{'time','proms','stat'})
tbl.time=categorical(tbl.time)
lm=fitlm(tbl,'proms~stat+time')
%plot the data
w = linspace(min(sctrdata(:,4)),max(sctrdata(:,4)));
figure()
gscatter(sctrdata(:,4),sctrdata(:,3),sctrdata(:,2),'bgr','x.o')
line(w,feval(lm,w,'1'),'Color','b','LineWidth',2)
line(w,feval(lm,w,'2'),'Color','g','LineWidth',2)
line(w,feval(lm,w,'3'),'Color','r','LineWidth',2)
This is the 18x4 double sctrdata:
202 1 35 7.35290974607409
204 1 15 16.1392276652092
207 1 29 8.61536130247136
210 1 30 18.6708615548317
213 1 16 18.6747515545782
214 1 24 7.42659245084273
202 2 30 5.65164424693076
204 2 10 9.95657468999142
210 2 32 15.3576732431412
213 2 35 7.39980077321328
207 2 NaN NaN
214 2 NaN NaN
204 3 25 12.1170053044237
210 3 43 4.81401465834751
213 3 46 11.3542368593944
202 3 NaN NaN
207 3 NaN NaN
214 3 NaN NaN
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 5 日 23:28
sctrdata = [
202 1 35 7.35290974607409
204 1 15 16.1392276652092
207 1 29 8.61536130247136
210 1 30 18.6708615548317
213 1 16 18.6747515545782
214 1 24 7.42659245084273
202 2 30 5.65164424693076
204 2 10 9.95657468999142
210 2 32 15.3576732431412
213 2 35 7.39980077321328
207 2 NaN NaN
214 2 NaN NaN
204 3 25 12.1170053044237
210 3 43 4.81401465834751
213 3 46 11.3542368593944
202 3 NaN NaN
207 3 NaN NaN
214 3 NaN NaN];
%create the model
tbl=array2table(sctrdata(:,2:4),'VariableNames',{'time','proms','stat'})
tbl = 18x3 table
time proms stat ____ _____ ______ 1 35 7.3529 1 15 16.139 1 29 8.6154 1 30 18.671 1 16 18.675 1 24 7.4266 2 30 5.6516 2 10 9.9566 2 32 15.358 2 35 7.3998 2 NaN NaN 2 NaN NaN 3 25 12.117 3 43 4.814 3 46 11.354 3 NaN NaN
tbl.time=categorical(tbl.time)
tbl = 18x3 table
time proms stat ____ _____ ______ 1 35 7.3529 1 15 16.139 1 29 8.6154 1 30 18.671 1 16 18.675 1 24 7.4266 2 30 5.6516 2 10 9.9566 2 32 15.358 2 35 7.3998 2 NaN NaN 2 NaN NaN 3 25 12.117 3 43 4.814 3 46 11.354 3 NaN NaN
lm=fitlm(tbl,'proms~stat+time')
lm =
Linear regression model: proms ~ 1 + time + stat Estimated Coefficients: Estimate SE tStat pValue ________ _______ _________ _________ (Intercept) 33.94 8.9073 3.8104 0.0041509 time_2 -0.37315 6.5696 -0.056798 0.95595 time_3 10.761 7.1697 1.5009 0.16762 stat -0.71071 0.62282 -1.1411 0.28327 Number of observations: 13, Error degrees of freedom: 9 Root Mean Squared Error: 9.69 R-squared: 0.365, Adjusted R-Squared: 0.153 F-statistic vs. constant model: 1.72, p-value = 0.231
%plot the data
w = linspace(min(sctrdata(:,4)),max(sctrdata(:,4)));
figure()
gscatter(sctrdata(:,4),sctrdata(:,3),sctrdata(:,2),'bgr','x.o')
line(w,feval(lm,w,'1'),'Color','b','LineWidth',2)
Error using classreg.regr.modelutils.designmatrix>convertVar (line 505)
Cannot automatically convert a double variable to categorical values.

Error in classreg.regr.modelutils.designmatrix>dummyVars (line 437)
group = convertVar(group,glevels); % convert group to be compatible with glevels

Error in classreg.regr.modelutils.designmatrix (line 281)
[Xj,dummynames] = dummyVars(dummyCoding{j},Xj,catLevels{j});

Error in classreg.regr.CompactTermsRegression/designMatrix (line 142)
= classreg.regr.modelutils.designmatrix(X,'Model',terms(:,varLocs), ...

Error in LinearModel/predict (line 365)
design = designMatrix(model,Xpred);

Error in classreg.regr.CompactPredictor/feval (line 110)
yPred = reshape(predict(model,Xpred),sizeOut);
line(w,feval(lm,w,'2'),'Color','g','LineWidth',2)
line(w,feval(lm,w,'3'),'Color','r','LineWidth',2)

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2024 年 12 月 5 日 23:35
You design your tbl so that the first column is categorical.
You construct a fitlm model from that.
You attempt to feval() the model passing in a numeric value first and a character vector second. The numeric value is constructed from values based on column 4 of the original data.
Numeric values do not match the datatype of the first model parameter, which is categorical.
You should try
line(w,feval(lm,'1', w),'Color','b','LineWidth',2)
  2 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 5 日 23:36
sctrdata = [
202 1 35 7.35290974607409
204 1 15 16.1392276652092
207 1 29 8.61536130247136
210 1 30 18.6708615548317
213 1 16 18.6747515545782
214 1 24 7.42659245084273
202 2 30 5.65164424693076
204 2 10 9.95657468999142
210 2 32 15.3576732431412
213 2 35 7.39980077321328
207 2 NaN NaN
214 2 NaN NaN
204 3 25 12.1170053044237
210 3 43 4.81401465834751
213 3 46 11.3542368593944
202 3 NaN NaN
207 3 NaN NaN
214 3 NaN NaN];
%create the model
tbl=array2table(sctrdata(:,2:4),'VariableNames',{'time','proms','stat'})
tbl = 18x3 table
time proms stat ____ _____ ______ 1 35 7.3529 1 15 16.139 1 29 8.6154 1 30 18.671 1 16 18.675 1 24 7.4266 2 30 5.6516 2 10 9.9566 2 32 15.358 2 35 7.3998 2 NaN NaN 2 NaN NaN 3 25 12.117 3 43 4.814 3 46 11.354 3 NaN NaN
tbl.time=categorical(tbl.time)
tbl = 18x3 table
time proms stat ____ _____ ______ 1 35 7.3529 1 15 16.139 1 29 8.6154 1 30 18.671 1 16 18.675 1 24 7.4266 2 30 5.6516 2 10 9.9566 2 32 15.358 2 35 7.3998 2 NaN NaN 2 NaN NaN 3 25 12.117 3 43 4.814 3 46 11.354 3 NaN NaN
lm=fitlm(tbl,'proms~stat+time')
lm =
Linear regression model: proms ~ 1 + time + stat Estimated Coefficients: Estimate SE tStat pValue ________ _______ _________ _________ (Intercept) 33.94 8.9073 3.8104 0.0041509 time_2 -0.37315 6.5696 -0.056798 0.95595 time_3 10.761 7.1697 1.5009 0.16762 stat -0.71071 0.62282 -1.1411 0.28327 Number of observations: 13, Error degrees of freedom: 9 Root Mean Squared Error: 9.69 R-squared: 0.365, Adjusted R-Squared: 0.153 F-statistic vs. constant model: 1.72, p-value = 0.231
%plot the data
w = linspace(min(sctrdata(:,4)),max(sctrdata(:,4)));
figure()
gscatter(sctrdata(:,4),sctrdata(:,3),sctrdata(:,2),'bgr','x.o')
line(w,feval(lm,'1',w),'Color','b','LineWidth',2)
line(w,feval(lm,'2',w),'Color','g','LineWidth',2)
line(w,feval(lm,'3',w),'Color','r','LineWidth',2)
David Bruce
David Bruce 2024 年 12 月 6 日 19:33
Ah, I see now. Thank you so much Walter and for responding so quickly!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWLAN Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by