Search code examples
regressionstatastata-macros

Mlogit macro with dummy variables


I am new to Stata and macros.

I am trying to loop over several variables to generate estimates from the mlogit command and then save them in datasets. That portion is working well.

The problem I have is a categorical variable that I need to split into dummy variables:

global mypath "/Volumes/NO NAME/Dissertation/Data/AIM 2"
use "$mypath/AIM 2 DATA"
global SES "sesq2 sesq3 sesq4 sesq5"

/*regression*/

foreach xvar in age_median female marital ethnicity literacy $SES poor_health physical_median mental_median facility_fee time_clinic {
    mlogit trauma_main `xvar', b(5) vce(cluster ea_id) rrr
    parmest, saving("$mypath/multi_`xvar'.dta", replace)
}

I thought that by setting SES as a global variable, the loop would treat that as one set of variables, but I was mistaken. The code loops over every variable in $SES so I end up with each dummy variable regressed onto trauma_main separately, which is not what I want.

Is there a way to "tell" Stata to treat the dummy variables as one block? Additionally, I know that I could do i.SES and using that does work fine, but the reference group that is used is not the one that I want. I have googled how to set the reference group for something like i.var, but I am coming up with nothing useful, likely because I am using the wrong search terms.

Thank you in advance for any advice.

Maggie


Solution

  • You do not need to split your categorical variable into dummies. You can use the factor variables notation (i.) instead. This is documented in help fvvarlist. With factor variables, a change of the reference category is straightforward.

    Here is an example. The site variable has three categories. By default site = 1 is the reference category for the categorical variable:

    webuse sysdsn1, clear

    foreach v in age male i.site {
    mlogit insure `v'
    }

    With ib you can set the reference category to any desired level. If you want site = 2 as the reference, you can do the following:

    foreach v in age male ib2.site {
    mlogit insure `v'
    }