Announcement

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

  • svy command column proportions in a matrix

    First time poster so hopefully my questions makes sense. I've been creating tabulations to export to excel using the putexcel command, which has gone smoothly using this syntax:

    local startrow = `startrow' + 5
    local startcol = `startrow' - 1

    tabulate q2 q64c, matcell(freq) matrow(heatingfuel) col nofreq

    mata: st_matrix("freq", (st_matrix("freq") :/ colsum(st_matrix("freq"))))
    mat li freq , format("%3.2f")


    putexcel A`startrow'=matrix(heatingfuel) B`startrow'=matrix(freq)

    tabulate q64c, matcell(freq) matrow(hometype)
    matrix list hometype

    local cols = rowsof(hometype)
    local col = 2

    forvalues i = 1/3 {
    local val = hometype[`i',1]
    local val_lab : label (q64c) `val'
    local temp=char(64+`col')
    putexcel `temp'+`startcol'=("`val_lab'")
    local col = `col' + 1
    }

    matrix list heatingfuel
    local rows = rowsof(heatingfuel)
    local row = 2

    forvalues i = 1/`rows' {

    local val = heatingfuel[`i',1]
    local val_lab : label (q2) `val'
    local temp= `startrow'
    putexcel A`startrow'=("`val_lab'")
    local row = `row' + 1
    local startrow = `startrow' + 1
    }

    But now I am using the svy command to weight my survey and it appears that I can no longer use the matcell syntax that I was using to inform my matrices. I know I need to use something from the ereturn list to build my column proportions now, but I can't quite figure it out. I saw on another board to use something like this but it doesn't quite get me to the column proportions matrix that I would like to export to excel with the putexcel command:
    matrix ct = e(N_pop)*e(Prop)

  • #2
    Thanks for such a quick reply Steve! That answers most of my question, but I'm still stuck on trying to get a matrix creation command that replicates the table after I enter the command:

    svy: tabulate q1 q64c if state=="WY", col percent

    Comment


    • #3
      Oh and here is the code in the correct form, I didn't realize how to do that first time around.

      Code:
      svy: tabulate q1 q64c if state=="WY", col percent
      Thanks,
      John

      Comment


      • #4
        You're welcome,John, and thanks for using CODE delimiters. The forum software was freezing and I wound up deleting the post that you referred to. It was (for everyone else):

        _____________________________________________
        Welcome to Statalist, John!

        Your real question was simple: how to get a matrix of weighted counts after svy: tabulate. All but one line of the code that you showed was irrelevant to this question and just obscured the simple answer that you already had in hand:
        Code:
        matrix freq = e(N_pop)*e(Prop)
        mata: st_matrix("freq", (st_matrix("freq") :/ colsum(st_matrix("freq"))))
        Please read all FAQs, especially FAQ 12. When you have code and results to show, put them between CODE delimiters,described in that FAQ.
        ______________________________________________

        I think that my answer is complete; the only change I would make is to multiply the proportions by 100 in the mata statement (percentages were not part of your original solution for tabulate). If you think that the answer is still incomplete, then show code and stata output to back up your statement (FAQ 12, again). It would be best if you used a simplifed table without too many rows and columns, enough to demonstrate the issue (FAQ 12 yet again). Then you can, by hand, if necessary, show what you think the result shouldhave looked like.

        Last edited by Steve Samuels; 10 Dec 2015, 18:12.
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          I see what you're saying, I definitely had a lot of extra information that wasn't pertinent. I've now attached a sample dataset and here are the results that will hopefully explain my issue better. Again, what I want to do is not only get a matrix of weighted counts after svy:tabulate, but specifically a matrix of column percentages after svy:tabulate, I just don't fully understand what I should use from the ereturn list of stored results to get there.

          Tabulate that I ran on the dataset:

          Code:
          svy: tabulate var1 var2, col percent
          Results (the matrix that I would like to refer to/build)

          Code:
          (running tabulate on estimation sample)
          
          Number of strata   =         1                  Number of obs     =          4
          Number of PSUs     =         4                  Population size   =          8
                                                          Design df         =          3
          
          --------------------------------------
                    |            var2           
               var1 |     1      2      3  Total
          ----------+---------------------------
                  1 |    50      0      0     25
                  2 |     0    100      0     25
                  3 |     0      0    100     25
                  4 |    50      0      0     25
                    | 
              Total |   100    100    100    100
          --------------------------------------
            Key:  column percentage
          Note: Variance estimate degrees of freedom = 3 are less than nominal table degrees of freedom =
                6
          
            Pearson:
              Uncorrected   chi2(6)         =    8.0000
              Design-based  F(3.00, 9.00)   =    2.0000     P = 0.1846
          Matrix that successfully build weighted counts but not column percentages:

          Code:
          matrix freq = e(N_pop)*e(Prop)
          Code:
          matrix list freq
          Results

          Code:
          freq[4,3]
              p11  p12  p13
          y1    2    0    0
          y1    0    2    0
          y1    0    0    2
          y1    2    0    0
          Thanks,
          Thomas
          Attached Files

          Comment


          • #6
            You don't seem to have actually tried my suggestion, copied from your do file.
            Code:
            matrix freq = e(N_pop)*e(Prop)
            mata: st_matrix("freq", (st_matrix("freq") :/ colsum(st_matrix("freq"))))
            You can add the percentages, row totals, column totals, and grand total on your own.

            And, please, next time, give the code for inputting small data sets.
            Last edited by Steve Samuels; 10 Dec 2015, 20:17.
            Steve Samuels
            Statistical Consulting
            [email protected]

            Stata 14.2

            Comment


            • #7
              That did work! Sorry about that, I got a bit confused on the order of it all but I should have just plugged in at first.

              Thanks again!
              John

              Comment

              Working...
              X