Saving confidence limits in Stata - stata

After running glm I can type matrix list r(table) and see a table of all of my results. If I wish, I can write slopes and SEs to variables, e.g., gen B=_b[x1] or gen se=_se[x1]. However, this does not work with the confidence limits, ll and ul. How can I access them in a similar manner?

I am not sure if the _b[] and _se[] results are associated with r(table)--I have thought they are products of e(b) and e(V).
Anyway, since you have r(table), you can just save the results into another matrix, and then use the regular matrix operations to put the lower bounds and upper bounds into new matrices. If for some reason transformation into variables is desired (for example, plotting), there's always -svmat-.
sysuse auto,clear
glm price mpg foreign, f(gaussian)
mat r=r(table)
matrix ll=r["ll",....]' // see -help matrix extraction-; transposed for svmat
svmat ll,names(ll) // lower bounds are in variable ll1

Related

Edit confidence interval in Stata coefplot

I am using the coefplot command in Stata to plot coefficients and confidence intervals from multiple regression models. I am plotting the same coefficient (X) from 4 different model specifications.
There is one model specification (alternative standard errors) that I cannot figure out how to estimate in Stata, but am able to estimate using R. That means that for one model specification, I have the standard error in R, but not in Stata.
Is there an easy way to manually alter the standard errors in coefplot?
My code is:
coefplot A B C D, drop(_cons) xline(0) keep(X)
How can I add to this code that the standard errors for coefficient X in model D should be Z?
You can manually edit the e(V) (variance-covariance matrix) and e(b) vectors. For this, define a program:
est restore estimates1
capture program drop changeeV
program changeeV, eclass
tempname b V
matrix `b' = e(b)
matrix `V' = e(V)
matrix `V'[1,1] = 1.1 // Add values of new variance-covariance matrix
matrix `b'[1,1] = 10 // Add new coefficient vector
ereturn post `b' `V' // Repost new vectors
ereturn local cmd "reg outcome treatment covariates"
// Repost initial command (required)
end
changeeV // Execute program
est store eaX // Store new generated estimtes
Note that, to reach the covariance matrix, you need to take the square of the standard errors from your output in R. Good luck!

Stata: Storing only part of a FE regression output for graphing

I am running a regression with two fixed effects categories (country and year, is economic macro data). Since I am using xtreg, one is autohid, but the other is a variable:
xtreg fiveyearyg taxratio i.year if taxratiocut == 1, i(wbcode1) fe cluster(wbcode1)
estimates store yi
I am running a number of these and I want to graph the coefficients for taxratio from each. But when I store the data, it stores both the taxratio coefficient, and the 50+ coefficients for the year fixed effects.
After a lot of searching, I cannot find any way to store (or recall) just part of the regression output, the one coefficient (with SEs) that I care about. Does anyone know a way to do that?
Here is how you can do that:
webuse grunfeld,clear
qui xtreg mvalue invest i.year,fe cluster(company)
//e(b) stores coefficient matrix and e(V) stores variance-covariance matrix. For details type: ereturn list after running the model
//let's say you want to extract only the coefficient on invest
mat coef_matrix=e(b)
scalar coef_invest=coef_matrix[1,1]
dis coef_invest
1.7178414
//to extract se of the the coefficient on invest
mat var_matrix=e(V)
mat diag_var_matrix=vecdiag(var_matrix) //diagonal elements are variances and the standard errors are square roots of these variances
matmap diag_var_matrix se_matrix , m(sqrt(#))) //you need to install matmap using ssc install matmap, you will get error if variance is negative
scalar se_invest=se_matrix[1,1]
dis se_invest
.14082153
Accessing coefficients is as easy as calling _b[varname]; analogously the corresponding standard errors: _se[varname].
An example:
webuse grunfeld, clear
qui xtreg mvalue invest i.year,fe cluster(company)
// coef for invest
display _b[invest]
// std error for invest
display _se[invest]
// displayed results in matrix
matrix list r(table)
For multiple-equation models use [eqno]_b[varname] where the preceding bracket contains an equation number.
More detail can be found in [U] 13.5 Accessing coefficients and standard errors.
Starting Stata 12, estimation commands also store results in r() [and not just e()]. Notice I listed r(table), which contains most results displayed by the estimation command xtreg.
You show interest in plotting coefficients, so you should read on the user-written command coefplot. Run ssc install coefplot to download and help coefplot to get started. It has many options.
Edit
A complete example that plots only coefficients for invest (leaving out those for year), using coefplot, and based on conditional regressions is:
clear
set more off
webuse grunfeld
xtreg mvalue invest i.year if time <= 10,fe cluster(company)
estimates store before10
xtreg mvalue invest i.year if time > 10,fe cluster(company)
estimates store after10
coefplot before10 after10, keep(invest)

Stata's estout with two sets of margins

Suppose I have a model like this:
webuse nlswork
poisson hours i.union##c.tenure, robust
margins union, dydx(tenure)
margins rb1.union, dydx(tenure)
I would like to stack the two AMEs on top of the differences of the AMEs using Ben Jann's -estout-. Unfortunately, you need to post the margins results for estout, which interferes with the second margins command.
Is there any way around this?
Cross-posted at the Statalist forum for some time without an answer.
I've never used -estout-, but perhaps this will give you a start.
webuse nlswork
poisson hours i.union##c.tenure, robust
estimates store m0
margins union, dydx(tenure) post
estimates store m1
estimates restore m0
margins rb1.union, dydx(tenure) post
estimates store m2
Why this works: margins needs access to the results of the original command, poisson in this example. As margins does not itself leave estimation results behind, the original results remain available if you run margins without post, and you can have several margins commands in a row without problems. However, if you add the post option to the first margins command, the new posted results displace those in memory. In that case, the second margins will complain that
margins cannot work with its own posted results
The solution, therefore, is to present the second margins with the original estimation results, just what estimates restore is designed to do.
Update
r(table) contains all the results from margins, and the columns are named. Here's a version of Roberto's stacking solution that takes advantage of these properties:
use nlswork, clear,
poisson hours i.union##c.tenure, robust
margins union, dydx(tenure)
matrix list r(table)
matrix m1 = r(table)
matrix m11 = m1["b".."se", 1...]'
matrix m12 = m1["ll".."ul",1...]'
matrix first = m11,m12
margins rb1.union, dydx(tenure)
matrix m2 = r(table)
matrix m21 = m2["b".."se", 1...]'
matrix m22 = m2["ll".."ul",1...]'
matrix second = m21,m22
matrix rownames second = tenure:diff
matrix RESULTS = first \ second
estout matrix(RESULTS)
estout takes matrices, so maybe you can try with that:
webuse nlswork, clear
poisson hours i.union##c.tenure, robust
margins union, dydx(tenure)
matrix first = r(b)
matrix list first
margins rb1.union, dydx(tenure)
matrix second = r(b)
matrix list second
*-----
matrix b = first[1,1] , first[1,2] \ second[1,1] , .
estout matrix(b)
You would need to polish the results, of course.
Update
There's a thread on Statalist from 2007, where Ben Jann (the author of estout) clarifies that stacking multiple stored results into one column is not possible with estout alone. His solution involves a program that merges results manipulating matrices and column/row names.
For the example you have provided, something like the following works:
webuse nlswork, clear
poisson hours i.union##c.tenure, robust
// first margin
margins union, dydx(tenure)
matrix first = r(b)
// second margin
margins rb1.union, dydx(tenure)
matrix second = r(b)
matrix rownames second = tenure:diff
// put together
matrix c = first' \ second
estout matrix(c)
(The thread is a bit old so I'm not sure if estout has been updated to do this at present.)

how to apply princomp with more variables on eeg data

i have a data set of eeg recordings with 5000 rows and 59 coloumns. as coloumns are channels of eeg headsets and rows represents signal amplitude at each channel. now i used princomp to reduce the dimensions. but i am confused in variable and observation as i have a lable vector of 5000 elements to classify tha data but if i used princomp at 5000x59 i got 59x59 matrix which can't be classified by given label and if i apply command on 59x5000 data i got 5000x5000 matrix it means pca increase the dimension instead of decreasing. so kindly make me understand how can i percept variables and observation in my data.
thnx
The Matlab command princomp can have multiple return values. If we denote your original 5000x59 data matrix as D, then
[C, S] = princomp(D);
gives you the principal component coefficients C (59x59), and the actual principal components S (5000x59), where C is the projection from the original space to the principal component space, and S contains the actual principal components as its columns. The relationship of the 3 matrices is
D * C = S
Btw, if you only care about the principal components S and don't need the coefficients C, you can do
[~, S] = princomp(D);
For more details, check out the official Matlab princomp doc.

PCA in Matlab - Are the Principal Compoents re-arranged?

I am trying to do a PCA on some volatility data, and let's just say I can propose a model as the following:
volatility = bata0 + beta1*x + beta2* x^2
where x are some observations, say for example, moneyness and so on.
So in Matlab, what I did was to say Y=[ones x x^2] and then do pca(Y)
and for some reason, my first row in my coefficient matrix is always something like 0 0 1, i.e., 0 everywhere else except the last column, and output of atent always shows the highest value in the first row as well, no matter how I change the model.
Obviously, this can't be the case where the last term in every single model is explained well by the last term in the equation. And if I remove the constant term in Y (i.e., Y= [x x^2] then the first row of coefficient matrix becomes something more normal (i.e., non-zero value everywhere).
So my questions are:
is my way of doing PCA right?
Does PCA automatically rearrange the principal component and hence the first row in the coefficient matrix with all zeros except 1 at the last column may not necessarily represent the last term in the equation and
if it is wrong, what is the correct way of doing it?
From Matlab's documentation for princomp:
COEFF = princomp(X) performs principal components analysis (PCA) on
the n-by-p data matrix X, and returns the principal component
coefficients, also known as loadings. Rows of X correspond to
observations, columns to variables. COEFF is a p-by-p matrix, each
column containing coefficients for one principal component. The
columns are in order of decreasing component variance.