Announcement

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

  • ASGEN : A Stata module for weighted average mean

    Thanks to Kit Baum, I have shared a new program asgen on SSC that computes weighted average mean. The program can be installed by
    Code:
    ssc install asgen
    Following are related details:


    Syntax

    [bysort varlist]: asgen newvar = exp [if] [in] [, weights(varname) by(varlist)]


    Description

    asgen creates a new variable from an existing variable or an expression. The new variable contains weighted average mean. The existing variable is supplied to asgen by the =exp. The weights are based on the values of an existing numeric variable, that is specified in the weights(varname) option.


    asgen accepts both the [if] [in] qualifiers for performing the required calculations on a subset of data. Further, the use of =exp can come handy in many situations where we want to make changes on the fly before we find the weighted average mean. For example, we might want to multiply the value of 100 to a variable before we find its weighted average. In such a case, the expression will look like:

    Code:
     asgen wmX = X * 100, w(Y)

    where wmX is a new variable to be created and shall contain the weighted average; X and Y are existing variables in the data set. Options

    weight(varname) is an optional option. Therefore, without this option, asgen works like egen command and finds simple mean


    Example 1: Weighted average mean for kstock using the variable mvalue as a weight

    Code:
    webuse grunfeld
     asgen WM_kstock = kstock, w(mvalue)

    Example 2: Weighted average mean using an expression

    .
    Code:
    asgen WM_kstock = kstock / invest, w(mvalue)
    This will divide the variable kstock on the variable invest before finding the weighted average mean.

    Example 3: Avoiding the use of [if] using an expression

    We use the [if] qualifier to perform calculation on a subset of the data. Using [if] condition, the resulting variable will have missing values where the condition is not true. There might be circumstance where we want to use [if] without encountering missing values in the new variable. With asgen, the use of an expression can come very handy in such a s situation. To proceed with an example, let us use astile [can be downloaded from ssc by: ssc install astile] to make five groups of firms based on the variable mvalue and five groups based on kstock.

    Code:
    astile size5 = mvalue, nq(5)
    astile kstock5 = kstock, nq(5)


    Now, let's find weighted average mean in each year for a combination of firms that are in the first quantile of mvalue (size == 1) and third quantile of kstock (kstock5 == 3) using mvalue as a weighting variable


    Code:
    bys year: asgen WM_invest = (kstock / (size5 ==1 & kstock5==3)), w(mvalue)
    Off-course, we could have done that using [if] qualifier, but that will generate missing values where the condition is not true

    Code:
    bys year: asgen WM_invest_IF = kstock if size5 ==1 & kstock5==3 , w(mvalue)
    sort id year
    The difference between WM_invest and WM_invest_IF is that the former spreads the results within years, and the later does not. The use of this trick is borrowed from the behavior of egen. Further details related to this trick can be read here in Nick Cox's column.
    *

    Comments for improvement of asgen are greatly appreciated.
    Last edited by Attaullah Shah; 10 Oct 2017, 22:06.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Associate Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    www.FinTechProfessor.com
    If you use MS Word, do check my asdoc program that easily sends Stata output to MS Word
Working...
X