Should tabstat and centile give different results for percentiles? - stata

. sysuse auto
(1978 Automobile Data)
. centile price, centile(25 75)
-- Binom. Interp. --
Variable | Obs Percentile Centile [95% Conf. Interval]
-------------+-------------------------------------------------------------
price | 74 25 4193 4009.467 4501.838
| 75 6378 5798.432 9691.6
. tabstat price, stat(p25 p75)
variable | p25 p75
-------------+--------------------
price | 4195 6342
When making calculations by hand, my answers agree with the centile command, and disagree with the tabstat command (bonus: they also disagree with the sum , detail command).
Where is this discrepancy (25th percentile: 4193 vs 4195, and 75th percentile: 6378 vs 6342) coming from?
I am using Stata 15.1 for Unix.

Related

Difference between two means and medians in stata

I want to export p-value of means and median difference to latex from stata. I tried the code below and it works for p-value of means. However, I dont know how to add the analysis on the difference between medians. Can you please help me with this?
eststo control: quietly estpost summarize a b c if treated == 0
eststo treated: quietly estpost summarize a b c if treated == 1
eststo diff: quietly estpost ttest a b c, by(treated) unequal
esttab using means_medians.tex, replace mlabels("Treated" "Control" "Difference")
cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0 ) fmt(3)) b(star pattern(0 0 1) fmt(2))") label
You can use epctile that is implemented as a proper estimation commands and returns e(V) and the estimation table. (Disregard the ugly output in the first part. I wrote it about 10 years ago for a specific project, but it seems like I have never made it pretty enough for the general use.)
. sysuse auto, clear
(1978 Automobile Data)
. epctile mpg, p(50)
Mean estimation Number of obs = 74
--------------------------------------------------------------
| Mean Std. Err. [95% Conf. Interval]
-------------+------------------------------------------------
__000006 | -.027027 .058435 -.1434878 .0894338
--------------------------------------------------------------
Percentile estimation
------------------------------------------------------------------------------
mpg | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
p50 | 20 .75 26.67 0.000 18.53003 21.46997
------------------------------------------------------------------------------
This command is not on SSC, it is on my page, so to install it, follow the prompts in findit epctile.

Stata multinomial regression - post-estimation Wald test

I've conducted a multinomial logistic regression analysis in Stata, followed by a Wald test, and was hoping someone could confirm that my code is doing what I think it's doing.
NB: I'm using some of Stata's example data to illustrate. The analysis I'm running for this illustration is completely meaningless, but uses the same procedure as my 'real' analysis, other than the fact that my real analysis also includes some probability weights and other covariates.
sysuse auto.dta
First, I run a multinomial logistic regression, predicting 'Repair Record' from 'Foreign' and 'Price':
mlogit rep78 i.foreign price, base(1) rrr nolog
Multinomial logistic regression Number of obs = 69
LR chi2(8) = 31.15
Prob > chi2 = 0.0001
Log likelihood = -78.116372 Pseudo R2 = 0.1662
------------------------------------------------------------------------------
rep78 | RRR Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
1 | (base outcome)
-------------+----------------------------------------------------------------
2 |
foreign |
Foreign | .7822853 1672.371 -0.00 1.000 0 .
price | 1.000414 .0007027 0.59 0.556 .9990375 1.001792
_cons | .5000195 1.669979 -0.21 0.836 .000718 348.2204
-------------+----------------------------------------------------------------
3 |
foreign |
Foreign | 686842 1.30e+09 0.01 0.994 0 .
price | 1.000462 .0006955 0.66 0.507 .9990996 1.001826
_cons | 1.254303 4.106511 0.07 0.945 .0020494 767.6863
-------------+----------------------------------------------------------------
4 |
foreign |
Foreign | 6177800 1.17e+10 0.01 0.993 0 .
price | 1.000421 .0006999 0.60 0.547 .9990504 1.001794
_cons | .5379627 1.7848 -0.19 0.852 .0008067 358.7452
-------------+----------------------------------------------------------------
5 |
foreign |
Foreign | 2.79e+07 5.29e+10 0.01 0.993 0 .
price | 1.000386 .0007125 0.54 0.587 .9989911 1.001784
_cons | .146745 .5072292 -0.56 0.579 .0001676 128.4611
------------------------------------------------------------------------------
Second, I want to know whether the 'Foreign' coefficient for outcome category 4 is significantly different to the 'Foreign' coefficient for outcome category 5. So, I run a Wald test:
test [4]1.foreign = [5]1.foreign
( 1) [4]1.foreign - [5]1.foreign = 0
chi2( 1) = 2.72
Prob > chi2 = 0.0988
From this, I conclude that the 'Foreign' coefficient for outcome category 4 is NOT significantly different to the 'Foreign' coefficient for outcome category 5. Put more simply, the association between 'Foreign' and 'Repair 4' (compared to 'Repair 1') is equal to the association between 'Foreign' and 'Repair 5' (compared to 'Repair 1') .
Is my code for the Wald test, and my inferences about what it's doing and showing, correct?
Additionally, to what was discussed in the comments you can also perform a likelihood-ratio test using the following code.
sysuse auto.dta
qui mlogit rep78 i.foreign price, base(1) rrr nolog
estimate store unrestricted
constraint 1 [4]1.foreign = [5]1.foreign
qui mlogit rep78 i.foreign price, base(1) rrr nolog constraints(1)
estimate store restricted
lrtest unrestricted restricted
The output of the test shows the same conclusion as the Wald test, but it has better properties as explained below.
Likelihood-ratio test LR chi2(1) = 3.13
(Assumption: restricted nested in unrestricted) Prob > chi2 = 0.0771
Quoting the official documentation from mlogit
The results produced by test are an approximation based on the estimated covariance matrix of the coefficients. Because the probability of being uninsured is low, the log-likelihood may be nonlinear for the uninsured. Conventional statistical wisdom is not to trust the asymptotic answer under these circumstances but to perform a likelihood-ratio test instead.

Stata xtoverid command error

I have panel data and my regression is of the form:
s_roa1 = s_roa + c_roa
I am new to Stata and i am trying to use the xtoverid command for a robust hausman test to help me choose between a fixed or random effects model:
xtoverid s_roa1 s_roa c_roa, fe i (year)
However, I get the following error:
varlist not allowed
Can anyone help me understand what does this suggest?
First of all, xtoverid is a community-contributed command, something which you fail to make clear in your question. It is customary and useful to provide this information right from the start, so others know that you do not refer to an official, built-in command.
Second, this is a post-estimation command, which means you run it directly after you estimate your model using xtreg, xtivreg, xtivreg2 or xthtaylor.
The help file provided by the authors offers an enlightening example:
. webuse nlswork
(National Longitudinal Survey. Young Women 14-26 years of age in 1968)
. tsset idcode year
panel variable: idcode (unbalanced)
time variable: year, 68 to 88, but with gaps
delta: 1 unit
.
. gen age2=age^2
(24 missing values generated)
. gen black=(race==2)
.
. xtivreg ln_wage age (tenure = union south), fe i(idcode)
Fixed-effects (within) IV regression Number of obs = 19,007
Group variable: idcode Number of groups = 4,134
R-sq: Obs per group:
within = . min = 1
between = 0.1261 avg = 4.6
overall = 0.0869 max = 12
Wald chi2(2) = 142054.65
corr(u_i, Xb) = -0.6875 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
ln_wage | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
tenure | .2450528 .0382041 6.41 0.000 .1701741 .3199314
age | -.0650873 .0126167 -5.16 0.000 -.0898156 -.040359
_cons | 2.826672 .2451883 11.53 0.000 2.346112 3.307232
-------------+----------------------------------------------------------------
sigma_u | .71990151
sigma_e | .64315554
rho | .55612637 (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(4133,14871) = 1.53 Prob > F = 0.0000
------------------------------------------------------------------------------
Instrumented: tenure
Instruments: age union south
------------------------------------------------------------------------------
.
. xtoverid
Test of overidentifying restrictions:
Cross-section time-series model: xtivreg fe
Sargan-Hansen statistic 0.965 Chi-sq(1) P-value = 0.3259
. xtoverid, robust
Test of overidentifying restrictions:
Cross-section time-series model: xtivreg fe robust
Sargan-Hansen statistic 0.960 Chi-sq(1) P-value = 0.3271
. xtoverid, cluster(idcode)
Test of overidentifying restrictions:
Cross-section time-series model: xtivreg fe robust cluster(idcode)
Sargan-Hansen statistic 0.495 Chi-sq(1) P-value = 0.4818
From Stata's command prompt, type help xtoverid for more details.

Using two different versions of Stata

I am working in two locations, in one I am using Stata 13 and in the other Stata 14.
Can I build a do-file that works in both versions even if some specific command has changed?
For instance, the following code will not work using Stata 13
sysuse auto, clear
ci means mpg price, level(90)
but this one works
sysuse auto, clear
ci mpg price, level(90)
Uising Stata 14, it will be the opposite.
I thought about adding capture but nothing happens in Stata 13 or Stata 14.
. sysuse auto, clear
(1978 Automobile Data)
. capture ci means mpg price, level(90)
. capture ci mpg price, level(90)
Update: Adding noisily after capture didn't help unfortunately. Here is an example with Stata 14 that works
. sysuse auto, clear
(1978 Automobile Data)
. capture noisily ci mpg price, level(90)
you must specify one of means, proportions, or variances following ci
. capture noisily ci means mpg price, level(90)
Variable | Obs Mean Std. Err. [90% Conf. Interval]
-------------+---------------------------------------------------------------
mpg | 74 21.2973 .6725511 20.17683 22.41776
price | 74 6165.257 342.8719 5594.033 6736.48
. gen lb=r(lb)
. su lb
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
lb | 74 5594.033 0 5594.033 5594.033
But this one does not work when you invert two lines of code (with Stata 14):
. sysuse auto, clear
(1978 Automobile Data)
. capture noisily ci means mpg price, level(90)
Variable | Obs Mean Std. Err. [90% Conf. Interval]
-------------+---------------------------------------------------------------
mpg | 74 21.2973 .6725511 20.17683 22.41776
price | 74 6165.257 342.8719 5594.033 6736.48
. capture noisily ci mpg price, level(90)
you must specify one of means, proportions, or variances following ci
* The program didn't stop but:
. gen lb=r(lb)
(74 missing values generated)
. su lb
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
lb | 0
Finally, note that the first code that works correctly with Stata 14 doesn't work with Stata 13
. sysuse auto, clear
(1978 Automobile Data)
. capture noisily ci mpg price, level(90)
Variable | Obs Mean Std. Err. [90% Conf. Interval]
-------------+---------------------------------------------------------------
mpg | 74 21.2973 .6725511 20.17683 22.41776
price | 74 6165.257 342.8719 5594.033 6736.48
. capture noisily ci means mpg price, level(90)
variable means not found
. gen lb=r(lb)
(74 missing values generated)
. su lb
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
lb | 0
If you wish to use capture to catch an error, there is follow-through as well. Here you first try version 14 syntax, and if and only if that fails you try version 13 syntax.
sysuse auto, clear
capture noisily ci means mpg price, level(90)
if _rc ci mpg price, level(90)
gen lb = r(lb)
Here if _rc is a Stataish abbreviation for if _rc > 0 which will happen if and only if a program fails. _rc of 0 means everything was legal (with minute qualifications). _rc is the return code.
I am not clear that putting a single value in a variable is a good idea, but let that be a different issue. Also, you asked for two confidence intervals, and only results for the first will remain in memory.

How to split a sample according to a certain variable in Stata?

I'd like to split a sample according to a specific variable, creating 4 sub-samples each one related to a quartile of the variable's distribution. The aim is to demonstrate that the presence of different levels of this variable influences the outcome of a regression, making it significant or not.
The easiest way to do this is to use the egen command to cut your variable into four equally-spaced intervals.
Example:
. sysuse auto, clear
(1978 Automobile Data)
. sum price, detail
Price
-------------------------------------------------------------
Percentiles Smallest
1% 3291 3291
5% 3748 3299
10% 3895 3667 Obs 74
25% 4195 3748 Sum of Wgt. 74
50% 5006.5 Mean 6165.257
Largest Std. Dev. 2949.496
75% 6342 13466
90% 11385 13594 Variance 8699526
95% 13466 14500 Skewness 1.653434
99% 15906 15906 Kurtosis 4.819188
. egen price_cut = cut(price), group(4)
. table price_cut, contents(n price min price max price)
----------------------------------------------
price_cut | N(price) min(price) max(price)
----------+-----------------------------------
0 | 18 3,291 4,187
1 | 19 4,195 4,934
2 | 18 5,079 6,303
3 | 19 6,342 15,906
----------------------------------------------
I hope this helps you.
This is the easiest way you can go about it:
xtile xx=yourvariable, nq(4)
I hope this helps.