Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
mnrfit, mnrvalue

Subject: mnrfit, mnrvalue

From: Titus Adelani

Date: 11 Jul, 2009 15:11:03

Message: 1 of 8

Can anyone provide me a good example on how to use mnrfit and mnrval in MATLAB, especially how to build the matrices X and Y? It does look like it's a bit different from the matrices used for general linear regression.

Thank you

Subject: mnrfit, mnrvalue

From: Peter Perkins

Date: 13 Jul, 2009 17:04:00

Message: 2 of 8

Titus Adelani wrote:
> Can anyone provide me a good example on how to use mnrfit and mnrval in MATLAB, especially how to build the matrices X and Y? It does look like it's a bit different from the matrices used for general linear regression.

Titus, here's a fairly simple example that attemtps to illustrate the difference between fitting a nominal model and fitting an ordinal model. Hope it helps.


    Example: Fit multinomial logistic regression models to data with one
    predictor variable and three categories in the response variable.
 
      x = [-3 -2 -1 0 1 2 3]';
      Y = [1 11 13; 2 9 14; 6 14 5; 5 10 10; 5 14 6; 7 13 5; 8 11 6];
      bar(x,Y,'stacked'); ylim([0 25]);
 
      % Fit a nominal model for the individual response category probabilities,
      % with separate slopes on the single predictor variable, x, for each
      % category. The first row of betaHatNom contains the intercept terms for
      % the first two response categories. The second row contains the slopes.
      betaHatNom = mnrfit(x,Y,'model','nominal','interactions','on')
 
      % Compute the predicted probabilities for the three response categories.
      xx = linspace(-4,4)';
      pHatNom = mnrval(betaHatNom,xx,'model','nominal','interactions','on');
      line(xx,cumsum(25*pHatNom,2),'LineWidth',2);
 
      % Fit a "parallel" ordinal model for the cumulative response category
      % probabilities, with a common slope on the single predictor variable, x,
      % across all categories. The first two elements of betaHatOrd are the
      % intercept terms for the first two response categories. The last element
      % of betaHatOrd is the common slope.
      betaHatOrd = mnrfit(x,Y,'model','ordinal','interactions','off')
 
      % Compute the predicted cumulative probabilities for the first two response
      % categories. The cumulative probability for the third category is always 1.
      pHatOrd = mnrval(betaHatOrd,xx,'type','cumulative','model','ordinal','interactions','off');
      bar(x,cumsum(Y,2),'grouped'); ylim([0 25]);
      line(xx,25*pHatOrd,'LineWidth',2);

Subject: mnrfit, mnrvalue

From: Ugur

Date: 20 Oct, 2009 19:11:06

Message: 3 of 8

Thanks for your help Peter. I have a related question.

Is it possible to have some coefficients category specific (other than the constant term) and some not ? Some call this "McFadden's conditional logit".


Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <h3fpe0$huj$1@fred.mathworks.com>...
> Titus Adelani wrote:
> > Can anyone provide me a good example on how to use mnrfit and mnrval in MATLAB, especially how to build the matrices X and Y? It does look like it's a bit different from the matrices used for general linear regression.
>
> Titus, here's a fairly simple example that attemtps to illustrate the difference between fitting a nominal model and fitting an ordinal model. Hope it helps.
>
>
> Example: Fit multinomial logistic regression models to data with one
> predictor variable and three categories in the response variable.
>
> x = [-3 -2 -1 0 1 2 3]';
> Y = [1 11 13; 2 9 14; 6 14 5; 5 10 10; 5 14 6; 7 13 5; 8 11 6];
> bar(x,Y,'stacked'); ylim([0 25]);
>
> % Fit a nominal model for the individual response category probabilities,
> % with separate slopes on the single predictor variable, x, for each
> % category. The first row of betaHatNom contains the intercept terms for
> % the first two response categories. The second row contains the slopes.
> betaHatNom = mnrfit(x,Y,'model','nominal','interactions','on')
>
> % Compute the predicted probabilities for the three response categories.
> xx = linspace(-4,4)';
> pHatNom = mnrval(betaHatNom,xx,'model','nominal','interactions','on');
> line(xx,cumsum(25*pHatNom,2),'LineWidth',2);
>
> % Fit a "parallel" ordinal model for the cumulative response category
> % probabilities, with a common slope on the single predictor variable, x,
> % across all categories. The first two elements of betaHatOrd are the
> % intercept terms for the first two response categories. The last element
> % of betaHatOrd is the common slope.
> betaHatOrd = mnrfit(x,Y,'model','ordinal','interactions','off')
>
> % Compute the predicted cumulative probabilities for the first two response
> % categories. The cumulative probability for the third category is always 1.
> pHatOrd = mnrval(betaHatOrd,xx,'type','cumulative','model','ordinal','interactions','off');
> bar(x,cumsum(Y,2),'grouped'); ylim([0 25]);
> line(xx,25*pHatOrd,'LineWidth',2);

Subject: mnrfit, mnrvalue

From: Ankit Kumar

Date: 29 Jul, 2010 11:37:05

Message: 4 of 8

Hi
I am trying to apply logit model to get coef:

I have in following format

y = [ 1 0 0 1 0 1]

x (x1 c2 x3) = [ 2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9]

y= a1*x 1 + a2*x2 + a3*x3

I would like to get a1, a2, a3 I think mnrfit, glmfit should be helpful but I could not get this done, could you guide pls.

Thanks
A

Subject: mnrfit, mnrvalue

From: Peter Perkins

Date: 29 Jul, 2010 12:50:10

Message: 5 of 8

On 7/29/2010 7:37 AM, Ankit Kumar wrote:
> y= a1*x 1 + a2*x2 + a3*x3

Withiout an intercept term?

> I would like to get a1, a2, a3 I think mnrfit, glmfit should be helpful
> but I could not get this done, could you guide pls.

What have you done so far?

Subject: mnrfit, mnrvalue

From: Ankit Kumar

Date: 30 Jul, 2010 05:04:04

Message: 6 of 8

Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <i2rte2$h9d$1@fred.mathworks.com>...
> On 7/29/2010 7:37 AM, Ankit Kumar wrote:
> > y= a1*x 1 + a2*x2 + a3*x3
>
> Withiout an intercept term?
>
> > I would like to get a1, a2, a3 I think mnrfit, glmfit should be helpful
> > but I could not get this done, could you guide pls.
>
> What have you done so far?

Hi Peter

Thanks for your response.

Withiout an intercept term?
Yeah, we can have intercept term.

What have you done so far?
In terms of modelling, it is simple for me as I will feed in data from excel and get the result in excel to use it further, therefore from MATLABB perspective it is just about feeding in data into the function and use the direct result (from 0 to 1 showing probablity and error items).

Pls tell me if it is enough or you need me to furnish more information.
 
Thanks
A

Subject: mnrfit, mnrvalue

From: Peter Perkins

Date: 30 Jul, 2010 12:25:51

Message: 7 of 8

On 7/30/2010 1:04 AM, Ankit Kumar wrote:
> What have you done so far?
> In terms of modelling, it is simple for me as I will feed in data from
> excel and get the result in excel to use it further, therefore from
> MATLABB perspective it is just about feeding in data into the function
> and use the direct result (from 0 to 1 showing probablity and error items).

What problems are you having with using GLMFIT?

Subject: mnrfit, mnrvalue

From: Ankit Kumar

Date: 2 Aug, 2010 07:32:07

Message: 8 of 8

Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <i2ugcf$7nl$1@fred.mathworks.com>...
> On 7/30/2010 1:04 AM, Ankit Kumar wrote:
> > What have you done so far?
> > In terms of modelling, it is simple for me as I will feed in data from
> > excel and get the result in excel to use it further, therefore from
> > MATLABB perspective it is just about feeding in data into the function
> > and use the direct result (from 0 to 1 showing probablity and error items).
>
> What problems are you having with using GLMFIT?

Hi Peter

Would like to know how to use the GLMFIT function for the data arranged in the fashion

y = [ 1 0 0 1 0 1]

x (x1 c2 x3) = [ 2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9]

and need to find y=a0+ a1*x 1 + a2*x2 + a3*x3

because data given in the sample

"x = [2100 2300 2500 2700 2900 3100 ...
     3300 3500 3700 3900 4100 4300]';
n = [48 42 31 34 31 21 23 23 21 16 17 21]';
y = [1 2 0 3 8 8 14 17 19 15 17 21]';
b = glmfit(x,[y n],'binomial','link','probit');"

appear/arranged differently.

Thanks
Ankit

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us