Announcement

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

  • Minimum (or Maxium) Value in a Matrix

    I am using Stata 16 on Windows 10. I am running analyses (logit and mlogit) on 12 outcome variables, 11 exposure variables and 9 adjustment variables in three primary models. I've created a do file that successfully outputs my results to an excel file using putexcel. For each set of variables, I'd also like to use putexcel to output the frequency of the minimum "cell" size within the cross-tabulation of my my variable pairs. For example:

    . tab sex group, matcell(x)

    ...........| Group
    .......Sex | Sham Experiment | Total
    -----------+----------------------+----------
    ......Male | 48 40 | 88
    ....Female | 29 38 | 67
    -----------+----------------------+----------
    .....Total | 77 78 | 157

    . matrix list x

    x[2,2]
    ....c1 c2
    r1 48 40
    r2 29 38


    (Sorry about the inserted periods - I couldn't get the formatting correct.) For the above, the value I'd like returned would be 29. The matrices will vary from a 2X2 up to a 4X5 matrix.

    I've tried:

    . display min(x)
    invalid syntax
    r(198);

    . generate minx=min(`x')
    invalid syntax
    r(198);

    . generate minx = min(x)
    invalid syntax
    r(198);

    . generate minx==min(x)
    == invalid name
    r(198);


    But I obviously don't have something correct. I appreciate any help. Thank you.

  • #2
    It is easier to do this in Mata

    Code:
    sysuse auto, clear
    tab foreign rep78, matcell(x)
    mata: x=st_matrix("x")'
    mata: min(x)
    mata: max(x)
    Res.:

    Code:
    . tab foreign rep78, matcell(x)
    
               |                   Repair Record 1978
      Car type |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
      Domestic |         2          8         27          9          2 |        48 
       Foreign |         0          0          3          9          9 |        21 
    -----------+-------------------------------------------------------+----------
         Total |         2          8         30         18         11 |        69 
    
    . 
    . mata: x=st_matrix("x")'
    
    . 
    . mata: min(x)
      0
    
    . 
    . mata: max(x)
      27

    Comment


    • #3
      The immediate reason why the min() calls are all wrong is that the Stata function with that name works rowwise and requires two or more comma-separated arguments. It doesn't work columnwise on a variable or over all the elements of any other single argument.

      Comment


      • #4
        Thank you Andrew! I'll use Mata - I will undoubtedly benefit from learning to use it!
        Thank you Nick.

        Comment


        • #5
          (Sorry about the inserted periods - I couldn't get the formatting correct.)
          I realize this is a side issue, but the difficulty you are having with the formatting is that you are not using code delimiters. See Forum FAQ #12 for instructions how to do that.

          Comment

          Working...
          X