Announcement

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

  • margin commands after replicate weights

    Hello,
    I am using a survey that recommends the follow syntax for weighted analysis when using replicate weights:
    svyset[pweight=perwt], vce(brr) brrweight(repwtp1-repwtp80) fay(.5)mse

    After my regression, I want to compute margins with standard errors based on balanced repeated replications (brr). I followed the advice on the Stata manual svy.pdf page 115.

    Here is my code:

    capture program drop mymargins
    program mymargins, eclass
    version 13
    syntax anything [if] [iw pw]
    if "`weight'" != "" {
    local wgtexp "[`weight' `exp']"
    }
    set buildfvinfo on
    `anything' `if' `wgtexp'
    margins groups1, at (age=(50(5)80))subpop(samp) atmeans
    end

    svyset[pweight=perwt], vce(brr) brrweight(repwtp1-repwtp80) fay(.5)mse
    local mycmdline svy, subpop(samp): logistic physdis ib4.groups1 age female
    quietly mymargins `mycmdline'
    svy brr _b: mymargins `mycmdline'


    However, I am getting an error that says "options not allowed r(101);" after the line quietly mymargins `mycmdline'

    Thanks a lot for your help,
    Susana

  • #2
    I think I see the problem. The solution is less clear.

    When you define local macro mycmdline, it contains a comma after svy. When that local is passed go your program mymargins as the "anything", the comma gets parsed as setting off options (as it would in a regular command) rather than as a part of `anything.' While the syntax for -syntax anything- allows specifiers that enable you to force -if-, -in- or -=- tbe parsed as part of "anything," I am not aware of any way to tell -syntax- to treat the comma as part of `anything'. Since the specification of -mymargins- doesn't allow for options, and -syntax- thinks you are passing options along, you are getting the error message you received.

    I can think of one solution, but it is a bit kludgy. Modify the syntax of -mymargins- so that your intended command (which you define in local mycmdline and which you intend to be the content of `anything') is an option in your program. So it would look something like this:

    Code:
     capture program drop mymargins
     program mymargins, eclass
     version 13
     syntax [if] [iw pw], command(string asis)
     if "`weight'" != "" {
        local wgtexp "[`weight' `exp']"
     }
     set buildfvinfo on
     `command' `if' `wgtexp'
     margins groups1, at (age=(50(5)80))subpop(samp) atmeans
     end
    
     svyset[pweight=perwt], vce(brr) brrweight(repwtp1-repwtp80) fay(.5)mse
     local mycmdline svy, subpop(samp): logistic physdis ib4.groups1 age female
     quietly mymargins, command(`mycmdline')
     svy brr _b: mymargins, command(`mycmdline')
    It's not elegant, and kind of un-Stataish. But it should work.

    Maybe there is a way to keep your original approach and prevent Stata from interpreting that comma that way--but I don't know how.
    Last edited by Clyde Schechter; 30 Oct 2014, 11:27.

    Comment


    • #3
      Thanks a lot for your prompt response and help. I run the code you suggested and everything run fine until
      the last line or svy brr _b: mymargins, command(`mycmdline') After that line: I got the following message: last estimates not found Thanks again! Susana

      Comment


      • #4
        Perhaps the above description is not entirely clear
        Here is my code with the error message.

        .
        Code capture program drop mymargins program mymargins, eclass version 13 syntax [if] [iw pw], command(string asis) if "`weight'" != "" { local wgtexp "[`weight' `exp']" } set buildfvinfo on `command' `if' `wgtexp' margins groups1, at (age=(50(5)80))subpop(samp) atmeans end svyset[pweight=perwt], vce(brr) brrweight(repwtp1-repwtp80) fay(.5)mse local mycmdline svy, subpop(samp): logistic physdis ib4.groups1 age female quietly mymargins, command(`mycmdline') svy brr _b: mymargins, command(`mycmdline') last estimates not found
        r(301);

        end of do-file

        r(301);

        Comment


        • #5
          I'm not entirely sure, but your program -mymargins- does not appear to be properly structured to work with the -svy:- prefix. See the program properties of the [P] manual for how to do that. If fixing that doesn't solve the problem, please show what happens when you run this with -trace- set on (at tracedepth 1 should be sufficient).

          By the way, to post a code block, you need to type
          Code:
           (including the square brackets) at the start, and
          at the end. What you posted came out basically as a jumble. To get the code block delimiters without typing them, use the advanced editor (click on the underlined A) and then click on the # button. The code block delimiters will appear and you can just paste between them.

          Comment


          • #6
            Hi Clyde:

            I am doing a similar exercise with the following code:

            Code:
             capture program drop mymargins
             program mymargins, eclass
             version 16
             syntax [if] [iw pw], command(string asis)
             if "`weight'" != "" {
                local wgtexp "[`weight' `exp']"
             }
             set buildfvinfo on
             `command' `if' `wgtexp'
             margins adhd_dx, at(alpha=(0.0108772 0.0177354 0.0281493 0.0689525 0.225))
             end
            
             svyset [pw=p_weight], vce(brr) brrw( brrw* )
             local mycmdline svy:twopm adj_er_tot_exp age_6_10 age_10_13 sex num_cond_0_3 father_num_cond_0_3 mother_num_cond_0_3 logfi logmi i.RACEX i.region i.insur i.adhd_dx##c.alpha, f(probit) s(glm, family(gamma) link(log))
             quietly mymargins, command(`mycmdline')
             svy brr _b: mymargins, command(`mycmdline')
            I also get the same error.

            Code:
                  name:  <unnamed>
                   log:  C:\Users\nan518\Desktop\trace.txt
              log type:  text
             opened on:  12 Apr 2022, 19:09:18
            
            . set tracedepth 1
            
            .  capture program drop mymargins
            
            . 
            .  program mymargins, eclass
              1. 
            .  version 16
              2. 
            .  syntax [if] [iw pw], command(string asis)
              3. 
            .  if "`weight'" != "" {
              4. 
            .     local wgtexp "[`weight' `exp']"
              5. 
            .  }
              6. 
            .  set buildfvinfo on
              7. 
            .  `command' `if' `wgtexp'
              8. 
            .  margins adhd_dx, at(alpha=(0.0108772 0.0177354 0.0281493 0.0689525 0.225))
              9. 
            .  end
            
            . 
            . 
            . 
            .  svyset [pw=p_weight], vce(brr) brrw( brrw* )
            ---------------------------------------------------------------------------------------------------------------------------------------- begin svyset ---
            - if _caller() < 9 {
              svyset_8 `0'
              exit
              }
            - capture Display
            - if !c(rc) {
            - local oldset `"`r(settings)'"'
            = local oldset `"_n [pweight= p_weight], brrweight(brrwBRR1 brrwBRR2 brrwBRR3 brrwBRR4 brrwBRR5 brrwBRR6 brrwBRR7 brrwBRR8 brrwBRR9 brrwBRR10 brrwBRR11 b
            > rrwBRR12 brrwBRR13 brrwBRR14 brrwBRR15 brrwBRR16 brrwBRR17 brrwBRR18 brrwBRR19 brrwBRR20 brrwBRR21 brrwBRR22 brrwBRR23 brrwBRR24 brrwBRR25 brrwBRR26 br
            > rwBRR27 brrwBRR28 brrwBRR29 brrwBRR30 brrwBRR31 brrwBRR32 brrwBRR33 brrwBRR34 brrwBRR35 brrwBRR36 brrwBRR37 brrwBRR38 brrwBRR39 brrwBRR40 brrwBRR41 brr
            > wBRR42 brrwBRR43 brrwBRR44 brrwBRR45 brrwBRR46 brrwBRR47 brrwBRR48 brrwBRR49 brrwBRR50 brrwBRR51 brrwBRR52 brrwBRR53 brrwBRR54 brrwBRR55 brrwBRR56 brrw
            > BRR57 brrwBRR58 brrwBRR59 brrwBRR60 brrwBRR61 brrwBRR62 brrwBRR63 brrwBRR64 brrwBRR65 brrwBRR66 brrwBRR67 brrwBRR68 brrwBRR69 brrwBRR70 brrwBRR71 brrwB
            > RR72 brrwBRR73 brrwBRR74 brrwBRR75 brrwBRR76 brrwBRR77 brrwBRR78 brrwBRR79 brrwBRR80 brrwBRR81 brrwBRR82 brrwBRR83 brrwBRR84 brrwBRR85 brrwBRR86 brrwBR
            > R87 brrwBRR88 brrwBRR89 brrwBRR90 brrwBRR91 brrwBRR92 brrwBRR93 brrwBRR94 brrwBRR95 brrwBRR96 brrwBRR97 brrwBRR98 brrwBRR99 brrwBRR100 brrwBRR101 brrwB
            > RR102 brrwBRR103 brrwBRR104 brrwBRR105 brrwBRR106 brrwBRR107 brrwBRR108 brrwBRR109 brrwBRR110 brrwBRR111 brrwBRR112 brrwBRR113 brrwBRR114 brrwBRR115 br
            > rwBRR116 brrwBRR117 brrwBRR118 brrwBRR119 brrwBRR120 brrwBRR121 brrwBRR122 brrwBRR123 brrwBRR124 brrwBRR125 brrwBRR126 brrwBRR127 brrwBRR128) singleuni
            > t(missing) vce(brr)"'
            - }
            - capture noisily SvySet `0'
            = capture noisily SvySet [pw=p_weight], vce(brr) brrw( brrw* )
            
                  pweight: p_weight
                      VCE: brr
                      MSE: off
                brrweight: brrwBRR1 .. brrwBRR128
              Single unit: missing
                 Strata 1: <one>
                     SU 1: <observations>
                    FPC 1: <zero>
            - if c(rc) {
              local rc = c(rc)
              capture SvySet `oldset'
              exit `rc'
              }
            ------------------------------------------------------------------------------------------------------------------------------------------ end svyset ---
            
            . 
            .  local mycmdline svy:twopm adj_er_tot_exp age_6_10 age_10_13 sex num_cond_0_3 father_num_cond_0_3 mother_num_cond_0_3 logfi logmi i.RACEX i.region i.in
            > sur i.adhd_dx##c.alpha, f(probit) s(glm, family(gamma) link(log))
            
            . 
            .  quietly mymargins, command(`mycmdline')
            ------------------------------------------------------------------------------------------------------------------------------------- begin mymargins ---
            - version 16
            - syntax [if] [iw pw], command(string asis)
            - if "`weight'" != "" {
            = if "" != "" {
              local wgtexp "[`weight' `exp']"
              }
            - set buildfvinfo on
            - `command' `if' `wgtexp'
            = svy:twopm adj_er_tot_exp age_6_10 age_10_13 sex num_cond_0_3 father_num_cond_0_3 mother_num_cond_0_3 logfi logmi i.RACEX i.region i.insur i.adhd_dx##c.
            > alpha, f(probit) s(glm, family(gamma) link(log))  
            - margins adhd_dx, at(alpha=(0.0108772 0.0177354 0.0281493 0.0689525 0.225))
            --------------------------------------------------------------------------------------------------------------------------------------- end mymargins ---
            
            . 
            .  svy brr _b: mymargins, command(`mycmdline')
            ------------------------------------------------------------------------------------------------------------------------------------------- begin svy ---
            - version 9
            - local version : di "version " string(_caller()) ":"
            - set prefix svy
            - capture _on_colon_parse `0'
            = capture _on_colon_parse brr _b: mymargins, command(svy:twopm adj_er_tot_exp age_6_10 age_10_13 sex num_cond_0_3 father_num_cond_0_3 mother_num_cond_0_3
            >  logfi logmi i.RACEX i.region i.insur i.adhd_dx##c.alpha, f(probit) s(glm, family(gamma) link(log)))
            - if c(rc) | `"`s(after)'"' == "" {
            = if c(rc) | `" mymargins, command(svy:twopm adj_er_tot_exp age_6_10 age_10_13 sex num_cond_0_3 father_num_cond_0_3 mother_num_cond_0_3 logfi logmi i.RAC
            > EX i.region i.insur i.adhd_dx##c.alpha, f(probit) s(glm, family(gamma) link(log)))"' == "" {
              if !c(rc) {
              local 0 `"`s(before)'"'
              }
              if replay() {
              if "`e(prefix)'" != "svy" {
              error 301
              }
              `version' Display `0'
              exit
              }
              }
            - quietly ssd query
            - if (r(isSSD)) {
              di as err "svy not possible with summary statistic data"
              exit 111
              }
            - `version' SvyEst `0'
            = version 16.1: SvyEst brr _b: mymargins, command(svy:twopm adj_er_tot_exp age_6_10 age_10_13 sex num_cond_0_3 father_num_cond_0_3 mother_num_cond_0_3 lo
            > gfi logmi i.RACEX i.region i.insur i.adhd_dx##c.alpha, f(probit) s(glm, family(gamma) link(log)))
            last estimates not found
            --------------------------------------------------------------------------------------------------------------------------------------------- end svy ---
            r(301);

            Comment


            • #7
              Your program as currently written does not satisfy all of the requirements for use with the -svy:- prefix. See -help program_properties-.

              But, more important, the command -margins- within it cannot be used with -svy:-. (When getting margins from a -svy:- estimation, you use -svy:- with the estimation command, but not with -margins-.)

              Comment

              Working...
              X