Announcement

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

  • How to generate different matrices based on different factors and different factors levels

    mata
    D = (1,1,1 \ 2,1,1 \ 3,1,2 \ 4,1,2 \ 5,1,3 \ 6,1,3 \ 7,2,1 \ 8,2,1 \ 9,2,2 \ 10,2,2 \ 11,2,3 \ 12,2,3)
    // two factor variables,the second column has two levels(1 2), the thrird column has three levels(1 2 3) ,then I will do loop on the two factor variables
    for (i=1; i<=2; i++){
    for (j=1; j<=3; j++) {
    Dij=st_select(D,ij) \\ gen Dij matrix when the second column select i(1 or 2) and the third column select(1 or 2 or 3), then we get six matrixes:D11 D12 D13 D21 D22 D23
    colnames = "_i_j" \\ rename the colnames of matrixes Dij
    Dij = colnonmissing(Dij)' \\ then perform the same operation on the generated matrix
    }
    }
    end

    i don't know how to write the two rows of program in red, can you help? thank you very much
    Last edited by fu gang; 14 Jan 2021, 19:08.

  • #2
    Maybe this help:
    Code:
    sysuse auto, clear
    mata: D = st_data(., "i.rep78")
    mata: D[.,1] :* D[.,2]
    Last edited by Niels Henrik Bruun; 14 Jan 2021, 23:28.
    Kind regards

    nhb

    Comment


    • #3
      thank you for your reply,thank you very much. using dummy variables , I have two Influencing factors,not one, and there are multilevels in each factor. I still can't do the loop and split the data, can you ,Can you explain further?


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(obs a b) double v1
       1 1 1 1.2
       2 2 2 1.8
       3 1 3 1.6
       4 2 1 1.5
       5 1 2 1.9
       6 2 3   1
       7 1 1 1.1
       8 2 2 1.5
       9 1 3 1.6
      10 2 1 1.7
      11 1 2 1.5
      12 2 3 1.6
      end
      a and b are two factor variables,v1 is the valuse for computing. We will select the data when a= (1 2) and b =(1 2 3),

      Comment


      • #4

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(obs a b) double v1
         1 1 1 1.2
         2 2 2 1.8
         3 1 3 1.6
         4 2 1 1.5
         5 1 2 1.9
         6 2 3   1
         7 1 1 1.1
         8 2 2 1.5
         9 1 3 1.6
        10 2 1 1.7
        11 1 2 1.5
        12 2 3 1.6
        end
        /* Do one loop first */
        mata
        D = st_data(.,.)
        for (i=1; i<=2; i++){
        D1=st_select(D,D[.,2]=1,.) /* mistake here wrong expression of rows*/
        D1
        }
        end


        D1=st_select(D,1\3\5\7\9\11,.) when i =1, i want to get the rows of D[.,2] (variable a ) ==1 ,
        D1 No results displayed, I don't get the target matrix, maybe I should use st_data()

        mata
        D = st_data(.,.)
        for (i=1; i<=2; i++){
        D1=st_data(v,.) /* I don't know how to express the target matrix rows, when i = 1, v shoud be the rows of matrix when a = 1*/
        D1
        }

        D1=st_data(D,1\3\5\7\9\11,.) when i use st_data(), D1 is the target matrix, so st_data() will work.

        When there are two or multiple cycles, how to take the intersection, that is, satisfy i=1 & j=1, That is, how to express the rows number of a=i and b=j through the expression of i and j
        Last edited by fu gang; 15 Jan 2021, 14:15.

        Comment


        • #5
          mata:
          mata clear
          mata: D = st_data(., "i.a")
          D
          A = D[.,1] :* D[.,2]
          A

          The result is a matrix (0:0:0:0:0:0:0:0:0:0:0:0)but it is not what i want.

          Comment


          • #6
            I misunderstood what you wanted.
            Can you show input and output? And explain what you want?
            Kind regards

            nhb

            Comment


            • #7
              thank you very much , i wil show it right now

              Comment


              • #8
                I have a data, including the sequence obs, categorical variables a (1, 2) ,and b (1, 2, 3), and some other variables that need statistics v1, v2, v3...vn, omitted as v1
                Now I want to split the data by a and b use mata. split thd data into 6 small data, D11, D12, D13, D21, D22, D23, when a=1 and b = 1, i'll get D11,and so on. and then calculate the number of non-missing values in the data, finally summarize the non-missing values of the six small data into one Table, If possible, calculate the sum of rows and columns. I can use stata to complete this calculation,but I am learning mata and want to use mata's loop to achieve
                Last edited by fu gang; 17 Jan 2021, 08:40.

                Comment


                • #9
                  Calculate the number of non-missing values in each small data Dij, and then merge the results into one table,calculate the sum of rows and columns, and finally my result is the number of non-missing values of v1, v2...vn variables separately,the target result is a table as follows:

                  count of non-missing values
                  D11 D12 D13 D21 D22 D23 Total
                  V1
                  V2
                  V3
                  ...
                  Vn
                  Total
                  Last edited by fu gang; 17 Jan 2021, 09:18.

                  Comment


                  • #10
                    I think I would solve this in Stata instead of Mata
                    Kind regards

                    nhb

                    Comment


                    • #11
                      thank you ,i have solved this in Stata for several methods
                      local v1 1 2
                      local v2 1 2 3

                      foreach i of local v1{
                      foreach j of local v2{
                      keep if a == `i'
                      keep if b == `j'
                      tabstat ……
                      }
                      }

                      maybe forvalues is better
                      Last edited by fu gang; 18 Jan 2021, 08:08.

                      Comment


                      • #12
                        or
                        tabstat varlist if a == `i' & b ==`j', stat(count) col(stat) save
                        or
                        tabstat varlist if a == `i' , by(`j') stat(count) col(stat) save

                        Comment


                        • #13
                          Maybe you have several better and more concise methods, can you share your better methods?

                          Comment


                          • #14
                            Fine solutions. All depends on what you want.

                            Using -collapse- might be what you want. Or -tab-

                            If you only have two categorical variables the command -sumat- in package matrixtools (ssc), but want the summary in a matrix
                            Kind regards

                            nhb

                            Comment


                            • #15
                              So kind of you, Thank you very much , I'll try them soon

                              Comment

                              Working...
                              X