Announcement

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

  • Running Sum Matrix

    Hello, I am new to Mata and how do you calculate a running sum matrix?

  • #2
    This works, and I too would like to see a better way.

    Code:
    : x = (1,2,3\4,5,6\7,8,9)
    
    : x
           1   2   3
        +-------------+
      1 |  1   2   3  |
      2 |  4   5   6  |
      3 |  7   8   9  |
        +-------------+
    
    : wanted = J(rows(x), 0, .)
    
    : for(j = 1; j <= cols(x); j++) {
    > wanted = wanted , runningsum(x[, j])
    > }
    
    : wanted
            1    2    3
        +----------------+
      1 |   1    2    3  |
      2 |   5    7    9  |
      3 |  12   15   18  |
        +----------------+

    Comment


    • #3
      Here's a start at a non-looping solution. Creating y as a function of the number of rows in x is left as an exercise for the reader.
      Code:
      : x = (1,2,3\4,5,6\7,8,9)
      
      : x
             1   2   3
          +-------------+
        1 |  1   2   3  |
        2 |  4   5   6  |
        3 |  7   8   9  |
          +-------------+
      
      : y = (1,0,0\1,1,0\1,1,1)
      
      : y*x
              1    2    3
          +----------------+
        1 |   1    2    3  |
        2 |   5    7    9  |
        3 |  12   15   18  |
          +----------------+

      Comment


      • #4
        Code:
        y=lowertriangle(J(rows(x),rows(x),1))

        Comment


        • #5
          Thanks to William Lisowski and John Mullahy for the cute solution: it's just the product of two matrices. On a different note, is there a good reason why runningsum() should not be generalised to work columnwise on a matrix with any number of columns?

          Comment


          • #6
            Hi,

            I am trying to run a loop where an individual has qty produced against variables 'qty_produce_kg' , 'yield' , 'dist_yr', 'subgrp'.

            The subgrp here is a list of indicators such as age1 age2 etc.

            I have tried to run the following loop, but it shows blank matrix:

            set trace on

            set matsize 800

            matrix prel = J(33,25,.)
            matrix rownames prel = age1 age2 age3 male female open obc scst openlease lease_land_yn fpc_subsidy_project subsidy_project_75pc subsidy_project_25pc subsidy_project_50pc nosubsidy intercropping monocropping preplanting_full preplanting_partial preplanting_none planting_full planting_partial planting_none afterplanting_full afterplanting_partial afterplanting_none
            matrix colnames prel = Samastipur_2017 Samastipur_2018 Samastipur_2019 Samastipur_2020 Saran_2017 Saran_2018 Saran_2019 Saran_2020 Siwan_2017 Siwan_2018 Siwan_2019 Siwan_2020 EC_2017 EC_2018 EC_2019 EC_2020 WC_2017 WC_2018 WC_2019 WC_2020 tot_2017 tot_2018 tot_2019 tot_2020


            local row A

            foreach crop of local cropname {

            local cropname blackgram chickpea fawabean greengram kidneybean lentil pea pigeonpea
            local subgrp age1 age2 age3 male female open obc scst openlease lease_land_yn fpc_subsidy_project subsidy_project_75pc subsidy_project_25pc subsidy_project_50pc nosubsidy intercropping monocropping preplanting_full preplanting_partial preplanting_none planting_full planting_partial planting_none afterplanting_full afterplanting_partial afterplanting_none
            local distyr Samastipur_2017 Samastipur_2018 Samastipur_2019 Samastipur_2020 Saran_2017 Saran_2018 Saran_2019 Saran_2020 Siwan_2017 Siwan_2018 Siwan_2019 Siwan_2020 EC_2017 EC_2018 EC_2019 EC_2020 WC_2017 WC_2018 WC_2019 WC_2020 tot_2017 tot_2018 tot_2019 tot_2020


            foreach d of local distyr {

            quietly summ qty_produce_kg_`crop' yield_`crop' if dist_yr == `d'

            foreach s of local subgrp {

            quietly summ qty_produce_kg_`crop' yield_`crop' if dist_yr == `d' & subgrp == `s'

            egen concat = concat(qty_produce_kg_`crop' yield_`crop'), punc( "()")

            matrix prel[rownumb(prel,"`s'"), colnumb(prel,"`d'")]= r(mean)

            *matrix prel(`row', `col'+ 2) = `cellval'

            local row = `A' + 1

            }

            local row = `A' + 1

            }

            }


            Any help would be appreciated

            Comment

            Working...
            X