Announcement

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

  • 🙋 Equivalent of mat_capp in Mata

    I am looking for the equivalent of mat_capp/mat_rapp commands in Mata.
    Specifically, I need to perform the same operations on string matrices which are not possible in the ado-language of Stata.

  • #2
    Those two Stata commands match on Stata matrixes' row and column names. Do Mata matrixes have row or column names?

    Comment


    • #3
      You might be interested in the class nhb_mt_labelmatrix which is part of lmatrixtools.(mlib/mata).
      The mata/mlib files are the backbone of the packages basetable, matrixtools, and emc.
      All of these can be found at the ssc.

      The content of lmatrixtools is:
      Code:
      . mata mata describe using lmatrixtools
      (library contains 102 members)
      
      
            # bytes   type                        name and extent
      -------------------------------------------------------------------------------
              1,336   auto classdef scalar        nhb_List()
                248   auto void                    ::append()
                288   auto transmorphic colvector  ::apply()
                212   auto transmorphic colvector  ::content()
                288   auto real matrix             ::find()
                368   auto real colvector          ::frequency()
                244   auto real scalar             ::has_next()
                700   auto transmorphic colvector  ::intersection_unique()
                160   auto real scalar             ::is_empty()
                140   auto real scalar             ::len()
                704   auto transmorphic colvector  ::less_unique()
                212   auto transmorphic scalar     ::next()
                136   auto void                    ::next_init()
                208   auto void                    ::remove()
                192   auto void                    ::reset()
                168   auto string scalar           ::type()
                412   auto transmorphic colvector  ::union_unique()
                212   auto transmorphic colvector  ::unique_values()
                696   auto transmorphic vector    nhb_fp_map()
                432   auto transmorphic vector    nhb_fp_reduce()
              1,256   auto real matrix            nhb_mc_percentiles()
              1,852   auto class scalar           nhb_mc_post_ci_table()
                672   auto real matrix            nhb_mc_predictions()
              1,216   auto real colvector         nhb_mc_smoothed_data()
                416   auto real rowvector         nhb_mc_smoothed_minmax()
                592   auto classdef scalar        nhb_mc_splines()
                216   auto void                    ::add_knots()
                540   auto real matrix             ::marginal_linear()
                156   auto void                    ::new()
                208   auto real vector             ::positive()
              1,200   auto real matrix             ::restricted_cubic()
                484   auto transmorphic matrix    nhb_msa_file_size_kb()
              1,252   auto transmorphic matrix    nhb_msa_oswalk()
                460   auto string scalar          nhb_msa_unab()
              2,288   auto transmorphic matrix    nhb_msa_variable_description()
              1,208   auto classdef scalar        nhb_mt_chi2tabulate()
                544   auto class scalar            ::column_proportions()
                132   auto class scalar            ::counts()
              1,220   auto class scalar            ::counts_with_totals()
                632   auto class scalar            ::expected()
                132   auto class scalar            ::greeks()
              1,048   auto class scalar            ::likelihood_ratio_chisquare_parts()
                380   auto void                    ::new()
              1,040   auto class scalar            ::pearson_chisquare_parts()
                656   auto class scalar            ::proportions()
                544   auto class scalar            ::row_proportions()
              4,964   auto void                    ::set()
                132   auto class scalar            ::tests()
                228   auto void                    ::verbose()
                736   auto transmorphic matrix    nhb_mt_format_real_matrix()
              1,368   auto transmorphic matrix    nhb_mt_justify_string_matrix()
              1,516   auto classdef scalar        nhb_mt_labelmatrix()
                664   auto void                    ::add_sideways()
                664   auto void                    ::append()
                332   auto void                    ::clear()
              1,084   auto transmorphic matrix     ::column_equations()
              1,084   auto transmorphic matrix     ::column_names()
                560   auto string colvector        ::duplicate_strip()
                156   auto real scalar             ::empty()
                596   auto void                    ::from_labelmatrix()
                672   auto void                    ::from_matrix()
                224   auto real matrix             ::hide_small()
                160   auto void                    ::new()
              2,920   auto void                    ::print()
              2,172   auto class scalar            ::regex_select()
              1,084   auto transmorphic matrix     ::row_equations()
              1,084   auto transmorphic matrix     ::row_names()
                784   auto void                    ::to_matrix()
                732   auto class scalar            ::transposed()
                268   auto transmorphic matrix     ::values()
              7,396   auto transmorphic matrix    nhb_mt_mata_string_matrix_styled()
                936   auto transmorphic matrix    nhb_mt_matrix_stripe()
                896   auto transmorphic matrix    nhb_mt_matrix_v_sep()
                736   auto transmorphic matrix    nhb_mt_resize_matrix()
              1,068   auto classdef scalar        nhb_mt_tabulate()
                544   auto class scalar            ::column_proportions()
                132   auto class scalar            ::counts()
              1,220   auto class scalar            ::counts_with_totals()
                632   auto class scalar            ::expected()
              1,048   auto class scalar            ::likelihood_ratio_chisquare_parts()
                220   auto void                    ::new()
              1,040   auto class scalar            ::pearson_chisquare_parts()
                656   auto class scalar            ::proportions()
                544   auto class scalar            ::row_proportions()
              3,112   auto void                    ::set()
              1,888   auto class scalar            ::tests()
                516   auto real colvector          ::validate_varname()
                536   auto string rowvector       nhb_muf_tokensplit()
              1,220   auto transmorphic matrix    nhb_sae_addvars()
              1,636   auto transmorphic matrix    nhb_sae_appendvars()
              1,648   auto real matrix            nhb_sae_collapse()
                964   auto string rowvector       nhb_sae_labelsof()
                472   auto transmorphic matrix    nhb_sae_logstatacode()
                692   auto transmorphic matrix    nhb_sae_markrows()
                244   auto transmorphic matrix    nhb_sae_num_scalar()
              1,612   auto transmorphic matrix    nhb_sae_stored_scalars()
                656   auto transmorphic matrix    nhb_sae_str_mult_matrix()
                884   auto transmorphic matrix    nhb_sae_subselect()
             11,308   auto class scalar           nhb_sae_summary_row()
                848   auto real rowvector         nhb_sae_unique_values()
                932   auto transmorphic matrix    nhb_sae_validate_variable()
                628   auto transmorphic matrix    nhb_sae_variable_data()
      -------------------------------------------------------------------------------
      If one of the above packages are installed, you can e.g. do:
      Code:
      . sysuse auto
      (1978 Automobile Data)
      
      . sumat price mpg if foreign, statistics(mean ci)
      -----------------------------------------
                       mean  ci95% lb  ci95% ub
      -----------------------------------------
      Price         6384.68   5289.07   7480.29
      Mileage(mpg)    24.77     22.01     27.54
      -----------------------------------------
      
      . matrix foreign = r(sumat)
      
      . sumat price mpg if !foreign, statistics(mean ci)
      -----------------------------------------
                       mean  ci95% lb  ci95% ub
      -----------------------------------------
      Price         6072.42   5230.64   6914.21
      Mileage(mpg)    19.83     18.54     21.12
      -----------------------------------------
      
      . matrix domestic = r(sumat)
      
      . mata
      ------------------------------------------------- mata (type end to exit) ---------------------------------------------------------------------
      : lmf = nhb_mt_labelmatrix()
      : lmf.from_matrix("foreign")
      : lmf.print()
      -----------------------------------------
                       mean  ci95% lb  ci95% ub
      -----------------------------------------
      Price         6384.68   5289.07   7480.29
      Mileage(mpg)    24.77     22.01     27.54
      -----------------------------------------
      
      : lmd = nhb_mt_labelmatrix()
      : lmd.from_matrix("domestic")
      : lmf.column_equations("Foreign")
      : lmd.column_equations("Domestic")
      : lmf.add_sideways(lmd)
      : lmf.print()
      -----------------------------------------------------------------------
                    Foreign                      Domestic                    
                       mean  ci95% lb  ci95% ub      mean  ci95% lb  ci95% ub
      -----------------------------------------------------------------------
      Price         6384.68   5289.07   7480.29   6072.42   5230.64   6914.21
      Mileage(mpg)    24.77     22.01     27.54     19.83     18.54     21.12
      -----------------------------------------------------------------------
      
      : end
      -----------------------------------------------------------------------------------------------------------------------------------------------
      I've used -sumat- from package matrixtools to get example matrices.
      Note that nhb_mt_labelmatrix also have the method append to put matrices above each other.
      Method is not yet public.
      Also, I'm quite proud of the method regex_select which can be used to select part of the matrix using regex notation.

      Have fun
      Kind regards

      nhb

      Comment


      • #4
        Ah, I did not read your request proporly, sorry.
        You want to use string matrices as bases.
        So far this is just for matrices of reals with labels, sorry
        Kind regards

        nhb

        Comment


        • #5
          Joseph, Neils thank you for responding and sorry about some confusion.

          Joseph is right that mata matrices don't have row and column names as Stata matrices do.
          I do need to concatenate string matrices because at least partially the content I want to operate on is strings.
          My row and column names are just the first row and column of the matrix.
          So far I aggregate the data in form of mata string matrices.
          The following example illustrates what I am looking for (GOOD)

          Code:
          clear all
          
          mata
              A = "varname", "obs", "state",    "type" \ ///
                  "mpg",     "74",  "original", "input" \ ///
                  "rep78",   "69",  "modified", "input"
          
              B = "varname", "obs", "state",    "type" \ ///
                  "rep78",   "74",  "original", "input" \ ///
                  "dch",     "39",  "original", "calculated"
              A
              B
              BAD=A,B
              "BAD -->"
              BAD
              // Hypothetical command
              // CONCAT_MAT A,B , MATCH("varname") MODE(COLUMNS), EMPTY("~")
              GOOD= "varname", "obs", "state",    "type" , "obs", "state",    "type"\ ///
                    "mpg",     "74",  "original", "input", "~",   "~",        "~" \ ///
                    "rep78",   "69",  "modified", "input", "74",  "original", "input" \ ///
                    "dch",     "~",   "~",        "~",     "39",  "original", "calculated"
              "GOOD -->"
              GOOD
          end
          So far I can see how this can be done with merging two data files, but I'd like to keep the column titles identical and avoid file operations.

          This may be is quite a niche need, so nothing like this may readily exist, but just wanted to check if there is a ready-made solution before writing my own.

          Thank you again, Sergiy

          Comment


          • #6
            The following post described a related task:
            https://theesspreckelsen.wordpress.com/2014/07/

            I have by now solved my problem with a custom code, so this thread can be closed.

            Comment

            Working...
            X