0

I have two 95% confidence intervals I would like to display side by side in one coeff plot in Stata.

The first is just a regression that I run, and I use coefplot to plot the coefficient & its 95% confidence interval.

The second, though, is not a regression. I just have a variable for a coefficient value and a variable for its standard error. I would like to hard code that coefficient & SE and make coefplot display that value. Or run a "dumb" regression using the coefficient & SE that displays a coefficient such that I can plot out that second 95% confidence interval. Anyone know how to do this?

I tried looking things up a lot and trying some regressions, but haven't found a way to hardcode it yet.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47

1 Answers1

1

You can do this with ereturn post. Here's an example. I rename the variable "gear" to "HAND_CODED_COEF" to make clear that's the one I'm messing with. I'm able to change the coefficient and its standard error. (Note this uses the coefplot package available on SSC.)

sysuse auto, clear 

rename gear HAND_CODED_COEF
regress mpg price weight head HAND_CODED_COEF

* Edit coefficient matrix  
mat my_b = e(b)
mat my_b[1,4] = 15

* Edit variance matrix
mat my_V = e(V)
mat my_V[4,4] = 2.2

* Write to saved estimates 
ereturn post  my_b my_V 

* Show that matrices have been changed
mat list e(b)
mat list e(V)

* coefplot shows the hand-edited values!
coefplot , nolabel drop(_cons)

Here's what the coefplot command gets you: enter image description here

And in case it better answers the question here's code for having one real regression (m1) and one nonsense regression where you edit things (m2). Edit I also explain how to edit the point estimate and confidence interval for the constant term specifically, and I add an xline to the coefplot showing that the confidence interval is as expected.

sysuse auto, clear 

rename gear HAND_CODED_COEF

eststo m1: regress mpg foreign 

* Fake regression 
regress mpg HAND_CODED_COEF

* Edit coefficient matrix  
mat my_b = e(b)

***** The constant estimate is in the 1st row, 2nd column of e(b)
***** see the mat list command:
mat list my_b 
* We will set it to -88
mat my_b[1,2] = -88

* Edit variance matrix
mat my_V = e(V)
***** The variance term for the constant is in the
***** bottom right cell of the matrix:  
mat list my_V

* We will set the variance to 100,
* so that the standard error will be 10, 
* and the lower part of the 95% confidence interval will be 
* (-88 - 1.96*10) 
mat my_V[2,2] = 100

* Write to saved estimates 
ereturn post  my_b my_V
eststo m2  

* Show that matrices have been changed
mat list e(b)
mat list e(V)

* coefplot shows the hand-edited values for m2!
* The xline shows that the confidence interval
* is exactly where we wanted 
coefplot m1 m2 , nolabel xline(`= -88 - 1.96*10')
braces
  • 570
  • 3
  • 8
  • I don't follow this. You're editing the last coefficient and entry in `e(V)` but the others refer to the regression with the extra predictor included. I can't see that you don't need to edit all the results, for the regression of real interest PLUS the extra information. – Nick Cox Apr 08 '23 at 08:46
  • I could have misunderstood the question but I thought OP just needed an example of editing e(b) and e(V) and having that show up in coefplot...I added another code chunk that does this across two regressions in case that's clearer – braces Apr 08 '23 at 13:46
  • Hello! This is helpful, but unfortunately it isn't coding correctly. my confidence interval just becomes way too big. I just want to recode the standard error for the constant (I don't want any slope value in my regression). which matrix index should I use in the variance matrix? and how can I run the regression to make sure it is what I want? – ConfusedStudent Apr 08 '23 at 16:16
  • 1
    The code totally controls the confidence interval through e(V)--you can adjust that number to whatever you want! I've updated the 2nd code chunk so that it makes changes to the constant term. In the comments I explain where the items are in the matrices and how the confidence interval is calculated. You can leave e(b) unchanged if you only want to edit the standard error, just comment out "mat my_b[1,2] = -88." Should answer your questions although I'm not sure what you mean with "how can I run the regression to make sure it is what I want?" – braces Apr 08 '23 at 17:23
  • I see, thank you so much! That makes sense. One last question. What if instead of adding a number "10", I want to instead put in the variable var_SE, where var_SE = 10. It is defined earlier as g var_SE = 10. When I try to just write mat my_V[2,2] = var_SE ^2, I get an error "var_SE not found". – ConfusedStudent Apr 08 '23 at 20:25
  • I think it's expecting a single number there. You can replace the "mat my_V[2,2] =" line with these two lines below and it works the same. In my case I make a variable called var_SE which is always equal to 10 and then in the next line grab its very first value (var_SE[1]) when I put it in the matrix. . gen var_SE = 10 [linebreak] . mat my_V[2,2] = var_SE[1]^2 – braces Apr 08 '23 at 20:46