logistic_regression
index
logistic_regression.py

Python module for computing Logistic Regression.
Requires numpy (http://numpy.scipy.org)
 
Version: 20060629
Contact:  Jeffrey Whitaker <jeffrey.s.whitaker@noaa.gov>
 
copyright (c) by Jeffrey Whitaker.
 
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both the copyright notice and this permission notice appear in
supporting documentation.
THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

 
Modules
       
numpy

 
Functions
       
calcprob(beta, x)
calculate probabilities (in percent) given beta and x
logistic_regression(x, y, beta_start=None, verbose=False, CONV_THRESH=0.001, MAXIT=500)
Uses the Newton-Raphson algorithm to calculate maximum
likliehood estimates of a logistic regression.
 
Can handle multivariate case (more than one predictor).
 
x - rank-2 array of predictors. Number of predictors = x.shape[0]=N
y - binary outcomes (len(y) = x.shape[1])
beta_start - initial beta vector (default zeros(N+1,x.dtype)
if verbose=True, diagnostics printed for each iteration.
MAXIT - max number of iterations (default 500)
CONV_THRESH - convergence threshold (sum of absolute differences
 of beta-beta_old)
 
returns beta (the logistic regression coefficients, a N+1 element vector),
J_bar (the (N+1)x(N=1) information matrix), and l (the log-likeliehood).
J_bar can be used to estimate the covariance matrix and the standard
error beta.
l can be used for a chi-squared significance test.
 
covmat = inverse(J_bar)     --> covariance matrix
stderr = sqrt(diag(covmat)) --> standard errors for beta
deviance = -2l              --> scaled deviance statistic
chi-squared value for -2l is the model chi-squared test.
simple_logistic_regression(x, y, beta_start=None, verbose=False, CONV_THRESH=0.001, MAXIT=500)
Uses the Newton-Raphson algorithm to calculate maximum
likliehood estimates of a simple logistic regression.  
 
Faster than logistic_regression when there is only one predictor.
 
x - predictor
y - binary outcomes (len(y) = len(x))
beta_start - initial beta (default zero)
if verbose=True, diagnostics printed for each iteration.
MAXIT - max number of iterations (default 500)
CONV_THRESH - convergence threshold (sum of absolute differences
 of beta-beta_old)
 
returns beta (the logistic regression coefficients, a 2-element vector),
J_bar (the 2x2 information matrix), and l (the log-likeliehood).
J_bar can be used to estimate the covariance matrix and the standard
error beta.
l can be used for a chi-squared significance test.
 
covmat = inverse(J_bar)     --> covariance matrix
stderr = sqrt(diag(covmat)) --> standard errors for beta
deviance = -2l              --> scaled deviance statistic
chi-squared value for -2l is the model chi-squared test.