Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: predict stdp after logistic regression;


From   May Boggess <[email protected]>
To   [email protected]
Subject   Re: st: predict stdp after logistic regression;
Date   27 Apr 2004 10:35:58 -0500

On Tuesday, Gerben asked:

> Who can tell me what the formula is that Stata uses for the 
> calculation of stdp after logistic regression? 
> I was unable to find it in the manuals and in the Statalist archives.

After -logistic-, -predict- with the option -stdp- is calculating the
standard error of the linear predictor, -xb-. 

Given the covariate settings (values of the independent variables, the
x's) in a particular observation, the linear predictor for that
observation is a function of the coefficients (the b's) of the model. 

So we use the delta method to calculate its standard error. This means
pre and post multiplying the covariance matrix e(V) by the first
derivatives (with respect to the b's) of the linear predictor.

Let's look at an example:

 clear 
 sysuse auto
 keep for turn
 logistic for turn, coef
 matrix V = e(V)
 predict xb,xb
 predict stdp,stdp
 list xb stdp in 1/5

For our example, the linear predictor xb is:

   f = b0 + b1*turn

so the derivatives with repect to the b's are:

   df/db1 = turn
   df/db0 = 1 

So the matrix we are going to multiply the covariance matricx e(V) by
should be a 2 X 1, with the value of turn in the first component and 1
in the second. (I know what order to put them in - it has to be the same
order as the columns of e(V)).

 clear
 sysuse auto
 keep for turn
 logistic for turn, coef
 matrix V = e(V)
 predict xb,xb
 gen myxb = _b[_cons] + _b[turn]*turn
 list xb myxb in 1

 predict stdp,stdp
 list xb stdp in 1

 mat X = J(1,2,0)
 mat X[1,1]=turn[1]
 mat X[1,2]=1 
 mat list X
 matrix XVX = X*V*X'
 di sqrt(XVX[1,1])

Now this was just getting the standard error for the linear predictor
for the first observation. I could repeat this for each observation if I
wanted. But I can take advantage of Stata's neat matrix functions to
compute a bunch of these guys at one time. I'll do just the first five,
so you can see what I mean:

 clear
 sysuse auto
 keep for turn
 logistic for turn, coef
 matrix V = e(V)
 keep in 1/5
 predict stdp,stdp
 list stdp 
 gen cons = 1
 mkmat  turn cons , matrix(X)
 matrix XVX = X*V*X'
 di sqrt(XVX[1,1])
 di sqrt(XVX[2,2])
 di sqrt(XVX[3,3])
 di sqrt(XVX[4,4])
 di sqrt(XVX[5,5])

Now, the only thing that is not slick now is the last piece where I take
the squareroot of each diagonal entry. Here is a neat trick for that:

 clear
 sysuse auto
 keep for turn
 logistic for turn, coef
 matrix V = e(V)
 keep in 1/20
 predict stdp,stdp
 gen cons = 1
 mkmat  turn cons , matrix(X)
 matrix XVX = X*V*X'
 matrix v = vecdiag(XVX)
 matrix se = vecdiag(cholesky(diag(v)))'
 svmat se
 list stdp se1

If you try this with many more observations you will probably have to
increase your matsize (see -help matsize-).

-- May
[email protected]




*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index