def test_combine_indices(): # Moved from grouputils __main__ section np.random.seed(985367) groups = np.random.randint(0, 2, size=(10, 2)) uv, ux, u, label = combine_indices(groups, return_labels=True) uv, ux, u, label = combine_indices(groups, prefix='g1,g2=', sep=',', return_labels=True) group0 = np.array(['sector0', 'sector1'])[groups[:, 0]] group1 = np.array(['region0', 'region1'])[groups[:, 1]] uv, ux, u, label = combine_indices((group0, group1), prefix='sector,region=', sep=',', return_labels=True) uv, ux, u, label = combine_indices((group0, group1), prefix='', sep='.', return_labels=True) group_joint = np.array(label)[uv] group_joint_expected = np.array([ 'sector1.region0', 'sector0.region1', 'sector0.region0', 'sector0.region1', 'sector1.region1', 'sector0.region0', 'sector1.region0', 'sector1.region0', 'sector0.region1', 'sector0.region0' ], dtype='|U15') assert_equal(group_joint, group_joint_expected)
def cov_cluster_2groups(results, group, group2=None, use_correction=True): '''cluster robust covariance matrix for two groups/clusters Parameters ---------- results : result instance result of a regression, uses results.model.exog and results.resid TODO: this should use wexog instead use_correction : bool If true (default), then the small sample correction factor is used. Returns ------- cov_both : ndarray, (k_vars, k_vars) cluster robust covariance matrix for parameter estimates, for both clusters cov_0 : ndarray, (k_vars, k_vars) cluster robust covariance matrix for parameter estimates for first cluster cov_1 : ndarray, (k_vars, k_vars) cluster robust covariance matrix for parameter estimates for second cluster Notes ----- verified against Peterson's table, (4 decimal print precision) ''' if group2 is None: if group.ndim != 2 or group.shape[1] != 2: raise ValueError( 'if group2 is not given, then groups needs to be ' + 'an array with two columns') group0 = group[:, 0] group1 = group[:, 1] else: group0 = group group1 = group2 group = (group0, group1) cov0 = cov_cluster(results, group0, use_correction=use_correction) #[0] because we get still also returns bse cov1 = cov_cluster(results, group1, use_correction=use_correction) # cov of cluster formed by intersection of two groups cov01 = cov_cluster(results, combine_indices(group)[0], use_correction=use_correction) #robust cov matrix for union of groups cov_both = cov0 + cov1 - cov01 #return all three (for now?) return cov_both, cov0, cov1
def cov_cluster_2groups(results, group, group2=None, use_correction=True): '''cluster robust covariance matrix for two groups/clusters Parameters ---------- results : result instance result of a regression, uses results.model.exog and results.resid TODO: this should use wexog instead use_correction : bool If true (default), then the small sample correction factor is used. Returns ------- cov_both : ndarray, (k_vars, k_vars) cluster robust covariance matrix for parameter estimates, for both clusters cov_0 : ndarray, (k_vars, k_vars) cluster robust covariance matrix for parameter estimates for first cluster cov_1 : ndarray, (k_vars, k_vars) cluster robust covariance matrix for parameter estimates for second cluster Notes ----- verified against Peterson's table, (4 decimal print precision) ''' if group2 is None: if group.ndim !=2 or group.shape[1] != 2: raise ValueError('if group2 is not given, then groups needs to be ' + 'an array with two columns') group0 = group[:, 0] group1 = group[:, 1] else: group0 = group group1 = group2 group = (group0, group1) cov0 = cov_cluster(results, group0, use_correction=use_correction) #[0] because we get still also returns bse cov1 = cov_cluster(results, group1, use_correction=use_correction) # cov of cluster formed by intersection of two groups cov01 = cov_cluster(results, combine_indices(group)[0], use_correction=use_correction) #robust cov matrix for union of groups cov_both = cov0 + cov1 - cov01 #return all three (for now?) return cov_both, cov0, cov1
def test_combine_indices(): # Moved from grouputils __main__ section np.random.seed(985367) groups = np.random.randint(0, 2, size=(10, 2)) uv, ux, u, label = combine_indices(groups, return_labels=True) uv, ux, u, label = combine_indices(groups, prefix='g1,g2=', sep=',', return_labels=True) group0 = np.array(['sector0', 'sector1'])[groups[:, 0]] group1 = np.array(['region0', 'region1'])[groups[:, 1]] uv, ux, u, label = combine_indices((group0, group1), prefix='sector,region=', sep=',', return_labels=True) uv, ux, u, label = combine_indices((group0, group1), prefix='', sep='.', return_labels=True) group_joint = np.array(label)[uv] group_joint_expected = np.array(['sector1.region0', 'sector0.region1', 'sector0.region0', 'sector0.region1', 'sector1.region1', 'sector0.region0', 'sector1.region0', 'sector1.region0', 'sector0.region1', 'sector0.region0'], dtype='|U15') assert_equal(group_joint, group_joint_expected)