Announcement

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

  • How toextract the results of various stata commands into matrices or mata universally and selectively

    For stata commands(such as npresent, fsum, etc.) without a matrix in the return values(the result of ret list,eret list etc.), how to store the command presentation result table
    as a matrix or mata for further operations? I know that we can export the table through log or other commands, and then import the results into stata, and then use commands
    such as mkmat to generate matrices, and use functions such as st_data() or st_view() to generate mata.
    Is there a universal (the tables generated for various commands is generally applicable), selected (you can choose the corresponding row and column at will), direct (rather than
    through a series of steps) command or method to generate Matrix or mata?
    For example:
    example 1:
    Code:
    npresent a b c
    i got the tow columns,one is the list of variables, the other is the Non-missing value count,I want turn the tow columns into matrices or mata
    example 2:
    Code:
    fsum a b c
    I got six columns,include Variable, N ,Mean,SD ,Min, Max .Can I get any columns or any rows from the table on the screen into matrices or mata

    Need you help,Your reply is a great help to me, Thank you very much




  • #2
    I am not very familiar with npresent or fsum, but I see several ways to obtain N, mean, min and max into mata. First would be to use tabstat with the option save and then copy the saved matrix into mata, eg:
    Code:
    tabstat a b c , s(n mean min max) save
    mata: stats = st_matrix("`r(StatTotal)'")
    Alternatively you can copy the variabels into mata and then use matas function to calculate min, max and mean, i.e.:
    Code:
    putmata vars = (a b c)
    mata:
    mean(vars)
    min(vars)
    max(vars)
    stats = mean(vars)' , min(vars)' , max(vars)'
    end

    Comment


    • #3
      thank you very much for your reply, i have learned tabstat cmd,Command includes save option, when add "save" , the result will generate the matrics
      r(StatTotal),then we can get it ,but if the command don't have the option,then how can we do? the code followed is a good idea,but when do "stats = mean(vars)' , min(vars)' , max(vars)'",it gives an err message as follows <istmt>: 3200 conformability error

      Comment


      • #4
        Sorry, the second example should be:
        Code:
        putmata vars = (a b c)
        mata:
        mean(vars)
        colmin(vars)
        colmax(vars)
        stats = mean(vars)',colmin(vars)',colmax(vars)'
        end

        Comment


        • #5
          thank you very much

          Comment

          Working...
          X