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

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

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

Functional-style Programming in Red (Arguments passed by val vs by ref)

I've purchased and making my way through the Red Programming book recently available from Pakt.
On p. 112 in the chapter relating to functions, something caught my eye:
Scalar values are passed by value, which means that a copy of the value is sent to the function
Other values are passed by reference, which means that a reference to the value is sent to the function
When I first got into ML and the Functional paradigm, what I loved was the fact that nothing was passed around by reference. This gave me the confidence that whatever I was doing inside a function would not affect anything outside of it.
But here in Red, series (which I could compare to a ML list), are passed to a function by reference, which means whatever I do to it inside a function will most certainly alter the list at the source.
Are there any options to pass a series by value, as it does with scalars, or is the only way to achieve this to make a copy of the parameter, and have the function work on this copy?
Regards,
yves

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.

Why does it say can't assign to literal in my function?

I am trying to make a menu calculator in which the user inputs items and the program will add up the order numbers and output the cost. I have done some of the code already but in the function in says can't assign to literal.
itemlist=["1","2","3","4","5","6","7","8","9"]
def itemcost():
1=3.50 #can't assign literal error is here
2=2.50
3=4.00
4=3.50
5=1.75
6=1.50
7=2.25
8=3.75
9=1.25
return itemcost
order=int(input("Enter order"))
while items in order:
itemcost+str(order)
First, some good information to put in a question is the language and platform you are using. Your error comment in the code IS helpful, however.
What your code is trying to do is assign the value 3.50 to the VALUE 1. You can't change the value of pure numbers for obvious reasons. What I think you want is:
itemlist["1"]=3.50
On line 4 (and lines 5-12 after it) the 1 is read as the value one, i.e. a literal value. If you want to assign the value 3.50 to a variable, you will need to name the variable something that cannot be interpreted as a number and does not begin with a number, such as _1 or var1.

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