# Standard error of a hypothetical predicted value in logistic regression?

I'm not sure how to ask this question to Google, so I'm coming here.

Learning (some) stats long ago, I was shown that once you've calculated your regression results and built a new model (in plain English: equation) with the coefficients as your betas, you could "predict" the value of the outcome for a hypothetical set of values for the inputs/covariates/independent variables. I recall being able to do this for all sorts of variations on regression, even logistic regression.

My question is: while I'm pretty sure this works to get a number for an outcome, how can I (or can I?) get an estimate of the precision of such a "prediction"?

Here's an example logistic regression result in Stata. I'm trying to model the log odds of being admitted to university (admit) as a linear combination of the predictors GRE (GRE test score), GPA (Grade point average), and rank (High school rank, factor variable, range: 1 - 4, with 1 being highly ranked). (Here's the source dataset: data)

Crudely, the regression model looks like this before analysis (dummy variables used for rank):

logit(admit) = b0 + b1(GRE) + b2(GPA) + b3(rank=2) + b4(rank=3) + b5(rank=4)

Here are the results:

And here's the complete model:

logit(admit) = -3.989 + .00226(GRE) + .804(GPA) - .675(rank=2) - 1.34(rank=3) - 1.55(rank=4)

All normal so far, I think. Now, say that I wanted to know what the log odds of getting admitted to college were for a student with a 650 GRE, a 3.24 GPA, and who went to a rank-1 high school. Plug and chug, right?

logit(admit) = -3.98+0.0022(650)+.804(3.24) = .05496

(Which in odds ratio form is exp(.05496) = 1.056 )

So: how precise is this estimate? Can I get a 95% CI for it somehow, or even an SE?

Thanks!

• Did my answer clear thing up? Mar 4, 2021 at 18:15
• Hey Dimitriy! Sorry about the delayed reply here and thanks for posting. I've been drowning in work and haven't had time to check SX stuff (among other things), so I'm only just catching up. I'll have a good read through in the morning and update you! Mar 6, 2021 at 7:26

Coefficients are just random variables, with variances and covariance, and there are rules for calculating functions of random variables that you learn in Stats 101. They all involve either calculating linear combinations of the coefficients or else non-linear combinations and using the Delta Method to get the approximate variance of the output.

Here are two (of many) ways you might use to do that in Stata. The nlcom/lincom is more manual (but helps build intuition), margins is much easier:

. sysuse auto, clear
(1978 Automobile Data)

. logit foreign c.mpg, nolog

Logistic regression                             Number of obs     =         74
LR chi2(1)        =      11.49
Prob > chi2       =     0.0007
Log likelihood =  -39.28864                     Pseudo R2         =     0.1276

------------------------------------------------------------------------------
foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg |   .1597621   .0525876     3.04   0.002     .0566922     .262832
_cons |  -4.378866   1.211295    -3.62   0.000    -6.752961   -2.004771
------------------------------------------------------------------------------

. /* linear combination of logit coefficients */
. lincom _b[_cons] + _b[mpg]*22

( 1)  22*[foreign]mpg + [foreign]_cons = 0

------------------------------------------------------------------------------
foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
(1) |   -.864099   .2779994    -3.11   0.002    -1.408968   -.3192302
------------------------------------------------------------------------------

. margins, predict(xb) at(mpg==22)

Adjusted predictions                            Number of obs     =         74
Model VCE    : OIM

Expression   : Linear prediction (log odds), predict(xb)
at           : mpg             =          22

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_cons |   -.864099   .2779994    -3.11   0.002    -1.408968   -.3192302
------------------------------------------------------------------------------

. /* predicted Pr(Y=1 | mpg =22) = p */
. nlcom phat_at_mpg_22:invlogit(_b[_cons] + _b[mpg]*22)

phat_at_m~22:  invlogit(_b[_cons] + _b[mpg]*22)

--------------------------------------------------------------------------------
foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
phat_at_mpg_22 |   .2964837   .0579854     5.11   0.000     .1828343     .410133
--------------------------------------------------------------------------------

. margins, at(mpg==22)

Adjusted predictions                            Number of obs     =         74
Model VCE    : OIM

Expression   : Pr(foreign), predict()
at           : mpg             =          22

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_cons |   .2964837   .0579854     5.11   0.000     .1828343     .410133
------------------------------------------------------------------------------

. /* odds p/(1-p) at mpg = 22 */
. nlcom or_at_mpg_2:invlogit(_b[_cons] + _b[mpg]*22)/(1-invlogit(_b[_cons] + _b[mpg]*22))

or_at_mpg_2:  invlogit(_b[_cons] + _b[mpg]*22)/(1-invlogit(_b[_cons] + _b[mpg]*22))

------------------------------------------------------------------------------
foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
or_at_mpg_2 |   .4214311   .1171576     3.60   0.000     .1918064    .6510557
------------------------------------------------------------------------------

. margins, expression(predict()/(1-predict())) at(mpg==22)

Adjusted predictions                            Number of obs     =         74
Model VCE    : OIM

Expression   : predict()/(1-predict())
at           : mpg             =          22

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_cons |   .4214311   .1171576     3.60   0.000     .1918064    .6510557
------------------------------------------------------------------------------

. /* log odds ln(p/(1-p)): comapare to (1) */
. nlcom ln_or_at_mpg_2:ln(invlogit(_b[_cons] + _b[mpg]*22)/(1-invlogit(_b[_cons] + _b[mpg]*22)))

ln_or_at_m~2:  ln(invlogit(_b[_cons] + _b[mpg]*22)/(1-invlogit(_b[_cons] + _b[mpg]*22)))

--------------------------------------------------------------------------------
foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
ln_or_at_mpg_2 |   -.864099   .2779994    -3.11   0.002    -1.408968   -.3192302
--------------------------------------------------------------------------------

. margins, expression(ln(predict()/(1-predict()))) at(mpg==22)

Adjusted predictions                            Number of obs     =         74
Model VCE    : OIM

Expression   : ln(predict()/(1-predict()))
at           : mpg             =          22

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_cons |   -.864099   .2779994    -3.11   0.002    -1.408968   -.3192302
------------------------------------------------------------------------------


Note how the last set is identical to the first.

Code:

sysuse auto, clear
logit foreign c.mpg, nolog
/* linear combination of logit coefficients */
lincom _b[_cons] + _b[mpg]*22
margins, predict(xb) at(mpg==22)
/* predicted Pr(Y=1 | mpg =22) = p */
nlcom phat_at_mpg_22:invlogit(_b[_cons] + _b[mpg]*22)
margins, at(mpg==22)
/* odds p/(1-p) at mpg = 22 */
nlcom or_at_mpg_2:invlogit(_b[_cons] + _b[mpg]*22)/(1-invlogit(_b[_cons] + _b[mpg]*22))
margins, expression(predict()/(1-predict())) at(mpg==22)
/* log odds ln(p/(1-p)): compare to (1) */
nlcom ln_or_at_mpg_2:ln(invlogit(_b[_cons] + _b[mpg]*22)/(1-invlogit(_b[_cons] + _b[mpg]*22)))
margins, expression(ln(predict()/(1-predict()))) at(mpg==22)

• Ah okay this is very helpful, but now I have another question (maybe it merits a second post; let me know): Mar 6, 2021 at 7:38
• what if I want to estimate the effect of X on Y given specific values of a set of covariates A? To use the sysauto example you used, what if I want to model the effect of mpg on foreign given that price = 5000 and turn = 42 ? I'm essentially asking for the regression to be run on a subset of the data -- is that valid? Mar 6, 2021 at 7:47
• Just use -margins, dydx(mpg) at(price==5e3 turn==42)- after -reg foreign c.(price turn), robust-. You can run a regression on a subset if you have enough data, but it’s not clear what you gain by doing that that. Mar 6, 2021 at 7:55