Why does feval produce the error 'cannot automatically convert a double variable to categorical values'?
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
David Bruce
el 5 de Dic. de 2024 a las 23:11
Comentada: David Bruce
el 6 de Dic. de 2024 a las 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 comentario
Walter Roberson
el 5 de Dic. de 2024 a las 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)
Respuesta aceptada
Walter Roberson
el 5 de Dic. de 2024 a las 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 comentarios
Walter Roberson
el 5 de Dic. de 2024 a las 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre WLAN Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!