User Documentation
 All Files Functions Variables Groups
Clustered Variance

The Clustered Variance module adjusts standard errors for clustering. For example, replicating a dataset 100 times should not increase the precision of parameter estimates, but performing this procedure with the IID assumption will actually do this. Another example is in economics of education research, it is reasonable to expect that the error terms for children in the same class are not independent. Clustering standard errors can correct for this.

The MADlb Clustered Variance module includes functions to calculate linear, logistic, and multinomial logistic regression problems.

Clustered Variance Linear Regression Training Function

The clustered variance linear regression training function has the following syntax.

clustered_variance_linregr ( tbl_data, 
                             tbl_output, 
                             depvar, 
                             indvar, 
                             clustervar, 
                             groupingvar
                           )

Arguments

tbl_data
TEXT. The name of the table containing the input data.
tbl_output
TEXT. The name of the table to store the regression model.
depvar
TEXT. An expression to evaluate for the dependent variable.
indvar
TEXT. An Expression to evalue for the independent variables.
clustervar
TEXT. A comma-separated list of the columns to use as cluster variables.
groupingvar (optional)
TEXT, default: NULL. Not currently implemented. Any non-NULL value is ignored. 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.

Clustered Variance Logistic Regression Training Function

The clustered variance logistic regression training function has the following syntax.

clustered_variance_logregr( tbl_data, 
                            tbl_output, 
                            depvar, 
                            indvar, 
                            clustervar, 
                            groupingvar, 
                            max_iter, 
                            optimizer, 
                            tolerance, 
                            verbose
                          )

Arguments

tbl_data
TEXT. The name of the table containing the input data.
tbl_output
TEXT. The name of the table to store the regression model.
depvar
TEXT. An expression to evaluate for the dependent variable.
indvar
TEXT. An expression to evaluate for the independent variable.
clustervar
TEXT. A comma-separated list of columns to use as cluster variables.
groupingvar (optional)
TEXT, default: NULL. Not yet implemented. Any non-NULL values are ignored. 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.
max_iter (optional)
INTEGER, default: 20. The maximum number of iterations that are allowed.
optimizer (optional)
TEXT, default: 'irls'. The name of the optimizer to use:
  • 'newton' or 'irls': Iteratively reweighted least squares
  • 'cg': conjugate gradient
  • 'igd': incremental gradient descent.
tolerance (optional)
FLOAT8, default: 0.0001 The difference between log-likelihood values in successive iterations that should indicate convergence. A zero disables the convergence criterion, so that execution stops after n Iterations have completed.
verbose (optional)
BOOLEAN, default FALSE. Provides verbose output of the results of training.

Clustered Variance Multinomial Logistic Regression Training Function
clustered_variance_mlogregr( tbl_data, 
                             tbl_output, 
                             depvar, 
                             indvar, 
                             clustervar, 
                             ref_category, 
                             groupingvar, 
                             max_iter, 
                             optimizer, 
                             tolerance, 
                             verbose
                           )

Arguments

tbl_data
TEXT. The name of the table containing the input data.
tbl_output
TEXT. The name of the table to store the regression model.
depvar
TEXT. An expression to evaluate for the dependent variable.
indvar
TEXT. An expression to evaluate for the independent variable.
clustervar
TEXT. A comma-separated list of columns to use as cluster variables.
ref_category
INTEGER. Reference category in the range [0, num_category).
groupingvar (optional)
TEXT, default: NULL. Not yet implemented. Any non-NULL values are ignored. A comma-separated list of columns to use as grouping variables.
max_iter (optional)
INTEGER, default: 20. Maximum iteration number for logistic regression.
optimizer (optional)
TEXT, default: 'irls'. Optimization method to use for logistic regression.
tolerance
FLOAT8, default: 0.0001. The computation ends when the difference of likelihoods in two consecutive iterations is smaller than this value.
verbose (optional)
BOOLEAN, default FALSE. If TRUE, detailed information is printed when computing logistic regression.

Examples
  1. View online help for the clustered variance linear regression function.
    SELECT madlib.clustered_variance_linregr();
    
  2. Run the linear regression function and view the results.
    DROP TABLE IF EXISTS tbl_output;
    SELECT madlib.clustered_variance_linregr( 'abalone', 
                                              'tbl_output', 
                                              'rings', 
                                              'ARRAY[1, diameter, length, width]', 
                                              'sex', 
                                              NULL
                                            );
    SELECT * FROM tbl_output;
    
  3. View online help for the clustered variance logistic regression function.
    SELECT madlib.clustered_variance_logregr();
    
  4. Run the logistic regression function and view the results.
    DROP TABLE IF EXISTS tbl_output;
    SELECT madlib.clustered_variance_logregr( 'abalone', 
                                              'tbl_output', 
                                              'rings < 10', 
                                              'ARRAY[1, diameter, length, width]', 
                                              'sex'
                                            );
    SELECT * FROM tbl_output;
    
  5. Run the multinomial logistic regression and view the results. DROP TABLE IF EXISTS tbl_output; SELECT madlib.clustered_variance_mlogregr( 'abalone', 'tbl_output', 'CASE WHEN rings < 10 THEN 1 ELSE 0 END', 'ARRAY[1, diameter, length, width]', 'sex', 0 ); SELECT * FROM tbl_output;

Notes
  • Note that we need to manually include an intercept term in the independent variable expression. The NULL value of groupingvar means that there is no grouping in the calculation.

Technical Background

Assume that the data can be separated into \(m\) clusters. Usually this can be done by grouping the data table according to one or multiple columns.

The estimator has a similar form to the usual sandwich estimator

\[ S(\vec{c}) = B(\vec{c}) M(\vec{c}) B(\vec{c}) \]

The bread part is the same as Huber-White sandwich estimator

\begin{eqnarray} B(\vec{c}) & = & \left(-\sum_{i=1}^{n} H(y_i, \vec{x}_i, \vec{c})\right)^{-1}\\ & = & \left(-\sum_{i=1}^{n}\frac{\partial^2 l(y_i, \vec{x}_i, \vec{c})}{\partial c_\alpha \partial c_\beta}\right)^{-1} \end{eqnarray}

where \(H\) is the hessian matrix, which is the second derivative of the target function

\[ L(\vec{c}) = \sum_{i=1}^n l(y_i, \vec{x}_i, \vec{c})\ . \]

The meat part is different

\[ M(\vec{c}) = \bf{A}^T\bf{A} \]

where the \(m\)-th row of \(\bf{A}\) is

\[ A_m = \sum_{i\in G_m}\frac{\partial l(y_i,\vec{x}_i,\vec{c})}{\partial \vec{c}} \]

where \(G_m\) is the set of rows that belong to the same cluster.

We can compute the quantities of \(B\) and \(A\) for each cluster during one scan through the data table in an aggregate function. Then sum over all clusters to the full \(B\) and \(A\) in the outside of the aggregate function. At last, the matrix mulplitications are done in a separate function on the master node.

When multinomial logistic regression is computed before the multinomial clustered variance calculation, it uses a default reference category of zero and the regression coefficients are included in the output table. The regression coefficients in the output are in the same order as multinomial logistic regression function, which is 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_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.

Literature

[1] Standard, Robust, and Clustered Standard Errors Computed in R, http://diffuseprior.wordpress.com/2012/06/15/standard-robust-and-clustered-standard-errors-computed-in-r/

Related Topics
File clustered_variance.sql_in documenting the SQL function