MADlib
1.1 A newer version is available
User Documentation
|
The dependent variable may be a Boolean value or a categorial variable that can be represented with a Boolean expression.
logregr_train(tbl_source, tbl_output, dep_col, ind_col, grouping_col := NULL, max_iter := 20, optimizer := 'irls', tolerance := 0.0001, verbose := False)
Text value. The name of the table containing the training data.
Text value. Name of the generated table containing the output model.
Text value. Name of the dependent variable column (of type BOOLEAN) in the training data or an expression evaluating to a BOOLEAN.
Text value. Expression list to evaluate for the independent variables. An intercept variable is not assumed. It is common to provide an explicit intercept term by including a single constant 1
term in the independent variable list.
Text value. An expression list used to group the input dataset into discrete groups, running one regression per group. Similar to the SQL "GROUP BY" clause. When this value is NULL, no grouping is used and a single result model is generated. Default value: NULL.
Integer value. The maximum number of iterations that are allowed. Default value: 20.
Text value. The name of the optimizer to use:
Float value. The difference between log-likelihood values in successive iterations that should indicate convergence. A zero disables the convergence criterion, so that execution stops after nIterations
have completed. Default: 0.0001.
Text. Grouping columns, if provided in input. This could be multiple columns depending on the grouping_col
input.
Float array. Vector of the coefficients of the regression.
Float. The log-likelihood \( l(\boldsymbol c) \).
Float array. Vector of the standard error of the coefficients.
Float array. Vector of the z-statistics of the coefficients.
Float array. Vector of the p-values of the coefficients.
Float array. The odds ratio, \( \exp(c_i) \).
Float array. The condition number of the \(X^{*}X\) matrix. A high condition number is usually an indication that there may be some numeric instability in the result yielding a less reliable model. A high condition number often results when there is a significant amount of colinearity in the underlying design matrix, in which case other regression techniques may be more appropriate.
nIterations
argument if a tolerance
parameter is provided and the algorithm converges before all iterations are completed. sql> CREATE TABLE patients (id INTEGER NOT NULL, second_attack INTEGER, treatment INTEGER, trait_anxiety INTEGER); sql> COPY patients FROM STDIN WITH DELIMETER '|'; 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 \.
sql> SELECT madlib.logregr_train( 'patients', 'patients_logregr', 'second_attack', 'ARRAY[1, treatment, trait_anxiety]', Null, 20, 'irls' );
-- Set extended display on for easier reading of output sql> \x on sql> SELECT * from patients_logregr; coef | {5.59049410898112,2.11077546770772,-0.237276684606453} log_likelihood | -467.214718489873 std_err | {0.318943457652178,0.101518723785383,0.294509929481773} z_stats | {17.5281667482197,20.7919819024719,-0.805666162169712} p_values | {8.73403463417837e-69,5.11539430631541e-96,0.420435365338518} odds_ratios | {267.867942976278,8.2546400100702,0.788773016471171} condition_no | 179.186118573205 num_iterations | 9 -- Alternatively, unnest the arrays in the results for easier reading of output sql> \x off sql> SELECT unnest(array['intercept', 'treatment', 'trait_anxiety' ]) as attribute, unnest(coef) as coefficient, unnest(std_err) as standard_error, unnest(z_stats) as z_stat, unnest(p_values) as pvalue, unnest(odds_ratios) as odds_ratio FROM patients_logregr;
(Binomial) logistic regression refers to a stochastic model in which the conditional mean of the dependent dichotomous variable (usually denoted \( Y \in \{ 0,1 \} \)) is the logistic function of an affine function of the vector of independent variables (usually denoted \( \boldsymbol x \)). That is,
\[ 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. 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 one of three algorithms:
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.
A somewhat random selection of nice write-ups, with valuable pointers into further literature.
[1] Cosma Shalizi: Statistics 36-350: Data Mining, Lecture Notes, 18 November 2009, http://www.stat.cmu.edu/~cshalizi/350/lectures/26/lecture-26.pdf
[2] Thomas P. Minka: A comparison of numerical optimizers for logistic regression, 2003 (revised Mar 26, 2007), http://research.microsoft.com/en-us/um/people/minka/papers/logreg/minka-logreg.pdf
[3] Paul Komarek, Andrew W. Moore: Making Logistic Regression A Core Data Mining Tool With TR-IRLS, IEEE International Conference on Data Mining 2005, pp. 685-688, http://komarix.org/ac/papers/tr-irls.short.pdf
[4] D. P. Bertsekas: Incremental gradient, subgradient, and proximal methods for convex optimization: a survey, Technical report, Laboratory for Information and Decision Systems, 2010, http://web.mit.edu/dimitrib/www/Incremental_Survey_LIDS.pdf
[5] A. Nemirovski, A. Juditsky, G. Lan, and A. Shapiro: Robust stochastic approximation approach to stochastic programming, SIAM Journal on Optimization, 19(4), 2009, http://www2.isye.gatech.edu/~nemirovs/SIOPT_RSA_2009.pdf