Announcement

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

  • New Package: FLEXMAT: Create Tables and matrices with flexibility

    I have published a new package -- flexmat -- on the SSC. To install
    Code:
    ssc install flexmat
    Description
    flexmat creates flexible matrices of text, numbers or both. This program is specifically useful when numeric and string results need to be stored in a matrix form or in a table. flexmat hardly throws any comfortability error. So we can append as well as insert cells, rows, columns, or even previously stored matrices to the current matrix without worrying about comfortability or about mixing reals with string. We can also delete rows or columns from a table / matrix. We can also merge two matrices. The merge is just like 1:1 merge of the official merge command.

    Why flexmat?
    The motivation for flexmat came from many requests related to making customizable tables with asdoc (One of my programs, available on the SSC). Though asdoc is extremely easy to use with many official commands of Stata such as sum, cor, reg, table, tab, ttest, etc. It is still challenging to create a custom table from bits and pieces of statistics coming from different lines of codes. To this end, asdoc offers the row() option, where tables are created row by row. The row() option is not flexible. You cannot create rows by cells. You cannot add columns. You cannot go back and drop rows or columns. You cannot replace cells or rows. All these limitations are taken care of in the flexmat.

    An Example
    Suppose we wish to create this table.

    Code:
            -------------------------------------------------------------------------
              1 |Year                    Mean          SD         Min         Max
            ----+---------------------------------------------------------------------
              2 |2001                    5.1%        1.5%        0.00           10%
              3 |2002                    6.74        1.68        .012            11
              4 |2003                    6.58        1.61       0.025             9
              5 |2005                   0.370       0.680       0.1370        0.370
              6 |2006                   0.270       0.870       0.070         0.370
            --------------------------------------------------------------------------
    In the following example, I am using sub-commands reset, addcell, addrow, addcol, dropcol, and addmat. You can read about all sub-commands and options in the help file of flexmat or this page

    Code:
    * Start with a clean slate
    flexmat reset
    
    * Create the title row
    flexmat addrow, data(Year, Mean, SD, Min, Max) row(1) col(1)
    Explanation:
    addrow : sub-command to add a row of data to a table
    data() : pass data contents, separated by a parse character
    row(1) : specify row number; default is 1
    col(1) : specify column number; default is 1

    After the above line, flexmat table looks likes this. Note that numbers at the top of the table and at the left. These denote the row and column numbers for easy identification. These can be used later on while posting more data to the table.
    Code:
           1      2      3      4      5
        +------------------------------------+
      1 |  Year   Mean     SD    Min    Max  |
        +------------------------------------+


    Code:
    * Create the first column
    
    flexmat addcol, data(2001, 2002, 2003, 2005, 2006) row(2) col(1)
    
      0 |1                      2                3                4                5
    ----+-----------------------------------------------------------------------------
      1 |Year                Mean               SD              Min              Max
    ----+-----------------------------------------------------------------------------
      2 |2001                                                                        
      3 |2002                                                                        
      4 |2003                                                                        
      5 |2005                                                                        
      6 |2006                                                                        
    ----------------------------------------------------------------------------------
    The basic structure of the table is now ready. We can now add cells, rows, or columns to any row() and col() combination

    Cells can be either replaced or added. Say we have the mean value of 5.1% and we want to post it to row(2) and col(2)
    Code:
     * Adding cells
    flexmat addcell, data(5.1% ) row(2) col(2)

    Assume the mean values are 6.74, 6.58, 3.7, 2.7 in years 2002, 2003, 2005, 2006. Therefore, use row(3) and col(2) as starting point for adding this data.
    Code:
    * Add column 
    flexmat addcol, data(6.74, 6.58, 3.7, 2.7 ) row(3) col(2)
    
     0 |1                      2                3                4                5
    ----+-----------------------------------------------------------------------------
      1 |Year                Mean               SD              Min              Max
    ----+-----------------------------------------------------------------------------
      2 |2001                5.1%                                                    
      3 |2002                6.74                                                    
      4 |2003                6.58                                                    
      5 |2005                 3.7                                                    
      6 |2006                 2.7                                                    
    ----------------------------------------------------------------------------------

    Both Stata and Mata matrices can be appended to the flexmat table. The mata matrix should be in string. Let us create a mata column vector and append it under the SD column
    Code:
    * Add matrix
    mata: M = "2.1%"\ "2"\ "3.32" \ "6.3" \ "1.75"
    
    flexmat addmat, matname(M) row(2) col(3)
    0 |1                     2               3               4               5
    ----+-----------------------------------------------------------------------------
      1 |Year               Mean              SD             Min             Max
    ----+-----------------------------------------------------------------------------
      2 |2001               5.1%            2.1%                                
      3 |2002               6.74               2                                
      4 |2003               6.58            3.32                                
      5 |2005                3.7             6.3                                
      6 |2006                2.7            1.75                                
    ----------------------------------------------------------------------------------
    * Stata matrix can also be appended
    Code:
    mat S = 0\1\1.5\2.3\1
    flexmat addmat, matname(S) row(2) col(4) nonames
    * Drop elements
    Both rows and columns can be dropped from the flexmat matrix. Let us drop column 5
    Code:
    flexmat dropcol, col(5)
    
      0 |1                          2                   3                   4
    ----+-----------------------------------------------------------------------------
      1 |Year                    Mean                  SD                 Min
    ----+-----------------------------------------------------------------------------
      2 |2001                    5.1%                2.1%                   0
      3 |2002                    6.74                   2                   1
      4 |2003                    6.58                3.32                 1.5
      5 |2005                     3.7                 6.3                 2.3
      6 |2006                     2.7                1.75                   1
    ----------------------------------------------------------------------------------
    How to export

    Once the flexmat file is ready, it can be then exported to any of the asdocx's supported formats, that include MS Word, Excel, or LaTeX. asdocx is a premium version of asdoc and is available for a nominal fee of $9.99. Following is the asdocx syntax for exporting flexmat files.

    Code:
      asdocx exportflex , [asdocx_options]
    Since the default output format is .docx, therefore, if just typed:

    Code:
      asdocx exportflex
    the flexmat file will be exported to a Word file with asdocx .docx format. In case we wish to send the output to an Excel file, then we would use the asdocx option save(Myfile.xlsx). For example:

    Code:
       asdocx exportflex, save(Myfile.xlsx)
    We can also use other asdocx options such as font(font_name) to change the font family or fs(#) to set
    the font size of the document.

    Even if asdocx is not available, the flexmat table can be copy pasted into Excel.
    Last edited by Attaullah Shah; 07 Aug 2021, 08:48.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    Seems like a useful addition. Just one quick question. When you say
    The basic structure of the table is now ready.
    Do we need to create the table structure as a first step and then populate the table?

    Comment


    • #3
      Saeed Sardar I call it -- flexmat -- for a reason. It is flexible and can create a matrix from any awkward combination of cells, row, and columns. Even if the structure of the table does not exist and we post data to a cell or row , flexmat will first make the structure and then post the data. Therefore, we do not have to worry about the structure of the table. See some examples here.

      1. Posting data to a cell that does not exist
      We start with a clean slate and post some data to a cell at the intersection of row(2) and col(3). Since we have not created a table with these dimensions, flexmat still posts the data to the correction location: Creating a table with two rows and three columns as a first step; and posting the contents of the data() option to the desired cell.

      Code:
      . flexmat clear
      flexmat addcell, data(Data1) row(2) col(3)
      
      
         
        0 |1                          2                          3 
      ----+-----------------------------------------------------------------------------
        1 |                                                       
      ----+-----------------------------------------------------------------------------
        2 |                                                 Data1 
      ----------------------------------------------------------------------------------
      2. Posting data to a row that does not exist
      Similarly, we can post data to a row that does not exist. In the following example, we shall post data to row(2)
      Code:
      * Clear flexmat tables
      flexmat clear
      
       flexmat addrow, data(Data1, Data2) row(2) col(1)
      
      
         
        0 |1                                                 2 
      ----+-----------------------------------------------------------------------------
        1 |                                                    
      ----+-----------------------------------------------------------------------------
        2 |Data1                                         Data2 
      ----------------------------------------------------------------------------------
      3. Rows and columns can be negative
      Both row() and col() options accept negative integers. Negative row() is used to create / add a row at the top of the table, that is above the row number one. Negative col() is used to add / create columns to the left of column 1.
      Code:
      *Add a cell to the left of the at row(2)
       flexmat addcell, data(Me at the Left) row(2) col(-1)
      
      
         
        0 |1                                            2                         3 
      ----+-----------------------------------------------------------------------------
        1 |                                                                         
      ----+-----------------------------------------------------------------------------
        2 |Me at the Left                           Data1                     Data2 
      ----------------------------------------------------------------------------------
      
      . 
      * Add a cell at the top of row(1) in column 1.
      . flexmat addcell, data(Me at the Top) row(-1) col(1)
      
      
         
        0 |1                                            2                         3 
      ----+-----------------------------------------------------------------------------
        1 |Me at the Top                                                            
      ----+-----------------------------------------------------------------------------
        2 |                                                                         
        3 |Me at the Left                           Data1                     Data2 
      ----------------------------------------------------------------------------------
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Thanks for such a detailed reply. Indeed, flexmat seems to be very useful in developing complex tables. I have one more question (I hope I am not asking too many): can flexmat modify asdoc or asdocx tables? I mean, can flexmat modify tables already created by asdoc / asdocx? Thanks for you time.

        Comment


        • #5
          I have one more question (I hope I am not asking too many)
          You are welcome. I have a lot to write about flexmat and your questions help me in that direction.

          flexmat and asdocx are interwoven. asdocx uses flexmat for building the basic structure of tables. These tables are written as Mata matrices and are available for further modifications. Once asdocx completes a table, it is shown on the screen with rows and column numbers. The table can be modified using all flexmat sub-commands and options. Let me give you an example. Let me (1) create a table of detailed descriptive statistics (2) modify it with flexmat, and (3) finally export the table to MS Word.

          Code:
          *Use auto dataset
          sysuse auto, clear
          
          * Table of detailed descriptive statistics using asdocx
          asdocx sum, detail 
            
                                                    Descriptive Statistics
          
            0 |1                       2         3         4        5        6        7        8        9       10 
          ----+------------------------------------------------------------------------------------------------------
            1 |Variables             Obs      Mean Std. Dev.      Min      Max       p1      p99    Skew.    Kurt. 
          ----+------------------------------------------------------------------------------------------------------
            2 |price                  74  6165.257  2949.496     3291    15906     3291    15906    1.653    4.819 
            3 |mpg                    74    21.297     5.786       12       41       12       41    0.949    3.975 
            4 |rep78                  69     3.406      0.99        1        5        1        5   -0.057    2.678 
            5 |headroom               74     2.993     0.846      1.5        5      1.5        5    0.141    2.208 
            6 |trunk                  74    13.757     4.277        5       23        5       23    0.029    2.192 
            7 |weight                 74  3019.459   777.194     1760     4840     1760     4840    0.148    2.118 
            8 |length                 74   187.932    22.266      142      233      142      233   -0.041    2.042 
            9 |turn                   74    39.649     4.399       31       51       31       51    0.124    2.229 
           10 |displacement           74   197.297    91.837       79      425       79      425    0.592    2.376 
           11 |gear_ratio             74     3.015     0.456     2.19     3.89     2.19     3.89    0.219    2.102 
           12 |foreign                74     0.297      0.46        0        1        0        1    0.887    1.787 
          -----------------------------------------------------------------------------------------------------------
          
          * Modify a cell contents 
          * Let us replace the text variables with text Vars at row(1) and col(1)
          flexmat addcell, data(Vars) row(1) col(1)
          
          * Let us drop column 9
          flexmat dropcol, col(9)
          
          * Drop row 12
          flexmat droprow, row(12)
          
          * Modified tables looks like this
          
          
                                                   Descriptive Statistics
          
            0 |1                         2          3          4         5         6         7         8         9 
          ----+----------------------------------------------------------------------------------------------------
            1 |Vars                    Obs       Mean  Std. Dev.       Min       Max        p1       p99     Kurt. 
          ----+----------------------------------------------------------------------------------------------------
            2 |price                    74   6165.257   2949.496      3291     15906      3291     15906     4.819 
            3 |mpg                      74     21.297      5.786        12        41        12        41     3.975 
            4 |rep78                    69      3.406       0.99         1         5         1         5     2.678 
            5 |headroom                 74      2.993      0.846       1.5         5       1.5         5     2.208 
            6 |trunk                    74     13.757      4.277         5        23         5        23     2.192 
            7 |weight                   74   3019.459    777.194      1760      4840      1760      4840     2.118 
            8 |length                   74    187.932     22.266       142       233       142       233     2.042 
            9 |turn                     74     39.649      4.399        31        51        31        51     2.229 
           10 |displacement             74    197.297     91.837        79       425        79       425     2.376 
           11 |gear_ratio               74      3.015      0.456      2.19      3.89      2.19      3.89     2.102 
          ---------------------------------------------------------------------------------------------------------
          
          * Export to MS Word
          asdocx exportflex
          Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	107.0 KB
ID:	1623480


          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            Really helpful. Thanks for your time and detailed replies.

            Comment


            • #7
              For asdocx users!
              Please do not install flexmat as it is already part of the asdocx package. Installing it again may result in duplication of functions (same functions may exists in the lasdocx.mlib and lflexmat.mlib libraries).
              Regards
              --------------------------------------------------
              Attaullah Shah, PhD.
              Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
              FinTechProfessor.com
              https://asdocx.com
              Check out my asdoc program, which sends outputs to MS Word.
              For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

              Comment

              Working...
              X