MADlib
1.3 A newer version is available
User Documentation
|
Multinomial logistic regression is a widely used regression analysis tool that models the outcomes of categorical dependent random variables. The model assumes that the conditional mean of the dependent categorical variables is the logistic function of an affine combination of independent variables. Multinomial logistic regression finds the vector of coefficients that maximizes the likelihood of the observations.
mlogregr( source, depvar, indepvar, max_num_iterations, optimizer, precision, ref_category )Arguments
TEXT. The name of the table containing the input data.
The training data is expected to be of the following form:
{TABLE|VIEW} source ( ... dependentVariable INTEGER, independentVariables FLOAT8[], ... )
Table names can be optionally schema qualified and table and column names should follow the same case-sensitivity and quoting rules as in the database.
max_num_iterations
iterations. ref_category | INTEGER. The reference category. Categories are encoded as integers with values from {0, 1, 2,..., numCategories – 1} |
---|---|
coef | FLOAT8[]. An array of coefficients, \( \boldsymbol c \). |
log_likelihood | FLOAT8. The log-likelihood, \( l(\boldsymbol c) \). |
std_err | FLOAT8[]. An array of the standard errors. |
z_stats | FLOAT8[]. An array of the Wald z-statistics. |
p_values | FLOAT8[]. An array of the Wald p-values. |
odds_ratios | FLOAT8[]. An array of the odds ratios. |
condition_no | FLOAT8. The condition number of the matrix, computed using the coefficients of the iteration immediately preceding convergence. |
num_iterations | INTEGER. The number of iterations executed before the algorithm completed. |
DROP TABLE IF EXISTS patients; CREATE TABLE patients (id INTEGER NOT NULL, second_attack INTEGER, treatment INTEGER, trait_anxiety INTEGER); COPY patients FROM STDIN WITH DELIMITER '|'; 1 | 1 | 1 | 70 3 | 1 | 1 | 50 5 | 1 | 0 | 40 7 | 1 | 0 | 75 9 | 1 | 0 | 70 11 | 0 | 1 | 65 13 | 0 | 1 | 45 15 | 0 | 1 | 40 17 | 0 | 0 | 55 19 | 0 | 0 | 50 2 | 1 | 1 | 80 4 | 1 | 0 | 60 6 | 1 | 0 | 65 8 | 1 | 0 | 80 10 | 1 | 0 | 60 12 | 0 | 1 | 50 14 | 0 | 1 | 35 16 | 0 | 1 | 50 18 | 0 | 0 | 45 20 | 0 | 0 | 60 \.
\x on Expanded display is on. SELECT * FROM madlib.mlogregr( 'patients', 'second_attack', 'ARRAY[1, treatment, trait_anxiety]', 20, 'irls', 0.0001, 1 );Result:
-[ RECORD 1 ]--+---------------------------------------------------------- ref_category | 1 coef | {6.3634699417818,1.02410605239327,-0.119044916668605} log_likelihood | -9.41018298388876 std_err | {3.21389766375092,1.17107844860319,0.0549790458269306} z_stats | {1.97998524145757,0.874498248699546,-2.16527796868917} p_values | {0.0477051870698143,0.381846973530451,0.0303664045046178} odds_ratios | {580.256322802414,2.78460505656035,0.887767924288744} condition_no | 106329.420371447 num_iterations | 5
\[ E[Y \mid \boldsymbol x] = \sigma(\boldsymbol c^T \boldsymbol x) \]
for some unknown vector of coefficients \( \boldsymbol c \) and where \( \sigma(x) = \frac{1}{1 + \exp(-x)} \) is the logistic function. Multinomial logistic regression finds the vector of coefficients \( \boldsymbol c \) that maximizes the likelihood of the observations.Let
By definition,
\[ P[Y = y_i | \boldsymbol x_i] = \sigma((-1)^{y_i} \cdot \boldsymbol c^T \boldsymbol x_i) \,. \]
Maximizing the likelihood \( \prod_{i=1}^n \Pr(Y = y_i \mid \boldsymbol x_i) \) is equivalent to maximizing the log-likelihood \( \sum_{i=1}^n \log \Pr(Y = y_i \mid \boldsymbol x_i) \), which simplifies to
\[ l(\boldsymbol c) = -\sum_{i=1}^n \log(1 + \exp((-1)^{y_i} \cdot \boldsymbol c^T \boldsymbol x_i)) \,. \]
The Hessian of this objective is \( H = -X^T A X \) where \( A = \text{diag}(a_1, \dots, a_n) \) is the diagonal matrix with \( a_i = \sigma(\boldsymbol c^T \boldsymbol x) \cdot \sigma(-\boldsymbol c^T \boldsymbol x) \,. \) Since \( H \) is non-positive definite, \( l(\boldsymbol c) \) is convex. There are many techniques for solving convex optimization problems. Currently, logistic regression in MADlib can use:
We estimate the standard error for coefficient \( i \) as
\[ \mathit{se}(c_i) = \left( (X^T A X)^{-1} \right)_{ii} \,. \]
The Wald z-statistic is
\[ z_i = \frac{c_i}{\mathit{se}(c_i)} \,. \]
The Wald \( p \)-value for coefficient \( i \) gives the probability (under the assumptions inherent in the Wald test) of seeing a value at least as extreme as the one observed, provided that the null hypothesis ( \( c_i = 0 \)) is true. Letting \( F \) denote the cumulative density function of a standard normal distribution, the Wald \( p \)-value for coefficient \( i \) is therefore
\[ p_i = \Pr(|Z| \geq |z_i|) = 2 \cdot (1 - F( |z_i| )) \]
where \( Z \) is a standard normally distributed random variable.
The odds ratio for coefficient \( i \) is estimated as \( \exp(c_i) \).
The condition number is computed as \( \kappa(X^T A X) \) during the iteration immediately preceding convergence (i.e., \( A \) is computed using the coefficients of the previous iteration). A large condition number (say, more than 1000) indicates the presence of significant multicollinearity.
The multinomial logistic regression uses a default reference category of zero, and the regression coefficients in the output are in the order described below. For a problem with \( K \) dependent variables \( (1, ..., K) \) and \( J \) categories \( (0, ..., J-1) \), let \( {m_{k,j}} \) denote the coefficient for dependent variable \( k \) and category \( j \). The output is \( {m_{k_1, j_0}, m_{k_1, j_1} \ldots m_{k_1, j_{J-1}}, m_{k_2, j_0}, m_{k_2, j_1}, \ldots m_{k_2, j_{J-1}} \ldots m_{k_K, j_{J-1}}} \). The order is NOT CONSISTENT with the multinomial regression marginal effect calculation with function marginal_mlogregr. This is deliberate because the interfaces of all multinomial regressions (robust, clustered, ...) will be moved to match that used in marginal.
A collection of nice write-ups, with valuable pointers into further literature:
[1] Annette J. Dobson: An Introduction to Generalized Linear Models, Second Edition. Nov 2001
[2] Cosma Shalizi: Statistics 36-350: Data Mining, Lecture Notes, 18 November 2009, http://www.stat.cmu.edu/~cshalizi/350/lectures/26/lecture-26.pdf
[3] Scott A. Czepiel: Maximum Likelihood Estimation of Logistic Regression Models: Theory and Implementation, Retrieved Jul 12 2012, http://czep.net/stat/mlelr.pdf
File multilogistic.sql_in documenting the multinomial logistic regression functions