Why does feval produce the error 'cannot automatically convert a double variable to categorical values'?
29 views (last 30 days)
Show older comments
David Bruce
on 5 Dec 2024 at 23:11
Commented: David Bruce
on 6 Dec 2024 at 19:33
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 Comment
Walter Roberson
on 5 Dec 2024 at 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.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)
Accepted Answer
Walter Roberson
on 5 Dec 2024 at 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 Comments
Walter Roberson
on 5 Dec 2024 at 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.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,'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)
More Answers (0)
See Also
Categories
Find more on WLAN Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!