Announcement

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

  • Practice post - Help with macro lists (for a mixed logit model)

    Summary: I am looking for help with a macro to cycle through a list of variables in groups of two or three. I want to include all the variables in the list except two or three in one part of a model, and include the excluded variables elsewhere in the model. The macro is to cycle through the list with a different group of variables excluded each time.

    Detail: I have conducted a discrete choice experiment (DCE) survey of 300 people, each answering 16 choice sets with 3 options (i.e. total observations = 14400). I would like to analyse the results with a mixed logit model using the user written mixlogit command programmed by Prof Arne Hole. To select which variables should be included in the model as fixed and which at random, I usually run the model repeatedly, each time with the (dummy coded) levels of one attribute as random and all others as fixed. Any variables which show significant heterogeneity individually I then include as random in the final model. I usually do this manually, but the experiment has three versions and was replicated in another sample so there are many iterations, and I would like to use a macro to cycle through the models.

    What I've tried: I have assigned each group of variables (the levels for each attribute) a local macro name (eg social, depress, etc), so that they can be referred to in groups. I've then assigned a local macro name to the list of all of these groups of variables ('all'). I then run a macro using foreach to loop through the list of grouped variables (called 'x'). Within that macro, I create a new macro variable ('b') which is the list of all grouped variables, minus the one in the 'for each' statement ('x') using the macro lists A - B statement. I then have the mixlogit model, with 'b' (all the variables except the one in the for each statement) as fixed and 'x' (the current variable in the foreach statement as random.

    The problem: I get the following message from STATA "The variable sideeffect1 is specified to have both fixed and random coefficients: r(498);" This implies that the macro is not removing the macro variable x from the macro variable b. (I've tested this by manually including a variable as both fixed and random and got the same result).

    Code:
    clear 
    use "C:\Users\filepath\ChoiceCA", replace
    
    **Assign 'local' macro names to the variables that represent the (dummy coded) levels for each attribute
    local social social1 social2 social3
    local depress depressed1 depressed2 depressed3
    local pain pain1 pain2 pain3
    local tired tired1 tired2 tired3
    local sleep sleep1 sleep2 sleep3
    local appetite appetite1 appetite2 appetite3
    local nausea nausea1 nausea2 nausea3
    local digest digestive1 digestive2 digestive3
    local side sideeffect1 sideeffect3 sideeffect6
    local treat treat6w treat3m
    local walk walk1 walk2 walk3
    local work work1 work2 work3
    
    **Assign a 'local' macro name to the list of all attributes (using the previously defined macro names)
    local all `social' `depress' `pain' `tired' `social' `sleep' `appetite' `nausea' `digest' `side' `treat' `walk' `work' 
    
    
    **Run a macro to loop through each attribute and run the mixlogit with the selected attribute as random, and all others as fixed.
    
    foreach x in `all' {
    local b: list local(all) - local(x)
    mixlogit choice01 `b' extra_cont, group (OBSID) id(RID) rand(`x' const) nrep(10)
    estat ic
    estimates store MXL`x'
    }
    Code:
    
    
    Data: I have included an example of the data with the first 100 observations, and including only 4 of the attribute variables (social1 social2 social3 depressed1 depressed2 depressed3 pain1 pain2 pain3 extra_cont)
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(RID OBSID choice01 social1 social2 social3 depressed1 depressed2 depressed3 pain1 pain2 pain3 extra_cont const)
    69156  1 0 0 0 1 0 0 0 0 0 0       1 1
    69156  1 1 1 0 0 1 0 0 1 0 0       0 0
    69156  1 0 0 1 0 1 0 0 0 0 0       1 0
    69156  2 1 0 1 0 1 0 0 0 0 1     260 1
    69156  2 0 1 0 0 0 1 0 0 0 1     260 0
    69156  2 0 1 0 0 1 0 0 1 0 0       0 0
    69156  3 0 0 0 1 0 0 1 0 1 0       4 1
    69156  3 0 1 0 0 1 0 0 1 0 0       0 0
    69156  3 1 0 0 1 0 0 1 1 0 0      52 0
    69156  4 0 0 0 1 0 0 0 1 0 0       4 0
    69156  4 0 0 0 0 0 0 1 1 0 0       4 1
    69156  4 1 1 0 0 1 0 0 1 0 0       0 0
    69156  5 1 1 0 0 0 1 0 0 1 0      52 1
    69156  5 0 1 0 0 1 0 0 1 0 0       0 0
    69156  5 0 0 0 0 0 0 1 0 1 0      52 0
    69156  6 0 1 0 0 1 0 0 1 0 0       0 0
    69156  6 1 0 1 0 0 1 0 0 0 0     260 0
    69156  6 0 0 1 0 0 1 0 1 0 0      52 1
    69156  7 0 1 0 0 1 0 0 1 0 0       0 0
    69156  7 0 1 0 0 1 0 0 0 0 1       1 0
    69156  7 1 1 0 0 1 0 0 0 0 0     260 1
    69156  8 0 0 0 0 0 0 0 0 1 0       4 0
    69156  8 0 0 0 0 0 0 0 0 0 1       1 1
    69156  8 1 1 0 0 1 0 0 1 0 0       0 0
    69156  9 0 0 0 1 0 1 0 1 0 0 .142857 1
    69156  9 0 0 0 1 0 1 0 1 0 0       1 0
    69156  9 1 1 0 0 1 0 0 1 0 0       0 0
    69156 10 0 0 1 0 1 0 0 0 0 1       1 1
    69156 10 0 1 0 0 0 0 0 0 1 0       1 0
    69156 10 1 1 0 0 1 0 0 1 0 0       0 0
    69156 11 0 1 0 0 0 0 0 0 0 1      52 0
    69156 11 0 1 0 0 1 0 0 1 0 0       0 0
    69156 11 1 1 0 0 0 0 0 0 0 1       8 1
    69156 12 0 0 0 0 1 0 0 0 0 1 .142857 0
    69156 12 0 1 0 0 0 1 0 0 0 0 .142857 1
    69156 12 1 1 0 0 1 0 0 1 0 0       0 0
    69156 13 0 1 0 0 1 0 0 1 0 0       0 0
    69156 13 1 0 0 0 0 0 1 1 0 0      52 1
    69156 13 0 0 0 1 0 1 0 0 0 0      52 0
    69156 14 0 0 1 0 0 0 1 0 0 0 .142857 0
    69156 14 0 1 0 0 1 0 0 1 0 0       0 0
    69156 14 1 0 1 0 0 0 1 0 0 0      52 1
    69156 15 0 0 1 0 0 0 1 1 0 0       8 0
    69156 15 0 1 0 0 1 0 0 1 0 0       0 0
    69156 15 1 0 0 1 0 0 0 0 1 0       8 1
    69156 16 0 0 0 0 1 0 0 0 1 0       8 0
    69156 16 0 0 0 0 1 0 0 0 1 0       1 1
    69156 16 1 1 0 0 1 0 0 1 0 0       0 0
    69168 17 1 1 0 0 1 0 0 1 0 0       0 0
    69168 17 0 0 0 1 0 1 0 0 0 1       4 1
    69168 17 0 0 0 1 0 1 0 0 1 0      52 0
    69168 18 0 0 0 1 0 1 0 0 0 1     260 1
    69168 18 1 0 1 0 0 0 1 0 0 1     260 0
    69168 18 0 1 0 0 1 0 0 1 0 0       0 0
    69168 19 0 0 0 1 0 1 0 0 1 0      52 0
    69168 19 1 0 0 0 1 0 0 0 1 0      52 1
    69168 19 0 1 0 0 1 0 0 1 0 0       0 0
    69168 20 0 0 0 0 1 0 0 1 0 0       4 0
    69168 20 0 1 0 0 1 0 0 1 0 0       0 0
    69168 20 1 1 0 0 0 0 0 1 0 0       4 1
    69168 21 0 0 0 0 1 0 0 1 0 0       4 0
    69168 21 0 0 0 0 1 0 0 0 1 0       1 1
    69168 21 1 1 0 0 1 0 0 1 0 0       0 0
    69168 22 1 0 1 0 0 0 1 0 0 0       1 1
    69168 22 0 1 0 0 1 0 0 1 0 0       0 0
    69168 22 0 1 0 0 0 0 0 0 0 0       1 0
    69168 23 1 1 0 0 0 0 0 1 0 0     260 1
    69168 23 0 1 0 0 0 0 0 0 0 0       1 0
    69168 23 0 1 0 0 1 0 0 1 0 0       0 0
    69168 24 0 1 0 0 1 0 0 1 0 0       0 0
    69168 24 1 0 1 0 0 0 1 0 0 1     260 0
    69168 24 0 0 1 0 0 0 1 0 0 0      52 1
    69168 25 0 1 0 0 1 0 0 1 0 0       0 0
    69168 25 0 0 0 0 0 0 0 0 0 0      52 1
    69168 25 1 0 0 1 0 0 1 0 0 1      52 0
    69168 26 0 0 0 1 0 0 1 0 0 1       8 1
    69168 26 1 0 1 0 0 1 0 0 1 0       8 0
    69168 26 0 1 0 0 1 0 0 1 0 0       0 0
    69168 27 1 1 0 0 0 0 1 0 1 0      52 0
    69168 27 0 1 0 0 0 0 1 0 1 0       8 1
    69168 27 0 1 0 0 1 0 0 1 0 0       0 0
    69168 28 1 0 1 0 0 0 0 1 0 0      52 1
    69168 28 0 1 0 0 1 0 0 1 0 0       0 0
    69168 28 0 0 1 0 0 0 0 1 0 0 .142857 0
    69168 29 0 0 0 0 0 0 0 0 0 0 .142857 0
    69168 29 0 1 0 0 1 0 0 1 0 0       0 0
    69168 29 1 1 0 0 1 0 0 1 0 0 .142857 1
    69168 30 0 1 0 0 1 0 0 1 0 0       0 0
    69168 30 1 1 0 0 1 0 0 1 0 0       1 0
    69168 30 0 0 1 0 0 1 0 0 1 0       1 1
    69168 31 0 1 0 0 1 0 0 1 0 0       0 0
    69168 31 1 0 0 0 0 1 0 0 0 1       8 0
    69168 31 0 0 0 0 0 1 0 0 0 1       1 1
    69168 32 0 0 0 1 1 0 0 0 0 0 .142857 1
    69168 32 0 0 0 1 1 0 0 0 0 0       1 0
    69168 32 1 1 0 0 1 0 0 1 0 0       0 0
    69169 33 1 0 0 1 0 0 1 1 0 0       4 0
    69169 33 0 1 0 0 1 0 0 1 0 0       0 0
    69169 33 0 0 0 1 0 0 1 0 1 0       1 1
    69169 34 1 0 0 1 0 0 1 0 1 0      52 1
    end
Working...
X