Announcement

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

  • Using svy command with graph command

    Dear all,

    I am trying to use graph in Stata 16 to plot bars of the means of categorical variables (5-point scale) while using the svy command so as to have accurate point estimates.

    Here's an example of what I tried to do using a variable "nuclear" that excludes the 6th response category ("don't know").
    Code:
    . svy: graph bar nuclear if nuclear!=6
    graph is not supported by svy with vce(linearized); see help svy estimation for a list of Stata
    estimation commands that are supported by svy
    r(322);
    I had successfully used svyset to calibrate the weights with the rake option:
    Code:
    #delimit ;
    svyset [pweight=_one], rake(i.gender2 i.age1 i.region1 i.race3 i.latino i.educ1, 
        totals(_cons=25127
        1.gender2=12236 2.gender2=12891
        1.age1=7568 2.age1=8305 3.age1=9254 
        1.region1=5260 2.region1=4435 3.region1=9499 4.region1=5933
        1.race3=18242 2.race3=3191 3.race3=3694
        1.latino=20604 2.latino=4523
        1.educ1=9944 2.educ1=7748 3.educ1=7435
        ));
    #delimit cr
    I also saved the raked weights as "rake_wt":
    Code:
    #delimit ;
    svycal rake i.gender2 i.age1 i.region1 i.race3 i.latino i.educ1 [pw=_one],
    gen(rake_wt) totals(_cons=25127
        1.gender2=12236 2.gender2=12891
        1.age1=7568 2.age1=8305 3.age1=9254 
        1.region1=5260 2.region1=4435 3.region1=9499 4.region1=5933
        1.race3=18242 2.race3=3191 3.race3=3694
        1.latino=20604 2.latino=4523
        1.educ1=9944 2.educ1=7748 3.educ1=7435
        ) ;
    #delimit cr
    I know graph has a pweight option, however I get different point estimates when I set pweight equal to my calibrated weights. For example, here's the results from svy: mean:
    Code:
    . svy: mean nuclear if nuclear!=6
    (running mean on estimation sample)
    
    Survey: Mean estimation
    
    Number of strata =       1        Number of obs   =      2,836
    Number of PSUs   =   2,836        Population size =     25,127
    Calibration      :    rake        Design df       =      2,835
    
    --------------------------------------------------------------
                 |             Linearized
                 |       Mean   Std. Err.     [95% Conf. Interval]
    -------------+------------------------------------------------
         nuclear |   3.087027   .0283936      3.031353    3.142701
    --------------------------------------------------------------
    Versus the graph I get when I set pweight equal to my calibrated weights:
    Code:
    graph bar nuclear if nuclear!=6 [pw = rake_wt]


    Without having to individually save each svy: mean result, is there a way I can graph the accurate point estimates?

    Best,
    Jason

  • #2
    Dear all,

    I've looked through the Stata forum and read several other unanswered posts on similar topics. In my case, I ended up saving each svy: mean in an excel file and plotting the accurate point estimates this way. I briefly looked up whether R's ggplot package could do this, and it sounded like there was a similar problem using weighted means in a graphing function. Hope it helps anyone else who has this issue.

    Best,
    Jason

    Comment


    • #3
      It's not very efficient to try to shoehorn complex estimation procedures into graphs. It's far better to run the model, collect estimates into a dataset or frame, and plot those. Then you aren't relying on whether the graph commands interpret weights the same way as the estimation procedure (they often don't), and you can much more easily customize the graph later.

      Comment


      • #4
        If you want to plot point estimates and don't care about standard errors or other measures of variance, just use aweights instead of the svy prefix. Here's a demonstration that shows how these two methods yield identical point estimates (but different standard errors).
        Code:
        . use https://www.stata-press.com/data/r17/nhanes2f
        
        . svyset
        
        Sampling weights: finalwgt
                     VCE: linearized
             Single unit: missing
                Strata 1: stratid
         Sampling unit 1: psuid
                   FPC 1: <zero>
        
        . svy: mean age, over(race)
        (running mean on estimation sample)
        
        Survey: Mean estimation
        
        Number of strata = 31            Number of obs   =      10,337
        Number of PSUs   = 62            Population size = 117,023,659
                                         Design df       =          31
        
        --------------------------------------------------------------
                     |             Linearized
                     |       Mean   std. err.     [95% conf. interval]
        -------------+------------------------------------------------
          c.age@race |
              White  |   42.50856   .3215069      41.85284    43.16427
              Black  |   40.21349   .5807288      39.02908    41.39789
              Other  |   40.46675    2.36883      35.63549    45.29801
        --------------------------------------------------------------
        
        . mean age [aweight = finalwgt], over(race)
        
        Mean estimation                         Number of obs = 10,337
        
        --------------------------------------------------------------
                     |       Mean   Std. err.     [95% conf. interval]
        -------------+------------------------------------------------
          c.age@race |
              White  |   42.50856   .1632382      42.18858    42.82854
              Black  |   40.21349   .4615468      39.30877    41.11821
              Other  |   40.46675   1.057306      38.39423    42.53928
        --------------------------------------------------------------
        You can use aweights in graph bar in the same fashion:
        Code:
        . graph bar age [aweight = finalwgt], over(race)
        David Radwin
        Senior Researcher, California Competes
        californiacompetes.org
        Pronouns: He/Him

        Comment


        • #5
          Thank you both! We do care about the standard errors and CIs, so l collected the svy: mean estimates into a dataset and plotted them that way, as Leonardo also suggested.

          Comment

          Working...
          X