Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom one-tailed confidence intervals for coefplot

    I'm using the community-contributed command from SSC coefplot. The command is described here:

    http://repec.sowi.unibe.ch/stata/coefplot/

    Instead of using the default confidence intervals that are relevant for a two-tailed test, I would like to show the relevant confidence intervals for a one-tailed test. In practice, that means that for the relevant confidence intervals for a one-tailed test based from an OLS regression with alpha = 0.5, I can use the results of the same OLS with alpha = 0.1 to define the lower or upper bound depending on the alternative hypothesis. In the graph, that means that the confidence interval will extend all the way to one of the margins of the graph.

    Here is an example of the data I'm working with:

    Code:
     * Example generated by -dataex-. To install: ssc install dataex clear input float dep byte indep -.005 1  1.75 0  1.75 1 -.005 0  .005 1  1.75 0  .875 1  .005 0  .005 1  .875 0  -.13 0  1.75 1  .875 1 -.875 0  .625 0  .005 0  .875 1 -1.25 1  .005 1     4 0 end
    This is the default behavior:

    Code:
    regress dep indep    
    
    estimates store A1      
    
    coefplot A1, drop(_cons) xline(0)
    fig1.png

    In the graph above, for a one-tailed test, the line would extend to one of the margins depending on the H1.

    The authors of coefplot documented how the program retrieves the CIs in here:

    http://repec.sowi.unibe.ch/stata/coe...rvals.html#h-3

    In general, they compute the CIs using standard formulas based on estimation results. There is also the option to include custom variances, degrees of freedom, and standard errors. I do not think any of this options would work to graph the relevant one-tailed confidence interval. The other alternative is to directly provide confidence intervals using the option ci(). I've explored this option without success. Their example uses precomputed confidence intervals from bootstrap. The authors described how to use the option here:

    http://repec.sowi.unibe.ch/stata/coe...tml#stlog-1-ci

    Here is the relevant text:

    "ci(spec) specifies the source from which to collect confidence intervals. Default is to compute confidence intervals for the levels specified in levels() using variances/standard errors (and, possibly, degrees of freedom). The ci() option is useful to plot confidence intervals that have been provided by the estimation command (such as, e.g., bootstrap). spec is cispec [cispec ...] where cispec is name to get the lower and upper confidence limits from rows 1 and 2 of e(name) (or matrix name), respectively. Alternatively, cispec may be (mspec mspec) to identify the lower and upper confidence limits, with mspec as above for b(). For example, after bootstrap, ci(ci_bc) would get bias-corrected confidence intervals from rows 1 and 2 of e(ci_bc). The same could be achieved by ci((ci_bc[1] ci_bc[2]))."

    First, I tried to just create a matrix with the lower and upper bound for the confidence intervals:

    Code:
    matrix A = ( -1 \ 1 )
    
    coefplot A1, drop(_cons) xline(0) ci(A)
    But it doesn't work

    (A1: e(A) not found)
    (A1: e(A) not found)
    (A1: could not determine CI1)

    I might be misunderstanding what they meant by "matrix name". Then I tried to put the lower and upper bound in either e(b) or e(V). The issue with e(b) is that the coefplot needs to read the lower bound from [1,1] and the upper bound from [2,1], but e(b) is a vector. To change e(V), I tried with:

    Code:
    program define oneTailedCI, eclass
            version 14.2
            
            matrix a = e(V)
            
            matrix b = (0.037 , 0.4 )
            
            matrix a[1,1] = b[1,1]
            
            matrix a[2,1] = b[1,2]
            
            matrix a[1,2] = b[1,2]
            
            ereturn repost V = a, ADDCONS
            
    end
    but the resulting e(V) is just full of zeros. I don't see where is e(V) changing in such a way.

    I've exhausted my options and would appreciate any pointer to something that might work. Ideally, I would end up with a graph like this:

    Code:
    sysuse auto, clear
    
    regress price mpg
    
     coefplot, drop(_cons) xline(0) msymbol(d) mcolor(white)    ///
       levels(99 95 90 80 70) ciopts(lwidth(3 ..) lcolor(*.2 *.4 *.6 )) ///
        legend(order(1 "99" 2 "95" 3 "90") rows(1))
    fig2.png


    But the dark blue in the center would extend all the way to one of the margins to reflect the one-tailed test.

    Thanks!
    Last edited by Tabare Capitan; 12 Jan 2019, 16:28.

  • #2
    The links that you provide for coefplot are useful, but the definitive source is Stata Journal. The command is also available from SSC. It is easy to define a matrix as the input, where you specify the desired confidence intervals. Here, I replicate coefplot's output following a regression and using a matrix that I derive from the same regression.

    Code:
    sysuse auto, clear
    qui regress mpg weight i.rep78
    coefplot, drop(_cons) scheme(s1color)
    *SAME GRAPH USING MATRIX AS INPUT
    qui regress mpg weight i.rep78
    mat r= r(table)
    *WE JUST NEED THE FIRST 7 ROWS IN THIS INSTANCE.
    mat R= r[1..7,1..7]
    *SAME PLOT AS RUNNING COEFPLOT AFTER REGRESSION
    coefplot matrix(R), drop(_cons) se(2) scheme(s1color)
    The matrix "R" above is a regular Stata matrix, and you can change its entries as desired

    Code:
    . mat list R
    
    R[7,7]
                                1b.          2.          3.          4.          5.            
                weight       rep78       rep78       rep78       rep78       rep78       _cons
         b  -.00550304           0  -.47860434  -.47156229  -.59903186   2.0862757   38.059415
        se     .000601           .   2.7650348   2.5531445   2.6065995   2.7248169   3.0933614
         t  -9.1564473           .  -.17309162  -.18469863  -.22981355   .76565721   12.303578
    pvalue   3.475e-13           .   .86313434   .85405871   .81898119    .4467394   2.091e-18
        ll  -.00670404           .  -6.0040854  -5.5736145  -5.8079052  -3.3588364   31.877825
        ul  -.00430203           .   5.0468767   4.6304899   4.6098415   7.5313878   44.241005
        df          63          63          63          63          63          63          63

    coefplot graph:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	42.6 KB
ID:	1478864



    Comment


    • #3
      Thanks a lot for the reply Andrew. I will try to do my graph using a matrix as the input.

      Comment

      Working...
      X