Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Matrix rows and columns in McDonald and Moffit calculation

    Hi,

    Please see below an example McDonald and Moffit calculation:


    gen one=1
    mkmat whrs, matrix(y)
    mkmat one, matrix(ones)
    mkmat one kl6 k618 wa we, matrix(X)
    matrix Xb=inv(ones'*ones)*ones'*X
    matrix b = e(b)
    matrix beta = b[1,5],b[1,1], b[1,2],b[1,3],b[1,4]
    matrix BXoverS=Xb * beta'/b[1,6]
    scalar Z=el(BXoverS,1,1)
    scalar Mu=normalden(Z)/normal(Z)
    scalar P=normal(Z)
    scalar P1=P*(1-Z*Mu-Mu^2)
    scalar P2=normalden(Z*Z+normalden(Z)*Mu)
    display Mu
    display P
    display P1
    display P2

    My question is specifically on this command: matrix beta = b[1,5],b[1,1], b[1,2],b[1,3],b[1,4]. I'd like to know how the figures in the example command were determined. I understand that figures represent rows and columns for elements of the matrix b, but how do I determine the number and ordering of the elements?

    I hope someone can assist.

  • #2
    From the code, it appears that you select \(k\)−1 coefficients from the matrix of coefficients, e(b), and normalize these by dividing by the \(k\)th coefficient, where \(k\) is the number of slope coefficients. I don't think the order should matter, but you can test this by trying out different orderings. There is no way to tell what coefficients are in e(b) apart from directly examining the matrix or looking at the regression command.

    Code:
    matrix list e(b)
    Last edited by Andrew Musau; 06 Jan 2024, 04:14.

    Comment


    • #3
      Thank you so much Andrew. I seem to be getting an error message when I move to the command. Would you know where the issue is?

      code:
      matrix list e(b)

      e(b)[1,26]
      aware_count: aware_count: aware_count: aware_count: aware_count: aware_count: aware_count:
      0b. 1. 2. 3. 0. 1b. 2.
      age_catego~s age_catego~s age_catego~s age_catego~s education education education
      y1 0 -.20132751 -.6625963 -.87057099 .1804277 0 .2116552

      aware_count: aware_count: aware_count: aware_count: aware_count: aware_count: aware_count:
      3. 4. 0. 1. 2. 3. 4.
      education education income income income income income
      y1 .10575347 .09571243 -.23376885 -.18024522 -.26007125 -.16936466 -.10334494

      aware_count: aware_count: aware_count: aware_count: aware_count: aware_count: aware_count:
      5. 6b. 0b. 1. 2. 4.
      income income gender~emale poli~t_other poli~t_other poli~t_other poli~t_other
      y1 -.05831255 0 .0136834 0 -.03834463 .10754739 .25653429

      aware_count: aware_count: aware_count: aware_count: /:

      gn_sus_q1 gn_sus_q3 gn_sus_q4 _cons var(e.awa~nt)
      y1 -.60788258 .2009674 .51922832 1.991675 1.389505

      . matrix beta = b[1,25],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12]
      > ,b[1,13],b[1,14],b[1,15],b[1,15],b[1,15],b[1,16],b[1,17],b[1,18],b[1,19],b[1,20],b[1,21],b[1,22],b[1,23],b[1,
      > 24]

      . matrix BXoverS=Xb * beta'/b[1,26]
      conformability error
      r(503);

      Comment


      • #4
        From what I figured, it is because the rows and columns of the matrices Xb and beta that I'm trying to multiply are not aligned. How do I fix this?

        Comment


        • #5
          Before trying to replicate the code, you should take the time to understand what it is doing. I am not familiar with this decomposition, so my comments come from examining the code in #1. My understanding is limited since I do not have access to the dataset and the estimation results that the code uses. If you have access to the code but not to the data and estimation results, find these so that you can fully understand the code. The conformability error is straightforward to diagnose. In the original code, the author creates an \(X\) matrix that is N \(×\) 5, where N is the sample size.

          mkmat one kl6 k618 wa we, matrix(X)

          The matrix beta is 1 \(\times\) 5 since the author takes the first five coefficients and divides by the sixth.

          matrix beta = b[1,5],b[1,1], b[1,2],b[1,3],b[1,4]
          By transposing the beta matrix, he is able to multiply it with the Xb matrix.

          matrix BXoverS=Xb * beta'/b[1,6]
          Also, note that in the coefficients matrix generated by tobit or regress, the last element is the coefficient on the constant term. So it is important to know which coefficient is used in the normalization.

          Comment


          • #6
            Thank you so much for your assistance Andrew, I think I got it. Only issue is now on the last 4 display commands. Nothing is displaying.

            Comment


            • #7
              I wouldn't know what the issue is without seeing your data and codes. Use matrix list to view the matrices and display to view the scalars that lead up to the definition of Mu, P, P1 and P2. This way, you can identify where the issue arises.

              Comment


              • #8
                [CODE] tobit aware_count age_35_54 age_55_74 age75_ education_no_high_school education_college_nodegree education_degree education_postgrad
                > gender_binary_female political_affiliation_dem political_affiliation_ind political_affiliation_not gn_sus_q1 gn_sus_q3 gn_sus_q4, l
                > l(0)

                Refining starting values:

                Grid node 0: Log likelihood = -3253.3592

                Fitting full model:

                Iteration 0: Log likelihood = -3253.3592
                Iteration 1: Log likelihood = -3163.0364
                Iteration 2: Log likelihood = -3158.9434
                Iteration 3: Log likelihood = -3158.915
                Iteration 4: Log likelihood = -3158.915

                Tobit regression Number of obs = 1,984
                Uncensored = 1,532
                Limits: Lower = 0 Left-censored = 452
                Upper = +inf Right-censored = 0

                LR chi2(14) = 498.33
                Prob > chi2 = 0.0000
                Log likelihood = -3158.915 Pseudo R2 = 0.0731

                --------------------------------------------------------------------------------------------
                aware_count | Coefficient Std. err. t P>|t| [95% conf. interval]
                ---------------------------+----------------------------------------------------------------
                age_35_54 | -.2339827 .0832773 -2.81 0.005 -.3973035 -.0706618
                age_55_74 | -.8769795 .0908383 -9.65 0.000 -1.055129 -.6988303
                age75_ | -1.209102 .1687665 -7.16 0.000 -1.540082 -.8781229
                education_no_high_school | .1204809 .1753226 0.69 0.492 -.2233563 .464318
                education_college_nodegree | .2832136 .0927022 3.06 0.002 .101409 .4650183
                education_degree | .1967588 .088696 2.22 0.027 .022811 .3707067
                education_postgrad | .2714768 .1144488 2.37 0.018 .0470233 .4959304
                gender_binary_female | .0215732 .0683191 0.32 0.752 -.112412 .1555584
                political_affiliation_dem | -.0343242 .0883404 -0.39 0.698 -.2075747 .1389263
                political_affiliation_ind | .164384 .0947906 1.73 0.083 -.0215164 .3502844
                political_affiliation_not | .2303417 .1272139 1.81 0.070 -.0191462 .4798295
                gn_sus_q1 | -.7852713 .0938499 -8.37 0.000 -.9693267 -.6012158
                gn_sus_q3 | .2486098 .1055488 2.36 0.019 .0416108 .4556088
                gn_sus_q4 | .6036098 .1044214 5.78 0.000 .3988218 .8083978
                _cons | 1.686088 .1220211 13.82 0.000 1.446784 1.925393
                ---------------------------+----------------------------------------------------------------
                var(e.aware_count)| 2.099159 .0798288 1.948296 2.261702
                --------------------------------------------------------------------------------------------

                .
                . gen one=1
                variable one already defined
                r(110);

                . mkmat aware_count, matrix(y)

                . mkmat one, matrix(ones)

                . mkmat one age_35_54 age_55_74 age75_ education_no_high_school education_college_nodegree education_degree education_postgrad gender_
                > binary_female political_affiliation_dem political_affiliation_ind political_affiliation_not gn_sus_q1 gn_sus_q3 gn_sus_q4, matrix(X)
                >

                .
                . matrix Xb=inv(ones'*ones)*ones'*X

                . matrix beta = b[1,15],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12],b[1,13],b[1,14]

                .
                . matrix beta = b[1,15],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12],b[1,13],b[1,14]

                . matrix BXoverS=Xb * beta'/b[1,16]

                . scalar Z=el(BXoverS,1,1)

                . scalar Mu=normalden(Z)/normal(Z)

                . scalar P=normal(Z)

                . scalar P1=P*(1-Z*Mu-Mu^2)

                .
                .
                . scalar P2=normalden(Z*Z+normalden(Z)*Mu)

                . display Mu
                .

                . display P
                .

                .
                . display P1
                .

                . display P2
                .

                /CODE]

                Comment


                • #9
                  Code:
                    matrix list e(b)
                  
                  e(b)[1,16]
                        aware_count:   aware_count:   aware_count:   aware_count:   aware_count:   aware_count:   aware_count:
                          age_35_54      age_55_74         age75_   e~no_high_~l   educ~odegree   educ~_degree   education_~d
                  y1     -.18891066     -.66473667     -.85717894      .09891194      .23891776      .17052525      .20320376
                  
                        aware_count:   aware_count:   aware_count:   aware_count:   aware_count:   aware_count:   aware_count:
                       gender~emale   political~em   political_~d   political_~t      gn_sus_q1      gn_sus_q3      gn_sus_q4
                  y1     -.00830027     -.04716346      .11752833      .19827956     -.58755837      .21870967       .5474535
                  
                        aware_count:             /:
                              _cons  var(e.awa~nt)
                  y1      1.8041728      1.3923365
                  Last edited by Teresa Mungazi; 18 Jan 2024, 15:02.

                  Comment


                  • #10
                    Code:
                     matrix list beta 
                    
                    beta[1,15]
                                c1          c2          c3          c4          c5          c6          c7          c8          c9
                    r1    .5474535  -.18891066  -.66473667  -.85717894   .09891194   .23891776   .17052525   .20320376  -.00830027
                    
                               c10         c11         c12         c13         c14         c15
                    r1  -.04716346   .11752833   .19827956  -.58755837   .21870967   .21870967

                    Comment


                    • #11
                      Code:
                       tobit aware_count age_35_54 age_55_74 age75_ education_no_high_school education_college_nodegree education_degree education_postgrad
                      >  gender_binary_female political_affiliation_dem political_affiliation_ind political_affiliation_not gn_sus_q1 gn_sus_q3 gn_sus_q4, l
                      > l(0)
                      
                      Refining starting values:
                      
                      Grid node 0:  Log likelihood = -3253.3592
                      
                      Fitting full model:
                      
                      Iteration 0:  Log likelihood = -3253.3592  
                      Iteration 1:  Log likelihood = -3163.0364  
                      Iteration 2:  Log likelihood = -3158.9434  
                      Iteration 3:  Log likelihood =  -3158.915  
                      Iteration 4:  Log likelihood =  -3158.915  
                      
                      Tobit regression                                    Number of obs     =  1,984
                                                                                 Uncensored =  1,532
                      Limits: Lower =    0                                    Left-censored =    452
                              Upper = +inf                                   Right-censored =      0
                      
                                                                          LR chi2(14)       = 498.33
                                                                          Prob > chi2       = 0.0000
                      Log likelihood = -3158.915                          Pseudo R2         = 0.0731
                      
                      --------------------------------------------------------------------------------------------
                                     aware_count | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
                      ---------------------------+----------------------------------------------------------------
                                       age_35_54 |  -.2339827   .0832773    -2.81   0.005    -.3973035   -.0706618
                                       age_55_74 |  -.8769795   .0908383    -9.65   0.000    -1.055129   -.6988303
                                          age75_ |  -1.209102   .1687665    -7.16   0.000    -1.540082   -.8781229
                        education_no_high_school |   .1204809   .1753226     0.69   0.492    -.2233563     .464318
                      education_college_nodegree |   .2832136   .0927022     3.06   0.002      .101409    .4650183
                                education_degree |   .1967588    .088696     2.22   0.027      .022811    .3707067
                              education_postgrad |   .2714768   .1144488     2.37   0.018     .0470233    .4959304
                            gender_binary_female |   .0215732   .0683191     0.32   0.752     -.112412    .1555584
                       political_affiliation_dem |  -.0343242   .0883404    -0.39   0.698    -.2075747    .1389263
                       political_affiliation_ind |    .164384   .0947906     1.73   0.083    -.0215164    .3502844
                       political_affiliation_not |   .2303417   .1272139     1.81   0.070    -.0191462    .4798295
                                       gn_sus_q1 |  -.7852713   .0938499    -8.37   0.000    -.9693267   -.6012158
                                       gn_sus_q3 |   .2486098   .1055488     2.36   0.019     .0416108    .4556088
                                       gn_sus_q4 |   .6036098   .1044214     5.78   0.000     .3988218    .8083978
                                           _cons |   1.686088   .1220211    13.82   0.000     1.446784    1.925393
                      ---------------------------+----------------------------------------------------------------
                               var(e.aware_count)|   2.099159   .0798288                      1.948296    2.261702
                      --------------------------------------------------------------------------------------------
                      
                      . 
                      .  gen one=1
                      variable one already defined
                      r(110);
                      
                      . mkmat aware_count, matrix(y)
                      
                      . mkmat one, matrix(ones)
                      
                      . mkmat one age_35_54 age_55_74 age75_ education_no_high_school education_college_nodegree education_degree education_postgrad gender_
                      > binary_female political_affiliation_dem political_affiliation_ind political_affiliation_not gn_sus_q1 gn_sus_q3 gn_sus_q4, matrix(X)
                      >  
                      
                      . 
                      . matrix Xb=inv(ones'*ones)*ones'*X
                      
                      .  matrix beta = b[1,15],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12],b[1,13],b[1,14]
                      
                      . 
                      . matrix beta = b[1,15],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12],b[1,13],b[1,14]
                      
                      . matrix BXoverS=Xb * beta'/b[1,16]
                      
                      . scalar Z=el(BXoverS,1,1)
                      
                      . scalar Mu=normalden(Z)/normal(Z)
                      
                      . scalar P=normal(Z)
                      
                      . scalar P1=P*(1-Z*Mu-Mu^2)
                      
                      . 
                      . 
                      . scalar P2=normalden(Z*Z+normalden(Z)*Mu)
                      
                      . display Mu
                      .
                      
                      . display P
                      .
                      
                      . 
                      .  display P1
                      .
                      
                      .  display P2
                      .

                      Comment


                      • #12
                        This is my output Andrew, please ignore the bit where I din't use code delimeters. I coudn't figure out how to delete.

                        Comment


                        • #13
                          matrix beta = b[1,15],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12],b[1,13],b[1,14]
                          matrix beta = b[1,15],b[1,1], b[1,2],b[1,3],b[1,4],b[1,5],b[1,6],b[1,7],b[1,8],b[1,9],b[1,10],b[1,11],b[1,12],b[1,13],b[1,14]
                          I do not see a matrix b defined in your code. There may yet be other issues.

                          Comment

                          Working...
                          X