Announcement

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

  • Equation specific weights and _eqlist

    Hello Statalist

    I am trying to write a program that contains multiple equations where the weights used for each equation can be different. Let's call it eqspec for now. I would like the syntax to be something like
    Code:
    eqspec (reg y1 x1 [weight], options) (reg y2 x2 [weight], options) , global_options
    To parse the equations I am using the _eqlist class. This works fine when there are no weights specified, but fails when I include weights, as the following example shows.
    Code:
    program define eqspec
    syntax anything(equalok) [pweight]
        gettoken lhs rhs: anything, parse("=")
        tempname o
        .`o' = ._eqlist.new, numdepvars(1) wtypes(pw)
        .`o'.parse `lhs'
        return list
        local k_eq = `.`o'.eq count' // Number of univariate models
        forvalues k = 1/`k_eq' {
            local eqname`k' `.`o'.eq name `k'' // Univariate model for equation k
            di as res `"`eqname`k''"'
        }
    end
    
    * Test on auto data
    sysuse auto, clear
    * Generate weight variables
      gen pw1 = rnormal(1,.1)
      gen pw3 = rnormal(1,.05)
    
    * No weights defined
    eqspec (logit: foreign) (reg: length)
    
    * Global weight variable
    eqspec (logit: foreign) (reg: length) [pw=pw1]
    
    * Equation-specific weight variables - returns error.
    eqspec (logit: foreign [pw=pw1]) (reg: length [pw=pw3])
    Should _eqlist allow equation specific weights (it is largely undocumented)?
    Is there is a better / alternative way to parse the equations given to eqspec?

    Any advice gratefully received. Tim

  • #2
    Dear Tim,
    clearly _eqlist class manages equation specific weights.
    ​You made a mistake by parsing the local `lhs' instead of `0' which actually contains what you need to parse, i.e. the full command line.

    program define eqspec
    syntax anything(equalok) [pweight]
    gettoken lhs rhs: anything, parse("=")
    tempname o
    .`o' = ._eqlist.new, numdepvars(1) wtypes(pw)
    .`o'.parse `0'
    local k_eq = `.`o'.eq count' // Number of univariate models
    forvalues k = 1/`k_eq' {
    local eqname`k' `.`o'.eq name `k'' // Name for equation k
    local eqweights`k' `.`o'.eq wgt `k'' // Weight exp for equation k
    di as res `"`eqname`k''"'
    di as res `"`eqweights`k''"'
    }
    end

    * Test on auto data
    sysuse auto, clear
    * Generate weight variables
    gen pw1 = rnormal(1,.1)
    gen pw3 = rnormal(1,.05)

    * Equation-specific weight variables
    eqspec (logit: foreign [pw=pw1]) (reg: length [pw=pw3])


    Hope this helps
    F
    Federico

    Comment

    Working...
    X