Number of groups in esttab (after xtmixed) - stata

i'd like to add the number of groups to a esttab output after fitting a multilevel model in Stata (xtmixed). I found this post and understand that the number of groups is stores in the matrix e(N_g). However, I do not understand, how to use the value in this matrix and add it to the output. I tried estadd but did not manage to get it work. I also did not find something about using a single cell of a matrix in an output in the estout manual.
Any ideas on how to add the number of gorups to a esttab output? Or maybe experiences with adding single values from a matrix?
Thx!!!
P.S. This is the output of matrix list e(N_g)
symmetric e(N_g)[1,1]
c1
r1 117

Estadd is the way to go, here is an example:
clear all
webuse pig
// model
eststo: xtmixed weight week || id:, vce(robust)
// estadd portion - pull the single element of the matrix into a local and estadd it
matrix N_g = e(N_g)
local groups = N_g[1,1]
estadd local groups `groups'
// add groups in the stats option and it will appear at the bottom along
// with the number of observations in this case
esttab ., stats(N groups)

Actually, esttab, stats(N_g, labels("N")) is sufficient. estout can recognize N_g.

Related

Trying to export p values

I'm trying to write a loop to conduct individual t-tests for a list of variables (tab1) and export the means and p-values to Excel using the putexcel command. Right now my code looks like this:
putexcel set "Ttests.xlsx", sheet("t_test") replace
local n_models: word count `tab1'
forval i=1/`n_models' {
mat T=J(`n_models',4,.)
foreach x of tab1 {
ttest `x', by(var)
mat T[`i',1] = r(mu_1)
mat T[`i',2] = r(mu_2)
mat T[`'i,3] = r(mu_1) - r(mu_2)
mat T[`i',4] = r(p)
}
}
putexcel A1= matrix(T)
Unfortunately right now I'm only getting the means/p-values for the first variable of tab1. What am I doing wrong?
This isn't a minimal, complete or verifiable example. See https://stackoverflow.com/help/mcve for the standard here.
There is no data example.
The local macro tab1 isn't defined.
Even with fixes to those, the code is illegal as of tab1 is illegal in the foreach loop and the instruction to put stuff in the third column of the matrix is mangled as the punctuation is wrong. That comes from paraphrasing code, not showing us a version that does what you say.
So, what's key is to show us code that does what you say.
All that said, your problems are conceptual:
Bigger deal 1 You have two nested loops, over a counter and over the variables you want as response. But you need just one loop. You want to loop over the variables and at the same time bump up a counter. Or, you want to loop over a counter and at the same time pick another variable. It doesn't matter which way you do it, at least to Stata.
Bigger deal 2 What happens with (what I guess is) your code? Trace it through. Suppose your number of variables is 4. So, last time around the outer loop, you reset the entire matrix to missings, and then in the inner loop cycle around your variables and each time try to put results for each variable in the 4th row of your matrix (i.e. the same row!). Contrary to your report, I guess that what you see in the spreadsheet is results for the last variable named, not the first.
I think you want code more like this:
sysuse auto, clear
local tab1 mpg price
local var foreign
putexcel set "Ttests.xlsx", sheet("t_test") replace
local n_models: word count `tab1'
mat T = J(`n_models', 4, .)
tokenize `tab1'
forval i=1/`n_models' {
ttest ``i'', by(`var')
mat T[`i',1] = r(mu_1)
mat T[`i',2] = r(mu_2)
mat T[`i',3] = r(mu_1) - r(mu_2)
mat T[`i',4] = r(p)
}
putexcel A1 = matrix(T)

Stata xtline overlayed plot for multiple groups

I am attempting to produce an overlayed -xtline- plot that distinguishes between males and females (or any number of multiple groups) by displaying different plot styles for each group. I chose to recast the xtline plot as "connected" and show males using circle markers and females as triangle markers. Taking cues from this question on Statalist, I produced code similar to what is below. When I try this solution Stata produces the "too many options" error, which is perhaps predictable given the large number of unique persons. I am aware of this solution which employs combined graphs but that is also not practical given the large number of unique individuals in my data.
Does a more simple solution to this problem exist? Does Stata have the capacity to overlay multiple -xtline- plots like it can -twoway- plots?
The code below, using publicly available data from UCLA's excellent Stata guide shows my basic code and reproduces the error:
use http://www.ats.ucla.edu/stat/stata/examples/alda/data/alcohol1_pp, clear
xtset id age
gsort -male id
qui levelsof id if !male, loc(fidlevs)
qui levelsof id if male, loc(midlevs)
qui levelsof id, loc(alllevs)
tokenize `alllevs'
loc len_f : word count `fidlevs'
loc len_m : word count `midlevs'
loc len_all : word count `alllevs'
loc start_f = `len_all' - `len_f'
forval i = 1/`len_all' {
if `i' < `start_f' {
loc m_plot_opt "`m_plot_opt' plot`i'opts(recast(connected) mcolor(black) msize(medsmall) msymbol(circle) lcolor(black) lwidth(medthin) lpattern(solid))"
}
else if `i' >= `start_f' {
loc f_plot_opt "`f_plot_opt' plot`i'opts(recast(connected) mcolor(black) msize(medsmall) msymbol(triangle) lcolor(black) lwidth(medthin) lpattern(solid))"
}
}
di "xtline alcuse, legend(off) scheme(s1mono) overlay `m_plot_opt' `f_plot_opt'"
xtline alcuse, legend(off) scheme(s1mono) overlay `m_plot_opt' `f_plot_opt'
It is difficult (for me) to separate the programming issue here from statistical or graphical views on what kind of graph works well, or at all. Even with this modest dataset there are 82 distinct identifiers, so any attempt to show them distinctly fails to be useful, if only because the resulting legend takes up most of the real estate.
There is considerable ingenuity in the question code in working through all the identifiers, but a broad-brush approach seems to work as well. Try this:
use http://www.ats.ucla.edu/stat/stata/examples/alda/data/alcohol1_pp, clear
xtset id age
separate alcuse, by(male) veryshortlabel
label var alcuse1 "male"
label var alcuse0 "female"
line alcuse? age, legend(off) sort connect(L)
Key points:
There is nothing very special about xtline. It's just a convenience wrapper. When frustrated by its wired-in choices, people often just reach for line.
To get distinct colours, distinct variables suffice, which is where separate has a role. See also this Tip.
Although the example dataset is well behaved, extra options sort connect(L) will help in some case to remove spurious connections between individuals or panels. (In extreme cases, reach for linkplot (SSC).)
This could be fine too:
line alcuse age if male || line alcuse age if !male, legend(order(1 "male" 2 "female")) sort connect(L)

Extracting coefficients from sqreg in Stata

I am trying to run quantile regressions across deciles, and so I use the sqreg command to get bootstrap standard errors for every decile. However, after I run the regression (so Stata runs 9 different regressions - one for each decile except the 100th) I want to store the coefficients in locals. Normally, this is what I would do:
reg y x, r
local coeff = _b[x]
And things would work well. However, here my command is:
sqreg y x, q(0.1 0.2 0.3)
So, I will have three different coefficients here that I want to store as three different locals. Something like:
local coeff10 = _b[x] //Where _b[x] is the coefficient on x for the 10th quantile.
How do I do this? I tried:
local coeff10 = _b[[q10]x]
But this gives me an error. Please help!
Thank you!
Simply save matrix of coefficients from postestimation scalars and reference the outputted variable by row and column.
The reason you could not do the same as the OLS is the sqreg matrix holds multiple named instances of coefficient names:
* OUTPUTS MATRIX OF COEFFICIENTS (1 X 6)
matrix list e(b)
* SAVE COEFF. MATRIX TO REGULAR MATRIX VARIABLE
mat b = e(b)
* EXTRACT BY ROW/COLUMN INTO OTHER VARIABLES
local coeff10 = b[1,1]
local coeff20 = b[1,3]
local coeff30 = b[1,5]

Stata: extract p-values and save them in a list

This may be a trivial question, but as an R user coming to Stata I have so far failed to find the correct Google terms to find the answer. I want to do the following steps:
Do a bunch of tests (e.g. lrtest results in a foreach loop)
Extract the p-value from each test and save them in a list of some kind
Have a list I can do further operations on (e.g. perform multiple comparison correction)
So I am wondering how to extract p-values (or similar) from command results and how to save them into a vector-like object that I can work with. Here is some R code that does something similar:
myData <- data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10)) ## generate some data
pValue <- c()
for (variableName in c("b", "c")) {
myModel <- lm(as.formula(paste("a ~", variableName)), data=myData) ## fit model
pValue <- c(pValue, coef(summary(myModel))[2, "Pr(>|t|)"]) ## extract p-value and save in vector
}
pValue * 2 ## do amazing multiple comparison correction
To me it seems like Stata has much less of a 'programming' mindset to it than R. If you have any general Stata literature recommendations for an R user who can program, that would also be appreciated.
Here is an approach that would save the p-values in a matrix and then you can manipulate the matrix, maybe using Mata or standard matrix manipulation in Stata.
matrix storeMyP = J(2, 1, .) //create empty matrix with 2 (as many variables as we are looping over) rows, 1 column
matrix list storeMyP //look at the matrix
loc n = 0 //count the iterations
foreach variableName of varlist b c {
loc n = `n' + 1 //each iteration, adjust the count
reg a `variableName'
test `variableName' //this does an F-test, but for one variable it's equivalent to a t-test (check: -help test- there is lots this can do
matrix storeMyP[`n', 1] = `r(p)' //save the p-value in the matrix
}
matrix list storeMyP //look at your p-values
matrix storeMyP_2 = 2*storeMyP //replicating your example above
What's going on this that Stata automatically stores certain quantities after estimation and test commands. When the help files say this command stores the following values in r(), you refer to them in single quotes.
It could also be interesting for you to convert the matrix column(s) into variables using svmat storeMyP, or see help svmat for more info.

Extract the mean from svy mean result in Stata

I am able to extract the mean into a matrix as follows:
svy: mean age, over(villageid)
matrix villagemean = e(b)'
clear
svmat village
However, I also want to merge this mean back to the villageid. My current thinking is to extract the rownames of the matrix villagemean like so:
local names : rownames villagemean
Then try to turn this macro names into variable
foreach v in names {
gen `v' = "``v''"
}
However, the variable names is empty. What did I do wrong? Since a lot of this is copied from Stata mailing list, I particularly don't understand the meaning of local names : rownames villagemean.
It's not completely clear to me what you want, but I think this might be it:
clear
set more off
*----- example data -----
webuse nhanes2f
svyset [pweight=finalwgt]
svy: mean zinc, over(sex)
matrix eb = e(b)
*----- what you want -----
levelsof sex, local(levsex)
local wc: word count `levsex'
gen avgsex = .
forvalues i = 1/`wc' {
replace avgsex = eb[1,`i'] if sex == `:word `i' of `levsex''
}
list sex zinc avgsex in 1/10
I make use of two extended macro functions:
local wc: word count `levsex'
and
`:word `i' of `levsex''
The first one returns the number of words in a string; the second returns the nth token of a string. The help entry for extended macro functions is help extended_fcn. Better yet, read the manuals, starting with: [U] 18.3 Macros. You will see there (18.3.8) that I use an abbreviated form.
Some notes on your original post
Your loop doesn't do what you intend (although again, not crystal clear to me) because you are supplying a list (with one element: the text name). You can see it running and comparing:
local names 1 2 3
foreach v in names {
display "`v'"
}
foreach v in `names' {
display "`v'"
}
foreach v of local names {
display "`v'"
}
You need to read the corresponding help files to set that right.
As for the question in your original post, : rownames is another extended macro function but for matrices. See help matrix, #11.
My impression is that for the kind of things you are trying to achieve, you need to dig deeper into the manuals. Furthermore, If you have not read the initial chapters of the Stata User's Guide, then you must do so.