How does one use output from mnrfit to forecast nominal values

6 次查看(过去 30 天)
Hello: I have been trying to understand how one can use the model produced by mnrfit to forecast nominal values. Consider the example Maximum Likelihood-Multinomial Probit Model answered by Tom Lane on 12 Jun 2011. The example produces a model, called b1. How does one use b and x to generate a 1000 x 1 array of ordinal values yhat?

回答(1 个)

Tom Lane
Tom Lane 2011-11-15
You can use the mnrval function to compute fitted probabilities, then take the category with maximum probability. Here's an example:
% Suppose we have continuous x and three-valued y. Generate y at random.
y = randi(3,200,1);
x = zeros(size(y));
% Things with y=1 tend to have x values near zero.
n1 = sum(y==1);
x(y==1) = randn(n1,1)/2;
% Things with y=2 tend to have x near 3; and y=3 has x near 2
n2 = sum(y==2);
x(y==2) = 3 + randn(n2,1);
n3 = sum(y==3);
x(y==3) = 2 + randn(n3,1);
% Fit a multinomial model for y as a function of x.
b = mnrfit(x,y,'model','nominal');
% Look at the probabilities for the three categories as a function of x.
xx = linspace(min(x),max(x))';
pp = mnrval(b,xx,'model','nominal');
plot(xx,pp)
legend('Prob(y=1)','Prob(y=2)','Prob(y=3)','location','best')
% Generate predictions yyy at ten x values.
xxx = 4*rand(10,1);
ppp = mnrval(b,xxx,'model','nominal');
[maxp,yyy] = max(ppp,[],2);
t = (yyy==1); line(xxx(t),maxp(t),'color','b','marker','o','linestyle','none')
t = (yyy==2); line(xxx(t),maxp(t),'color','g','marker','s','linestyle','none')
t = (yyy==3); line(xxx(t),maxp(t),'color','r','marker','v','linestyle','none')

Community Treasure Hunt

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

Start Hunting!

Translated by