Quantcast
Channel: Statalist
Viewing all 72868 articles
Browse latest View live

xtreg fe versus areg robust errors?

$
0
0
Hello! I apologize if some of the questions in this post seem simple, but this has to do with my thesis and I appreciate any assistance!

I am dealing with panel data involving some banks over 63 time periods(N=4736 T=63) balanced. I used the Hausman test to determine I needed to use a FE model. But, this is where the trouble begins and my questions begin! So I run a basic xtreg fe model and get this result
Code:
xtreg zscore lnasset lnassetsq diverse leverage eeffqr DGS10 CPIAUCSL_PCH GDPC1_PC1, fe

Fixed-effects (within) regression               Number of obs     =    298,355
Group variable: cert                            Number of groups  =      4,736

R-sq:                                           Obs per group:
     within  = 0.0179                                         min =         62
     between = 0.0242                                         avg =       63.0
     overall = 0.0227                                         max =         63

                                                F(8,293611)       =     670.17
corr(u_i, Xb)  = -0.0196                        Prob > F          =     0.0000

------------------------------------------------------------------------------
      zscore |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lnasset |   .4193168   .0420687     9.97   0.000     .3368634    .5017702
   lnassetsq |  -.0071421   .0016294    -4.38   0.000    -.0103357   -.0039485
     diverse |   .0000122    .000058     0.21   0.834    -.0001015    .0001259
    leverage |   -.013206   .0004068   -32.46   0.000    -.0140033   -.0124087
      eeffqr |  -.0001518   8.33e-06   -18.23   0.000    -.0001681   -.0001355
       DGS10 |   .0478444   .0021882    21.86   0.000     .0435557    .0521332
CPIAUCSL_PCH |   .0676272   .0032808    20.61   0.000     .0611969    .0740575
   GDPC1_PC1 |   .0310941   .0008967    34.68   0.000     .0293366    .0328516
       _cons |  -1.795671   .2740217    -6.55   0.000    -2.332746   -1.258596
-------------+----------------------------------------------------------------
     sigma_u |  1.7745564
     sigma_e |  .99459682
         rho |  .76095759   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(4735, 293611) = 196.07              Prob > F = 0.0000
so now I go about running some test to check for serial correlation and heteroscedasticity.
Code:
xttest3

Modified Wald test for groupwise heteroskedasticity
in fixed effect regression model

H0: sigma(i)^2 = sigma^2 for all i

chi2 (4736)  =   379.43
Prob>chi2 =      1.0000
Which if I interpret this correctly means my model doesn't suffer from hetero.
Now running xtserial I get
Code:
xtserial zscore lnasset lnassetsq diverse leverage eeffqr DGS10 CPIAUCSL_PCH GDPC1_PC1

Wooldridge test for autocorrelation in panel data
H0: no first-order autocorrelation
    F(  1,    4735) =    226.237
           Prob > F =      0.0000
Meaning my model does suffer from autocorrelation. So what now with this information do I run a vce(robust) fe or is there some other tests I should run or other model or option I should use. I am outside my statistical chops currently but I am trying to learn.

Secondly if I were to use a VCE(Robust) model why when I run it using areg as such do I get such a different significance on some of my variables than using xtreg. It was my impression they were so similar that they should not differ by much?
Results from areg note that cert is just a unique identifier for each individual bank:
Code:
areg zscore lnasset lnassetsq diverse leverage eeffqr DGS10 CPIAUCSL_PCH GDPC1_PC1, a(cert) vce(robust)

Linear regression, absorbing indicators         Number of obs     =    298,355
Absorbed variable: cert                         No. of categories =      4,736
                                                F(   8, 293611)   =     343.16
                                                Prob > F          =     0.0000
                                                R-squared         =     0.7691
                                                Adj R-squared     =     0.7654
                                                Root MSE          =     0.9946

------------------------------------------------------------------------------
             |               Robust
      zscore |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lnasset |   .4193168   .0686047     6.11   0.000     .2848535    .5537801
   lnassetsq |  -.0071421   .0025061    -2.85   0.004    -.0120541   -.0022301
     diverse |   .0000122   .0000432     0.28   0.778    -.0000725    .0000969
    leverage |   -.013206   .0102436    -1.29   0.197    -.0332831    .0068711
      eeffqr |  -.0001518   .0001045    -1.45   0.146    -.0003566     .000053
       DGS10 |   .0478444   .0046113    10.38   0.000     .0388064    .0568824
CPIAUCSL_PCH |   .0676272   .0041492    16.30   0.000     .0594948    .0757596
   GDPC1_PC1 |   .0310941   .0011823    26.30   0.000     .0287769    .0334113
       _cons |  -1.795671   .4056216    -4.43   0.000    -2.590678   -1.000664
------------------------------------------------------------------------------
Now using a fe model:
Code:
xtreg zscore lnasset lnassetsq diverse leverage eeffqr DGS10 CPIAUCSL_PCH GDPC1_PC1, fe vce(robust)

Fixed-effects (within) regression               Number of obs     =    298,355
Group variable: cert                            Number of groups  =      4,736

R-sq:                                           Obs per group:
     within  = 0.0179                                         min =         62
     between = 0.0242                                         avg =       63.0
     overall = 0.0227                                         max =         63

                                                F(8,4735)         =     184.31
corr(u_i, Xb)  = -0.0196                        Prob > F          =     0.0000

                               (Std. Err. adjusted for 4,736 clusters in cert)
------------------------------------------------------------------------------
             |               Robust
      zscore |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lnasset |   .4193168    .134761     3.11   0.002     .1551224    .6835111
   lnassetsq |  -.0071421   .0051472    -1.39   0.165    -.0172331    .0029488
     diverse |   .0000122   .0000445     0.27   0.784     -.000075    .0000994
    leverage |   -.013206   .0102906    -1.28   0.199    -.0333804    .0069684
      eeffqr |  -.0001518   .0001059    -1.43   0.152    -.0003594    .0000558
       DGS10 |   .0478444   .0067429     7.10   0.000     .0346253    .0610636
CPIAUCSL_PCH |   .0676272   .0037723    17.93   0.000     .0602317    .0750227
   GDPC1_PC1 |   .0310941   .0014933    20.82   0.000     .0281667    .0340216
       _cons |  -1.795671   .8540467    -2.10   0.036        -3.47   -.1213422
-------------+----------------------------------------------------------------
     sigma_u |  1.7745564
     sigma_e |  .99459682
         rho |  .76095759   (fraction of variance due to u_i)
------------------------------------------------------------------------------
I do not understand exactly why in particular lnassetsq became so insignificant in the FE model with robust errors but not in the areg? I am sorry if I am missing something elementary here.

Merging datasets by inner join

$
0
0
Hi,

I have to merge three datasets by matching IDs as shown in the picture below. This is doable in R by a method called inner join. Is there is Stata equivalent for this? Array

When I import csv.file, some variables are not recognized

$
0
0
When I import csv.file, some variables are not recognized (?) but appear in the first some cells like the image (see id-1, id-1, id-3).
Can you please show me how to do with this?

Expanding Dataset

$
0
0
Hi everyone,

I am trying to expand a dataset that looks like this:
Dataset A
contract_id region_id county_n
1 1 5
2 1 5
3 2 4
4 2 4
5 3 3
6 3 3
In brief, this dataset includes unique contract_ids and each contract serves a region made up of countries (county_n represents the number of counties in a region. I am trying to expand this dataset so it has 1 row for every contract_id and county_id by merging it with this dataset:
Dataset B
region_id county_id
1 11
1 12
1 13
1 14
1 15
2 22
2 23
2 24
2 25
2 26
3 35
3 36
3 37
3 38
3 39

I've tried expanding Dataset A by the number of counties served by each region and then merging w/ Dataset B:
Code:
levelsof county_n,         local(levels)
foreach n of local levels {
 expand `n' if county_n == `n'

merge m:1 region_id using data_B,replace update
drop if _merge==2
drop _merge
}
However, this is inappropriate because it doesn't grab every county_id in Dataset B.

Any advice would be appreciated!

why the string variable was non-editable?

$
0
0
As you can see from the picture, the contens are grey and non-editable, why?
Array

Stat SE 16 | How to obtain the result according to the picture?

$
0
0
Good morning everybody.
I'm beginner. I know how to use the basic commands of the software. But I would like to replicate the same operation as the photo. Could you explain what to write in the command to obtain this?
Array
PS: I don't know the mathematical name of this formula, if you know it, don't hesitate to share your knowledge.
Thank you for your help.

Standard errors from margins commands

$
0
0
Hi,

I am wondering if someone can help me, as have been having some issues with plotting a figure to match the results from a post estimation margins command.

I have the following model:

regress y i.sex##c.age, vce(cluster studyid)

I am accounting for the fact that results are from 17 different studies (study id).

From this model, I obtain a significant interaction effect.

I then want to look at the outcome (y) at all levels of age, as therefore run:

margins sex, at(xcage=(7(1)18)), vce(unconditional)

(which also adjust the SE for the 17 clusters)

I then obtain a table with margins and SE that I would like to plot with:

marginsplot, horizontal recast(scatter) xline(119, lcolor(red))

However, the CIs for each age (by sex) overlap significantly (i.e. I was expecting sex differences at each age as I ran):

margins, dydx(sex) at(xcage=(7(1)18)) vce(unconditional)

...which shows most male/female differences are significant at the <0.001 level.

Therefore, my question is, why do the adjusted means (or the unconditional dy/dx / standard errors) do not match up to the marginal effects command?

Thank you in advance,
Lucy


saving logistic regression output as .dta file

$
0
0
Dear Statalist

I would like to save the output (odds ratio, ci, pval, se) of a simple logistic regression as a .dta file. I have tried using statsby, but cant work out how to save the output as odds ratios. I can view the output using r(table) - but cant work out how to save them as a .dta file. Any help would be much appreciated

Best Wishes

Joe

Price Elasticity of Demand - referring to last weeks sales?

$
0
0
Hello,

I am trying to work out store level price elasticity of demand for a product. I will have 104 weekly observations of price and sales per store.

I am doing this to teach a simple class (not in statistics).

In order to work out the PED (PED= % change in quantity demanded over % change in price) I need to refer to last weeks price and last weeks sales, but I don't know how.

At the moment the data is in long format, with the variables storecode sales price week.

I have considered creating 2 new variables: lastweekprice and lastweeksales but I'm not sure how to do that!


Many thanks for suggestions.

Rosie

coefplot after mlogit

$
0
0
Hello,

This is my first post so hopefully, I will get it right.

I run mlogit and would like to use coefplot (from ssc) to show my outcome. My dependent variable (decoupling) contains 4 categories.

What I currently get is a long graph with the three categories one beneath the other. What I would like to get is a graph with the three categories side-by-side. In fact, if it makes sense, having all four categories (including the based one) would be ideal. If someone knows how to do that, this will be much appreciated!

Here is an example of my data:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float decoupling long waves2 byte(urban educlvl husedlvl) float(edugap employment) byte(wealthq tv)
3 1 0 1 0 2 1 1 0
0 0 1 1 1 1 1 5 1
1 . 0 0 1 0 0 4 0
1 . 0 1 0 2 3 3 0
3 0 0 0 0 1 2 2 0
2 1 1 3 2 2 0 5 1
0 1 0 2 2 1 1 2 1
0 0 0 1 2 0 0 4 1
0 1 0 0 1 0 2 2 0
3 1 0 2 0 2 0 1 0
3 0 0 2 2 1 0 5 1
3 0 0 0 1 0 0 2 0
0 1 0 2 2 1 0 1 0
3 0 0 0 0 1 2 3 0
0 1 1 3 3 1 1 5 1
0 0 1 3 3 1 0 5 0
1 . 0 0 . . 3 4 0
1 . 0 2 2 1 0 3 0
1 1 0 2 2 1 3 3 0
1 0 0 0 1 0 0 4 1
end
label values decoupling decouplingl
label def decouplingl 0 "Supports gender equity/Empowered in household", modify
label def decouplingl 1 "Rejects gender equity/Empowered in household", modify
label def decouplingl 2 "Supports gender equity/Not empowered in household", modify
label def decouplingl 3 "Rejects gender equity/Not empowered in household", modify
label values waves2 waves2
label def waves2 0 "first wave", modify
label def waves2 1 "second wave", modify
label values urban urbanl
label def urbanl 0 "rural", modify
label def urbanl 1 "urban", modify
label values educlvl EDUCLVL
label def EDUCLVL 0 "no education", modify
label def EDUCLVL 1 "primary", modify
label def EDUCLVL 2 "secondary", modify
label def EDUCLVL 3 "higher", modify
label values husedlvl HUSEDLVL
label def HUSEDLVL 0 "no education", modify
label def HUSEDLVL 1 "primary", modify
label def HUSEDLVL 2 "secondary", modify
label def HUSEDLVL 3 "higher", modify
label values edugap edugapl
label def edugapl 0 "less", modify
label def edugapl 1 "equal", modify
label def edugapl 2 "more", modify
label values employment employmentl
label def employmentl 0 "doesn't work", modify
label def employmentl 1 "all year", modify
label def employmentl 2 "seasonally", modify
label def employmentl 3 "occasionlly", modify
label values wealthq WEALTHQ
label def WEALTHQ 1 "poorest", modify
label def WEALTHQ 2 "poorer", modify
label def WEALTHQ 3 "middle", modify
label def WEALTHQ 4 "richer", modify
label def WEALTHQ 5 "richest", modify
label values tv tv
label def tv 0 "no", modify
label def tv 1 "yes", modify
And here is the code that I am using
Code:
mlogit decoupling waves2 urban i.educlvl i.husedlvl i.edugap i.employment i.wealthq tv, rrr
estimates store m1
coefplot m1, drop(_cons) keep(*:) scheme(s1mono)
And the graph that I currently get:
Array

Thank you in advance!

how can i make the bootstrapping fast?

$
0
0
i am doing bootstrapping for SE in indirect effect, but It gets stuck at some point (don't move), how can i make it fast

here is the code
program med1, rclass
gsem (Attitude -> A622A, family(ordinal) link(logit)) (Attitude -> A622B, family(ordinal) link(logit)) (Attitude -> A622C, family(ordinal) link(logit)) (Attitude -> A622D, family(ordinal) link(logit)) (Attitude -> A622E, family(ordinal) link(logit)) (Attitude -> A623, family(ordinal) link(logit)) (Attitude -> A625_CAT3,family(bernoulli) link(logit)) (M1[AGOVERN] -> A622A, family(ordinal) link(logit)) (M1[AGOVERN] -> A622B, family(ordinal) link(logit)) (M1[AGOVERN] -> A622C, family(ordinal) link(logit)) (M1[AGOVERN] -> A622D, family(ordinal) link(logit)) (M1[AGOVERN] -> A622E, family(ordinal) link(logit)) (M1[AGOVERN] -> A623, family(ordinal) link(logit)) (0.V149_c -> Attitude, ) (0.V149_c -> A625_CAT3, family(bernoulli) link(logit)) (1.V149_c -> Attitude, ) (1.V149_c -> A625_CAT3, family(bernoulli) link(logit)) (2.V149_c -> Attitude, ) (2.V149_c -> A625_CAT3, family(bernoulli) link(logit)) (3b.V149_c -> Attitude, ) (3b.V149_c -> A625_CAT3, family(bernoulli) link(logit)) (M2[AGOVERN] -> A625_CAT3, family(bernoulli) link(logit)), covstruct(_lexogenous, diagonal) latent(Attitude M1 M2 ) nocapslatent

return scalar ME1= _b[A625_CAT3:Attitude]*_b[Attitude :0.V149_c]
return scalar ME2= _b[A625_CAT3:Attitude]*_b[Attitude :1.V149_c]
return scalar ME3= _b[A625_CAT3:Attitude]*_b[Attitude :2.V149_c]
end

bootstrap r(ME1) r(ME2) r(ME3), reps(25) cluster(AGOVERN) : med1

Failing to properly reproduce Stata Blog's examples with Matplotlib

$
0
0
Hello everyone,

I am trying to reproduce the code given on https://blog.stata.com/2020/09/14/st...l-predictions/
I was desperate not to find a way to use the interactive backend and eventually chose to use matplotlib.use('agg') instruction to get a graph at the end. But still, I have another problem. The following code
Code:
python:
import pandas as pd
data = pd.read_stata("predictions.dta")
import numpy as np
mylist = np.arange(20,90,step=10)
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
data = pd.read_stata("predictions.dta")
ax = plt.axes(projection='3d')
plt.savefig("essai.png")
end
stops at the ax creation line withe error :

Traceback (most recent call last):
File "/Users/alexispe/opt/anaconda3/lib/python3.7/site-packages/matplotlib/projections/__init__.py", line 58, in g
> et_projection_class
return projection_registry.get_projection_class(projectio n)
File "/Users/alexispe/opt/anaconda3/lib/python3.7/site-packages/matplotlib/projections/__init__.py", line 25, in g
> et_projection_class
return self._all_projection_types[name]
KeyError: '3d'

Is there any clue to help me? I use Stata 16 with Mac OS X and an Anaconda Python setup (I have other Python versions installed if needed).
At first, I thought that Python integration within Stata could an open window on a different (and perhaps better?) world that could threaten Stata but now I start to realise how smart the move is: it makes Stata users understand how easy, convenient and readable Stata language is!

Error option expression() required

$
0
0
Dear Stata User,

To learn more about Industrial Organizations and STATA programming, I have been trying to replicate the BLP (1995) code, but I am having issues on the estimation part, that I hope someone may help me with.

I have trace where the problem comes up.

Code:
 Source |       SS           df       MS      Number of obs   =     2,217
-------------+----------------------------------   F(5, 2211)      =    279.64
       Model |  1639.14273         5  327.828545   Prob > F        =    0.0000
    Residual |  2592.05292     2,211  1.17234415   R-squared       =    0.3874
-------------+----------------------------------   Adj R-squared   =    0.3860
       Total |  4231.19565     2,216  1.90938432   Root MSE        =    1.0827

------------------------------------------------------------------------------
logit_depvar |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        hpwt |  -.1205578    .277234    -0.43   0.664    -.6642241    .4231085
         air |  -.0347453   .0728063    -0.48   0.633    -.1775211    .1080305
         mpd |   .2631698   .0431176     6.10   0.000     .1786145     .347725
       space |   2.341023   .1251805    18.70   0.000      2.09554    2.586507
       price |  -.0887309   .0040258   -22.04   0.000    -.0966257   -.0808362
       _cons |  -10.06885   .2528788   -39.82   0.000    -10.56476   -9.572949
------------------------------------------------------------------------------
option expression() required
    The moment expression is incorrectly specified.
r(198);
The interesting part is that when run line per line of program table_3 Colum 1, I get by without any errors for the OLS estimation, but after GMM, the same error occurs again.

Code:
program main
    prepare_data
    table_3
end


program prepare_data
    insheet using "C:\Users\Max\Desktop\Artigos\Cars\BLP_Pyton\trunk\analysis\Transparent Identification (BLP Replication)\external\data\blp_1999_data.csv", clear comma names double
    drop v19
    save "C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\blp_data.dta", replace
end


program table_3
    use "C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\blp_data.dta", clear
    cap matrix drop TABLE
    
    * Column 1 (OLS)
    reg logit_depvar hpwt air mpd space price
    compute_inelastic_demand, price_coef_name(price)                   option expression() required
    local ols_inelastic_lower = round(r(ci_lower), 0.001)                         
    local ols_inelastic_upper = round(r(ci_upper), 0.001)                                      
    matrix TABLE = _b[_cons]        \ _se[_cons]  \ ///
                   _b[hpwt]         \ _se[hpwt]   \ ///
                   _b[air]          \ _se[air]    \ ///
                   _b[mpd]          \ _se[mpd]    \ ///
                   .                \ .           \ ///
                   _b[space]        \ _se[space]  \ ///
                   .                \ .           \ ///
                   _b[price]        \ _se[price]  \ r(num_inelastic)
    scalar ols_r2 = e(r2)
    
    * Column 2 (IV)
    * First construct the instruments, following BLP (1995, p. 861)
    egen ncar_firm = count(1), by(year firm_id)
    local five_dmd_variables "const hpwt air mpd space"
    foreach var of local five_dmd_variables{ 
        * Reproduces IV construction from BLP (1995) code
        gen own_`var' = `var' * ncar_firm
        egen all_`var' = total(`var'), by(year)
    }
    local indep_var "const hpwt air mpd space price" ///  GMM estimation
    local inst_var "const hpwt air mpd space own_* all_*" 
    gmm (logit_depvar - {xb:`indep_var'}), ///
        instruments(`inst_var') ///                                                                     
        winitial(identity) center                                                                   
    compute_2sls_se, indep_var(`indep_var') inst_var(`inst_var')         option expression() required
    compute_inelastic_demand, price_coef_name(xb_price:_cons)
    local iv_inelastic_lower = round(r(ci_lower), 0.001)
    local iv_inelastic_upper = round(r(ci_upper), 0.001)
    matrix TABLE = nullmat(TABLE) ,                            ///
                   (_b[xb_const:_cons] \ _se[xb_const:_cons] \ ///
                   _b[xb_hpwt:_cons]   \ _se[xb_hpwt:_cons]  \ ///
                   _b[xb_air:_cons]    \ _se[xb_air:_cons]   \ ///
                   _b[xb_mpd:_cons]    \ _se[xb_mpd:_cons]   \ ///
                   .                   \ .                   \ ///
                   _b[xb_space:_cons]  \ _se[xb_space:_cons] \ ///
                   .                   \ .                   \ ///
                   _b[xb_price:_cons]  \ _se[xb_price:_cons] \ r(num_inelastic))
    
    * Column 3 (OLS w/ logs)
    foreach var of varlist price hpwt space mpg {
        replace `var' = log(`var')
    }
    replace trend = year    // transform trend to get the same intercept coefficient
    
    reg price hpwt air mpg space trend
    matrix TABLE = nullmat(TABLE) ,          ///
                   (_b[_cons] \ _se[_cons] \ ///
                   _b[hpwt]   \ _se[hpwt]  \ ///
                   _b[air]    \ _se[air]   \ ///
                   .          \ .          \ ///
                   _b[mpg]    \ _se[mpg]   \ ///
                   _b[space]  \ _se[space] \ ///
                   _b[trend]  \ _se[trend] \ . \ . \ .)
    matrix_to_txt, saving("C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\tables.txt") mat(TABLE) format(%20.3f) ///
        title(<tab:ols_logit>) append
    file open f using ../output/tables.txt, write append
    file write f "(`ols_inelastic_lower'-`ols_inelastic_upper')" _tab
    file write f "(`iv_inelastic_lower'-`iv_inelastic_upper')" _tab "." _n
    file close f
    matrix TABLE = ols_r2 , ., e(r2)
    matrix_to_txt, saving("C:\Users\Max\Desktop\Artigos\Cars\Stata_Tutorial_IndustrialOrganizations\tables.txt") mat(TABLE) format(%20.3f) title() append
end

* Replicates standard errors from BLP (1995)
program compute_2sls_se, eclass
    syntax, indep_var(varlist) inst_var(varlist)
    preserve
    
    predict resid
    * local inst_var const hpwt air mpd space own_* all_*
    foreach instrument of varlist `inst_var' {
        gen mom_`instrument' = `instrument' * resid
    }
    quietly corr mom_*, covariance
    matrix VCOV_MOM = r(C)
    
    mkmat `indep_var', matrix(X)
    mkmat `inst_var', matrix(Z)
    matrix JACOBIAN = Z' * X / _N
    matrix VCOV_PARAM = ///
        inv(JACOBIAN' * JACOBIAN) * JACOBIAN' * VCOV_MOM * JACOBIAN * inv(JACOBIAN' * JACOBIAN) / _N
    ereturn repost V = VCOV_PARAM
    
    restore
end

program compute_inelastic_demand, rclass
    syntax, price_coef_name(string)
    * Rounds inputs to ensure match with BLP (1995) table
    scalar price_coef = round(_b[`price_coef_name'], 0.001)
    scalar price_coef_se = round(_se[`price_coef_name'], 0.001)
    
    gen price_elasticity = -price_coef * price * (1-share)
    quietly count if price_elasticity < 1
    return scalar num_inelastic = `r(N)'
    
    gen price_elasticity_ci_upper = -(price_coef + 2*price_coef_se) * price * (1-share)
    quietly count if price_elasticity_ci_upper < 1
    return scalar ci_upper = `r(N)'
    
    gen price_elasticity_ci_lower = -(price_coef - 2*price_coef_se) * price * (1-share)
    quietly count if price_elasticity_ci_lower < 1
    return scalar ci_lower = `r(N)'
    
    drop price_elasticity*
end
Here is a sample of the data I am using:

Code:
clear
input double(logit_depvar hpwt) byte air double(mpd space price)
 -6.725221995827 .528996865204 0 1.888145604396 1.1502  4.935802469136
 -7.175606516457 .494324423288 0 1.935989010989  1.278  5.516049382716
 -7.852502562348 .467613439557 0 1.716799450549 1.4592  7.108641975309
 -7.424867073844  .42654028436 0 1.687870879121 1.6068   6.83950617284
 -7.590731362432 .452488687783 0 1.504285714286 1.6458  8.928395061728
 -5.761440843009 .450870646766 0 1.726813186813 1.6224  7.153086419753
 -5.800458924364 .564001961746 0 1.727925824176  1.768  9.856790123457
 -7.494032269991 .731367541212 0 2.194120879121  1.768 11.269135802469
 -5.893490833865 .719013923762 0 2.056153846154  1.816 12.135802469136
  -7.37054142125 .728323699422 0 1.978269230769  1.744  12.97037037037
 -8.837259359519 .732484076433 0 1.918186813187  1.808              15
 -5.780288130463 .729386892178 0 1.912623626374  1.808 16.044444444444
 -7.402376987159 .780748663102 0 2.018324175824  1.776  18.22962962963
  -7.87526574482 .716510903427 0 1.892596153846  1.832 19.167901234568
  -5.38771548063 .419384902144 0 2.211923076923  1.122   5.16049382716
 -5.513151785684 .487231182796 0 1.802472527473  1.387  5.938271604938
 -5.159829472982 .451713395639 0 1.682307692308 1.5352   6.60987654321
 -6.180926607269 .468648998061 0           1.62   1.41  7.212345679012
 -5.992941074536 .702408256881 0 1.604423076923 1.5732  8.434567901235
 -7.742032900857 .388531618435 0         1.4175  1.736  7.644444444444
 -7.087644340445 .388531618435 0         1.4175  1.736  7.982716049383
 -4.616703146464 .385638297872 0 1.406373626374  1.736  8.372839506173
 -6.038658871342 .631188118812 0 1.463118131868  1.736 10.207407407407
 -7.802465286548 .843222985634 0 1.872568681319 1.2627 13.661728395062
 -6.738130217816 .659314313114 0 1.528763736264    1.8 10.345679012346
 -8.898149315437  .77528349919 0  1.65782967033    1.8 11.572839506173
 -7.857978882178 .772779700115 0 1.893708791209    1.8 12.123456790123
 -7.629367819706  .49504950495 0 2.330975274725 1.0168  4.925925925926
 -5.637141076827 .431034482759 0 1.949340659341  1.379  6.049382716049
 -7.595590772216 .413907284768 0 1.654491758242 1.4784  6.733333333333
 -6.161708963165 .446841294299 0 1.614436813187 1.6146  6.856790123457
 -9.585797856799  .45101088647 0 1.544340659341 1.5862  6.683950617284
 -6.701175396079 .386151797603 0 1.378557692308  1.768  8.143209876543
 -8.070101066651 .679012345679 0  1.55657967033  1.768 10.427160493827
 -5.181236055153 .384812724474 0 2.266442307692  1.141  4.738271604938
 -5.463168183523 .384172109105 0 2.130700549451 1.3277  5.518518518519
  -5.38139545377 .464297150176 0 1.711236263736 1.5939  6.597530864198
 -6.163448235385 .497768623412 0 1.732376373626  1.425  7.187654320988
 -7.450346159145 .380952380952 0 1.458667582418  1.736  8.118518518519
 -5.578863554699 .376850605653 0 1.443090659341  1.736  8.874074074074
 -5.203481508363  .60286360211 0 1.447541208791  1.736  9.706172839506
 -7.324715240714  .82023239918 0 2.050590659341  1.677 13.074074074074
  -7.39708396913 .719637223975 0 2.079519230769    1.8 18.318518518519
 -7.579640177217 .729562262642 0 1.970480769231  1.736  21.76049382716
 -6.841955237469 .382848392037 0 2.127362637363 1.3419  5.637037037037
 -6.924573150893 .449194547708 0 1.648928571429  1.638  6.859259259259
 -7.066129412843 .720504353047 0  1.65782967033 1.4972  8.120987654321
 -7.532588465879 .595681310499 0 1.533214285714    1.8  9.525925925926
 -6.654976390355 .598251265532 0 1.630013736264    1.8 11.046913580247
 -5.449077174338 .445879458795 0  1.86478021978 1.6016  7.404938271605
 -6.244589705636 .578313253012 0 1.730151098901  1.768   9.83950617284
 -6.245083863989 .716364450414 0 1.862554945055 1.7933  11.98024691358
 -7.318705638377  .77399380805 0 1.920412087912   1.76 13.474074074074
 -5.468062504112 .440917107584 0 1.852541208791 1.3608  5.906172839506
 -8.017784479964 .415282392027 0 1.667843406593 1.4025  6.553086419753
 -6.229720910417 .446291166513 0 1.602197802198 1.6195  6.750617283951
 -7.620248469214 .391363022942 0         1.4175  1.728  7.767901234568
 -7.106055733275 .620181025813 0 1.746840659341 1.4235  6.143209876543
 -5.881441826009 .449055435119 0  1.65782967033 1.5939  7.222222222222
 -7.009599786801 .458280657396 0 1.604423076923 1.4208  7.523456790123
 -5.896781323347 .613195977434 0 1.469793956044  1.768  9.308641975309
 -7.275763014923 .664609541894 0 1.534326923077    1.8 10.395061728395
 -6.718326740432 .776598498576 0 1.733489010989 1.6401 11.251851851852
-10.054804973755 .465766185375 0 2.201909340659 1.1136  7.641975308642
 -8.044587548282 .487908358082 0  1.98271978022 1.2627  8.876543209877
 -7.226972850754 .373878364905 0 2.332087912088  1.092  6.037037037037
 -7.000540728132 .428481411468 0  2.79717032967   .906  4.286419753086
 -6.320242054469  .43935052531 0 2.470054945055   .992  5.234567901235
-10.058397075003 .277777777778 0 2.789381868132  .7896   3.83950617284
 -9.092117377617 .375939849624 0 2.438901098901 1.0368  5.138271604938
 -8.722849008341        .24375 0 2.836112637363  .8586  3.444444444444
 -9.875475937897 .436724565757 0 2.542376373626 1.0106   5.41975308642
 -9.978877371848 .409722222222 0 1.858104395604  1.295 13.116049382716
 -9.180665140251 .398514015535 0 1.820274725275  1.295  13.38024691358
 -9.082277013601 .525259284041 0 1.530989010989  1.295 15.748148148148
 -8.312732485058 .622621930128 0 1.650041208791  1.183 18.441975308642
-10.187085015326 .333531864205 0 2.612472527473  1.053  4.698765432099
 -9.703643207457 .364356435644 0 2.087307692308 1.1682  8.587654320988
   -8.6207644385 .430816016219 0 2.280906593407 1.0205  8.876543209877
  -9.30211179366 .634495084897 0 1.952678571429 1.0496 16.037037037037
 -8.978212908876 .293654955427 0 2.553502747253  1.002  4.555555555556
-10.122375976667 .349397590361 0 2.122912087912 1.1352  8.333333333333
 -6.217754549513 .450127877238 0 2.584656593407   .948  4.920987654321
  -6.75767044042 .497695852535 0 2.388832417582 1.0354  5.308641975309
 -6.617200108078 .467532467532 0 2.298708791209 1.1008   5.62962962963
 -9.605431006932 .387858347386 0 1.773543956044 1.2395  7.034567901235
 -8.108781000785  .42731277533 0 2.335425824176 1.0332  6.414814814815
    -5.104170658  .31914893617 0 2.692582417582  .9699  4.555555555556
 -6.590614088581 .240629338269 0 2.480068681319 1.1008  6.417283950617
 -7.765050850697 .328293736501 0 2.060604395604 1.1946  7.404938271605
  -8.59832323573 .376114773168 0 2.182994505495 1.2627  8.320987654321
 -8.703890107146 .441330998249 0 2.082857142857 1.2648 10.234567901235
 -6.382279292207 .400962309543 0 2.253263157895 1.1502  4.782296650718
  -6.99530621771 .371609067261 0 2.161789473684  1.278  5.418660287081
 -7.725352136363 .347705146036 0 1.964947368421 1.4592  6.715311004785
 -7.397157080116 .315357931252 0 2.130526315789 1.6068   6.66028708134
 -7.590750652679 .424088210348 0 1.660421052632 1.6536  9.294258373206
 -5.869501152436 .357057843371 0 1.511052631579  1.768  9.468899521531
  -7.54825092196 .510667271902 0 1.420736842105  1.768 10.784688995215
 -5.868270574208 .498338870432 0 1.326947368421  1.816 11.698564593301
end

If anyone can guide me through it, I appreciate it!!

Max

Gologit2

$
0
0
Hi all my name is Alistair,


I've taken interest in the generalised ordered logit model, more specifically in stata, the gologit2 command.

I'm trying to convert this linear equation: Ηij = β0 + β1Χij + β2μj + ∑q γqΖqij + εij
into a generalised ordered logit regression equation. Where H = happiness, X = absolute income, u = relative income, γ = effect of control variables, Z = control variables.

Therefore I'm wondering if it would be possible to receive help on how to convert this above equation as after trying numerous approaches I've made next to no progress. Thank you very much.

Creation of groups

$
0
0
Hi everyone,
I would like to create a variable that takes the same value if some students have direct or indirect contact.
For instance:

Student 1 attends Class A
Student 2 attends classes A and B
Student 3 attends class C
Student 4 attends classes B and D

Then the new variable will have the same value for students 1, 2 and 4 (because 1 is doing class A with 2, and 4 is doing class B with 2), but a different value from student 3 who is not having any class with the others.

Do you think it is possible?

Thanks a lot




line graph, by() on different y-axis scales

$
0
0
Hi there

I'm plotting several graphs using the -line- command and the by() option.


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte sex int year float rate
1 1999 0.5
1 2000 1.5
1 2000 2.0
1 2002 2.3
1 2003 2.5
1 2004 3.0
1 2005 3.4
2 1999 17.5
2 2000 22.5
2 2001 27.5
2 2002 32.5
2 2003 37.5
2 2004 42.5
2 2005 47.5
end
I like how Stata automatically scales the y-axes to fit the line graphs nicely when plotting each sex separately:

line rate year if sex == 1, yscale(range(0 .)) ylabel(#5)
line rate year if sex == 2, yscale(range(0 .)) ylabel(#5)

But when I use the by() function to plot the graphs side by side, the y-axes are the same for each graph:

line rate year, by(sex) yscale(range(0 .)) ylabel(#5)

Of course this is a sensible default, but for my purposes I would like each graph's y-axis to be scaled just as it is when plotting each sex separately.

Any tips on how to achieve this?

With many thanks

Converting Wave Based Data to Age Based Data

$
0
0
Hello Statlisters!

I have panel data (currently in wide form) where each person is followed over several waves, and data is collected on certain variables each wave. What I would like to do is take that data and convert it to age-based data.

So, here is some example data:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte(id w1age w2age w3age w1cat w2cat w3cat)
1 21 23 25 2 3 1
2 20 22 24 1 3 1
3 22 24 26 1 1 2
4 19 21 23 1 2 2
5 22 24 26 1 3 2
end
In this example "id" is the person specific identifier, and the prefix "w#age" variables are the age at which the person was in wave# (where there are 3 waves) and "w#cat" is the category the person is in in each of the three waves. I'd like to convert that data to something that looks like this:

id a19cat a20cat a21cat a22cat a23cat a24cat a25cat a26cat
1 . . 2 . 3 . 1 .
2 1 . 3 . 1 .
3 . . . 1 . 1 . 2
4 1 . 2 . 2 . . .
5 . . . 1 . 3 . 2
Here, the prefix "a#" corresponds to the persons age. They have missing values at ages where they weren't observed and values on the category variable at the ages for when they were observed. So, for example, person was 21 at wave 1 (w1age) and was in category 2 at wave 1 (w1cat), 3 at wave2, and 1 at wave3, and I want that to become categorized as "2" for the new variable a21cat, 3 at a23cat, 1 at a25cat, and missing on all others.

I hope I've explained this clearly.

Thanks in advance for any assistance.

Kind regards,
Ben

xtabond2 and ommitted period dummies!

$
0
0


When reproducing this code to explain the growth relationship, it drops both period 1 and period 4 dummies, and the sargan test seems to reject the null. What can I improve in the coding to avoid period 4 to be dropped and how should I interpret the results as most of them are not significant which is worrying me.

my regression is Growthit=GDPi,t-1+Ginii,t-1+PPPIi,t-1+countrydummies*i+Perioddummiest+eit
with 69 countries and t=5....


xtabond2 l.Growth L.(GINI logGDP EDUCATION PPPI) _IPer*, iv(_IPer*) gmm(L.(EDUCATIO
> N PPPI GINI logGDP)) two small nol robust
Favoring space over speed. To switch, type or click on mata: mata set matafavor speed,
> perm.
_IPeriod_4 dropped due to collinearity
Warning: Two-step estimated covariance matrix of moments is singular.
Using a generalized inverse to calculate optimal weighting matrix for two-step estim
> ation.
Difference-in-Sargan/Hansen statistics may be negative.

Dynamic panel-data estimation, two-step difference GMM
------------------------------------------------------------------------------
Group variable: country1 Number of obs = 139
Time variable : Period Number of groups = 51
Number of instruments = 27 Obs per group: min = 0
F(0, 51) = . avg = 2.73
Prob > F = . max = 3
------------------------------------------------------------------------------
| Corrected
L.Growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
GINI |
L1. | -.0004038 .001774 -0.23 0.821 -.0039652 .0031575
|
logGDP |
L1. | -.0144806 .0576446 -0.25 0.803 -.1302069 .1012456
|
EDUCATION |
L1. | .0658534 .0283553 2.32 0.024 .0089278 .122779
|
PPPI |
L1. | -.0310512 .026564 -1.17 0.248 -.0843807 .0222783
|
_IPeriod_2 | .0099311 .013796 0.72 0.475 -.0177655 .0376278
_IPeriod_3 | .0065741 .0084682 0.78 0.441 -.0104266 .0235748
_IPeriod_5 | .0006271 .0080175 0.08 0.938 -.0154687 .0167229
------------------------------------------------------------------------------
Instruments for first differences equation
Standard
D.(_IPeriod_2 _IPeriod_3 _IPeriod_4 _IPeriod_5)
GMM-type (missing=0, separate instruments for each period unless collapsed)
L(1/4).(L.EDUCATION L.PPPI L.GINI L.logGDP)
------------------------------------------------------------------------------
Arellano-Bond test for AR(1) in first differences: z = -2.30 Pr > z = 0.022
Arellano-Bond test for AR(2) in first differences: z = 0.04 Pr > z = 0.970
------------------------------------------------------------------------------
Sargan test of overid. restrictions: chi2(20) = 37.41 Prob > chi2 = 0.010
(Not robust, but not weakened by many instruments.)
Hansen test of overid. restrictions: chi2(20) = 25.23 Prob > chi2 = 0.193
(Robust, but weakened by many instruments.)

Difference-in-Hansen tests of exogeneity of instrument subsets:
iv(_IPeriod_2 _IPeriod_3 _IPeriod_4 _IPeriod_5)
Hansen test excluding group: chi2(17) = 21.38 Prob > chi2 = 0.210
Difference (null H = exogenous): chi2(3) = 3.85 Prob > chi2 = 0.279

Define Asymmetric Variables

$
0
0
I have simple questions I need to clarify it for me and if it is possible to share some articles about it

If I Have one variable and I need portioning the variables into two variables :
1- Once the variable has a positive value or negative value
I would to study the analysis for asymmetric shocks (negative or positive):
I have this command and I want from you to explain it for me very well and if there is any suggestion it will be appreciated"

command:
suppose my variable is oil supply shock(OSS)

// oil supply
gen sl=0
replace sl=1 if OSS<0
gen sh= 1-sl
gen OSS_N = OSS*sl
gen OSS_P = OSS*sh

Explain for me the above command and weather is write or not to divide the pol shocks to positive and negative shocks

Creating new ID using old ID

$
0
0
I have a pretty open problem. I have an ID that is a string variable but I want to create a new ID that is a real variable. This is an example of the ID data below; you can see that it changes after the 12th number (this pattern of 12 is not constant however and I have over 770,000 observations)

ssuid
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413607018
00011413613418
00011413613418
00011413613418

I have already tried to convert the string variable into a real variable but due to the number being so large it doesn't work as a unique identifier. I feel I could make some sort of loop that compares ssuid(_n) with ssuid(_n-1) then I could make a new variable that simply counts from 1 for each new ssuid. This may not be the best way I am not sure (I am novice at Stata if it is not already obvious).

My ultimate goal is to use my data for fixed effects and random effects but this string variable is not working for using the xtset or tsset command. (This may really be where my problem is :/ )

All help would be wonderful.

Viewing all 72868 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>