Mlogit macro with dummy variables - regression

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

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'
}

Stata's documentation for the foreach command indicates that it would work without the global macro, i.e.
. foreach xvar in age_median female marital ethnicity literacy "sesq2 sesq3 sesq4 sesq5" poor_health physical_median mental_median facility_fee time_clinic {
If you want to do it using a global macro, you'll need to use compound double-quotes to define a macro that includes quote marks:
. global SES `""sesq2 sesq3 sesq4 sesq5""'

Related

in Kdb, how do I assign a list of symbols each to a list of values

For example, I want to pass a dictionary with 10 pairs into a function to bypass the 8 valence limit. I then want the keys of the dictionary each to be assigned as a local variable to be assigned to their value and use that as the parameters for my function. Alternatively, is there a better way to pull this off?
I am afraid there is no way to functionally assign value to local scope variable. E.g.
{eval parse "a: 10";}1b
creates global variable a.
You may fix some scope name, e.g. .l, keep variables there and clear scope before function return, e.g.:
{
eval each (:),'flip (`$".l.",/:string key x;value x);
r: .l.a + .l.b + .l.c;
delete from `.l;
r
}`a`b`c!1 2 3
But getting values directly from input dictionary (like x[`a]) seems easier and clearer approach.
apply helps to simplify invoking other functions, using dictionary values as parameters. This may be what you are looking for. E.g.
{
f: {x+y+z};
f . x`a`b`c
}`a`b`c!1 2 3

Stata error i ambiguous abbreviation when trying to run a loop in

I'm trying to run a regression for the variables below by state to compare them, but I cannot get the loop to run. This is the coe I have so far
foreach i in statefip{
reg lnwage female parent age fulltime educfin i
}
As far as Stata is concerned, you have set up a loop over one item, the variable statefip.
Also, the macro you declared is called i. It's not illegal not to reference that in the loop, but if you did reference it, you would need to refer to it using this kind of syntax:
sysuse auto, clear
foreach i in mpg weight price {
su `i'
}
Note the single quotation marks around the local macro name.
Since you didn't refer to the macro, Stata can only interpret your reference to i as a reference to a variable (or scalar), and it turns out that in your dataset the reference is ambiguous. So, that's the immediate error.
But there's a deeper problem. You're interpreting in as meaning that Stata will look inside the variable you mention and loop over its distinct values. That is not at all how to loop over the distinct values of any variable. foreach doesn't work that way.
It is easier to use by: here, so that something more like
bysort statefip : reg lnwage female parent age fulltime educfin
is more likely to work. But that is not necessarily ideal, depending on what you want to do with the results. But at this point I don't know what you want to do with the results, so I will stop there.
The FAQ here may be a good start.
In order to use a loop variable you need to enclose it with a leading left quote and trailing single right quote:
foreach i in statefip {
reg lnwage female parent age fulltime educfin `i'
}
However, it is unclear what statefip is as you do not use quote enclosures for local macros, `statefip', or dollar sign $ for global macros, $statefip. As used above, only one regression will be run in loop where you pass in statefip as last variable of regression call:
reg lnwage female parent age fulltime educfin statefip
If statefip is a variable in the dataset and you want to run iterations off each unique value, then you need to iterate off its levels and filter data with if in the regression call. Below runs separate regressions for each unique value contained in statefip variable.
levelsof statefip, local(fips)
foreach f in `fips' {
display "************ `f' ***********"
reg lnwage female parent age fulltime educfin if statefip == "`f'"
}

What is really happening when using variables?

I have a really basic question about something that I've never paid much attention to until now:
I noticed that when creating a function (in JS or Python) that uses a variable from the outer scope, the function is not defined using the value of the variable but rather the variable itself. So if I change the value of the variable the function will use the new value.
This is what I mean
let a = 10;
function printA(){
console.log(a);
}
printA(); // => 10
a = 20;
printA(); // => 20
a = 10
def printA():
print(a)
printA() # => 10
a = 20
printA() # => 20
I thought this was only going to work of objects because of the way you can modify an object inside a function but not primitive variables because there's no way to change their value without reasigning them. I guess this is a different story.
What I'm trying to understand is: when typing a variable name is typing its memory address what I'm really doing? Does this happen with all languages?
when I create a function like printA() that uses a variable that is not an argument, is the variable bound forever to the function by its address?
The variable a is "captured" by the function. The specifics of how that happens are usually implementation details and may result in the compiler/interpreter producing code that doesn't much resemble the original.
For instance, in C# (I know, not one of the languages you mentioned, but it's the one I'm most familiar with), the compiler will create a separate hidden class which actually contains fields for the variables that are captured by a lambda or nested function. It then accesses these fields rather than plain variables.
by its address
Variables don't typically have an address. For instance, every time you call a method, it will typically have an "activation record" of some kind created, that will typically contain its variables. But note that these records are not at some fixed location, which is how you can have parallel execution of methods, recursion, etc, without interference. (Some older BASICs did have fixed activation records, which is why they didn't allow for recursion). These activation records may typically be placed on some kind of stack.
But as I say, for captured variables, the compiler will typically need to do even more so that those variables aren't just stored in an activation record, and so that their lifetime is no longer tied to a single call.

Change a string variable into a set of categorical variables in one command

Say I have a categorical variable, for example a country column in a table.
How can I quickly add dummy variables for each category--WITH A RELEVANT NAME?
So if the column is for country, the variable for whether the person lives in the USA would be called USA not country16 or something.
This is pretty easy:
/* Make some fake data */
sysuse auto, clear
gen make_only = subinstr(lower(word(make,1)),".","",.)
/* Create meaningful dummies */
levelsof make_only, clean local(makes)
foreach m of local makes {
gen `m' = cond(make_only=="`m'",1,0)
}
However, it is probably easier to just use factor variables notation:
sencode make_only, label(make_only) replace
reg price i.make_only
list make price if make_only=="amc":make_only
Regression output will be nicely labeled, you don't create extra variables, and it's easy enough to refer to particular values.
sencode is written by Roger Newson and is available from SSC.

Lua - Multiple objects, linked values?

I recently looked up an example of simple Lua oop principles and modified it slightly, as seen below.
What I find trouble comprehending is the connection between elf.name and hobbit.name. Why is it that when I change the value of either, that it affects the other? I am aware that I could have set elf.name as local inside the function, but it wouldn't have had the same effect.
In contrast, changing the value of another.name has no effect on the other two. Is there a lasting connection between elf.name and hobbit.name? I thought they were treated as separate objects.
Thanks.
;^)
Zalokin
elf = {}
elf.name = "Frodo"
another = {}
function Character()
return elf
end
local hobbit = Character()
print ("elf.name set to Frodo")
print("hobbit.name - "..hobbit.name)
print("elf.name - "..elf.name.."\
")
hobbit.name = "Charlie"
print ("hobbit.name set to Charlie")
print("hobbit.name - "..hobbit.name)
print("elf.name - "..elf.name.."\
")
another.name = "Gary"
print ("hobbit.name set to Charlie and another.name set to Gary")
print("hobbit.name - "..hobbit.name)
print("elf.name - "..elf.name)
print("another.name - "..another.name.."\
")
Result: -
>>>>elf.name set to Frodo
>>>>hobbit.name - Frodo
>>>>elf.name - Frodo
>>>>
>>>>hobbit.name set to Charlie
>>>>hobbit.name - Charlie
>>>>elf.name - Charlie
>>>>
>>>>hobbit.name set to Charlie and another.name set to Gary
>>>>hobbit.name - Charlie
>>>>elf.name - Charlie
>>>>another.name - Gary
Any use of {}, is known as a table constructor. It creates a whole new table. When you do elf.name = "Frodo", you're modifying the table that elf points to. In your code, elf and another are initialized separately. On the other hand, hobbit is indirectly given a reference to elf. In other words, elf and hobbit are references to the same table.
function Character()
return elf
end
local hobbit = Character()
That's what you are wrong at. I belive lua is pass-by-reference. This way, your code doesn't work. Also, Hobbit shouldn't be instance of Elf - if Lua is pass-by-reference its natural that instances will share data. Also, at top elf's name is Frodo. I recommend you to remove it. All you need to do is do this like you did with another object.
EDIT: Lua IS pass-by-reference but only on tables and objects.
Quoting Lua 5.1 Reference Manual:
There are eight basic types in Lua: nil, boolean, number, string,
function, userdata, thread, and table. ....
Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only
references to them. Assignment, parameter passing, and function
returns always manipulate references to such values; these operations
do not imply any kind of copy.