hdpGLM

Introduction

The package hdpGLM makes it easy to estimate semi-parametric regression models, and summarize and visualize the results. The package is useful for many purposes:

  1. Find clusters using semi-parametric Bayesian methods (Dirichlet Process)
  2. Find a set of generalized linear models (GLM) that apply to different latent subpopulations
  3. Estimate latent heterogeneity in the marginal effect of regression coefficients
  4. Investigate if Simpson’s Paradox occurs due to latent or omitted features
  5. Cluster the data points based on marginal effect heterogeneity
  6. All the above, but with hierarchical data from many contexts (e.g., schools, hospitals, cities, states, countries, etc.)
  7. Find the effect of context-level features (upper-level covariates) on the latent heterogeneity of within-context features (lower-level covariates)

Background

The hdpGLM works similarly to linear regression models. It estimates coefficients of linear regressions, including generalized linear models, such as logit coefficients. But it simultaneously searches for latent clusters in the data and estimates the linear coefficients for those clusters. The result of the estimation is K vectors of linear coefficients, one vector for each cluster. If there are no clusters in the data, it returns the estimated coefficients similar to classical regression models.

The clustering procedure is based on a hierarchical semi-parametric Bayesian model proposed in Ferrari (2020). The model, called Hierarchical Dirichlet process Generalized Linear Models (hdpGLM), can be used to deal with latent heterogeneity in different situations, including those that emerge due to unobserved variables. It deals with the latent heterogeneity in two ways: (1) it finds latent clusters which can be better described by different regression models and (2) estimate the coefficient of those models. The hdpGLM can also be used with hierarchical data to estimate latent heterogeneity in multiple contexts and check if the clusters are context-dependent (see an example in section Estimating Context-dependent latent heterogeneity).

The linear model is estimated by sampling from the posterior distribution using a Gibbs sampler. Non-linear models (e.g., logit with binary outcomes) use Hamiltonian Monte Carlo within Gibbs. The algorithms are presented in Ferrari (2020).

Why should we estimate clusters of linear regressions instead of fitting a single regression model?

First, it improves the predictive performance of the regression model and keeps the interpretability of the regression coefficients. hdpGLM is much more flexible than traditional regression and produces monotonically lower root mean square error (see Ferrari (2020) for details).

Second, latent heterogeneity emerges when there are omitted variables in the estimation of regression models. The hdpGLM can be used to estimate marginal effects even when interactions were omitted. It recovers the linear coefficients of each latent group.

Usage

Estimation

The function hdpGLM estimates a semi-parametric Bayesian regression model. The syntax is similar to other R functions such as lm(), glm(), and lmer().

Here is a toy example. Suppose we are studying how income inequality affects support policies that help alleviate poverty in a given country A. Yet, suppose further that (1) the effect of inequality varies between groups of people; for some people, inequality increases support for welfare policies, but for others, it decreases welfare policy support; (2) we don’t know which individual belongs to which group. The data set welfare contains simulated data for this example.

## loading and looking at the data
welfare = read.csv2('welfare.csv')
head(welfare)
#>       support inequality     income   ideology
#> 1 -18.5649610  0.3392724  0.1425111  1.9225985
#> 2  -9.3905812 -0.9906646 -0.5117102  0.2483346
#> 3   0.9276234 -2.2318510 -0.3856288 -1.3619216
#> 4 -12.3594498 -3.0079501 -0.9440585 -0.2088675
#> 5  -2.4834411  0.1000455  0.8322192  0.1321378
#> 6 -11.4187853 -0.9543883 -0.8810503  0.2916444

Now, suppose that inequality increases support for welfare only among women, but it decreases support among men. We didn’t collect data on gender (male versus female). We could estimate the hdpGLM and recover the coefficients even if gender wasn’t observed. The package provides a function called hdpGLM, which estimates a semi-parametric Bayesian generalized linear model using a Dirichlet mixture. Let’s estimate the model. The example uses few iterations in the MCMC, but in real applications, one should use a much larger number.

library(hdpGLM)
#> 
#> ## ===============================================================
#> ## Hierarchial Dirichlet Process Generalized Linear Model (hdpGLM)
#> ## ===============================================================
#> 
#> Author: Diogo Ferrari
#> Usage : https://github.com/DiogoFerrari/hdpGLM
#> 
#> Attaching package: 'hdpGLM'
#> The following object is masked _by_ '.GlobalEnv':
#> 
#>     welfare
## estimating the model
mcmc = list(burn.in=10, ## MCMC burn-in period
            n.iter =500) ## number of MCMC iterations to keep
mod = hdpGLM(support ~ inequality + income + ideology, data=welfare,
             mcmc=mcmc)
## printing the outcome
summary(mod)
#>  
#> -------------------------------- 
#> dpGLM model object
#> 
#> Maximum number of clusters activated during the estimation: 17
#> Number of MCMC iterations: 500
#> burn-in: 10
#> -------------------------------- 
#> 
#> Summary statistics of clusters with data points
#> 
#> --------------------------------
#> Coefficients for cluster 1 (cluster label 1)
#> 
#>                Post.Mean Post.Median  HPD.lower HPD.upper
#> 1 (Intercept) -3.8267494  -3.8192840 -3.8906974 -3.740610
#> 2  inequality -1.5217421  -1.5305326 -1.6143204 -1.459832
#> 3      income  3.8805647   3.8803983  3.8066282  3.947550
#> 4    ideology -8.2547120  -8.2544791 -8.3263540 -8.176281
#> 5       sigma  0.9684311   0.9706939  0.8601578  1.094367
#> 
#> --------------------------------
#> Coefficients for cluster 2 (cluster label 15)
#> 
#>                Post.Mean Post.Median HPD.lower HPD.upper
#> 1 (Intercept) -3.8439477  -3.8553236 -3.995710 -3.623951
#> 2  inequality  2.0089940   2.0112313  1.862732  2.144639
#> 3      income  3.8686643   3.8569780  3.719395  4.032840
#> 4    ideology -8.2925124  -8.3056180 -8.425597 -8.159880
#> 5       sigma  0.9994702   0.9962431  0.896100  1.109040
#> 
#> --------------------------------

The summary function prints the result in a tidy format. The column k in the summary shows the label of the estimated clusters. The column Mean is the average of the posterior distribution for each linear coefficient in each cluster.

The function classify can be used to classify the data points into clusters based on the estimation.

welfare_clustered = classify(welfare, mod)
head(welfare_clustered)
#>   Cluster     support inequality     income   ideology
#> 1      15 -18.5649610  0.3392724  0.1425111  1.9225985
#> 2      15  -9.3905812 -0.9906646 -0.5117102  0.2483346
#> 3      15   0.9276234 -2.2318510 -0.3856288 -1.3619216
#> 4      15 -12.3594498 -3.0079501 -0.9440585 -0.2088675
#> 5       1  -2.4834411  0.1000455  0.8322192  0.1321378
#> 6      15 -11.4187853 -0.9543883 -0.8810503  0.2916444
tail(welfare_clustered)
#>      Cluster     support  inequality     income   ideology
#> 1995       1  -1.5230053 1.055855140 -0.7295937 -0.7067871
#> 1996       1   0.4814892 0.582588091  2.0051082  0.3090389
#> 1997       1 -14.1929956 0.391164197 -0.9607449  0.7765482
#> 1998       1  -8.2396789 0.074437376  1.2020300  1.0874928
#> 1999       1 -23.1583753 0.434223018 -0.6176438  2.0387294
#> 2000       1  -7.2075582 0.008355317 -0.4538951  0.2268072

There are a series of built-in functions, with various options, to plot the results. In the example below, you see two of those options. The separate parameter plot the posterior samples for each cluster separately, and the option ncols controls how many columns to use for the panels in the figure (to see more, run help(plot.hdpGLM) and help(plot.dpGLM)).

plot(mod, separate=T, ncols=4)
#> 
#> 
#> Generating plot...

Estimating Context-dependent Latent Heterogeneity

To continue the previous toy example, suppose that we are analyzing data from many countries, and we suspect that the latent heterogeneity is different in each country. The effect of inequality on support for welfare may be gender-specific only in some countries (contexts). Or maybe the way it is gender-specific varies from country to country. Suppose we didn’t have data on gender, but we collect information on countries’ gender gap in welfare provision. Let’s look at this new data set.

## loading and looking at the data
welfare = read.csv2('welfare2.csv')
head(welfare)
#>       support inequality     income   ideology country gap
#> 1 -18.5649610  0.3392724  0.1425111  1.9225985       0 0.1
#> 2  -9.3905812 -0.9906646 -0.5117102  0.2483346       0 0.1
#> 3   0.9276234 -2.2318510 -0.3856288 -1.3619216       0 0.1
#> 4 -12.3594498 -3.0079501 -0.9440585 -0.2088675       0 0.1
#> 5  -2.4834411  0.1000455  0.8322192  0.1321378       0 0.1
#> 6 -11.4187853 -0.9543883 -0.8810503  0.2916444       0 0.1
tail(welfare)
#>         support inequality     income    ideology country        gap
#> 3195  0.3190583 -0.7504798 -0.7839583  0.92300705       4 -0.8280808
#> 3196 -1.3837239  0.6620435 -1.5566268  0.05634618       4 -0.8280808
#> 3197 -1.3820016 -0.4298706 -1.0945688  0.71559078       4 -0.8280808
#> 3198  0.6878775  0.5450604  2.6175887 -1.94844469       4 -0.8280808
#> 3199 -7.9282930  1.7846004  1.6755823  1.29160208       4 -0.8280808
#> 3200 -1.7472485  0.5030992 -0.5395479  0.20109879       4 -0.8280808

The variable country indicates the country (context) of the observation, and the variable gap the gender gap in welfare provision in the respective country. The estimation is similar to the previous example, but now there is a second formula for the context-level variables. Again, the example below uses few iterations in the MCMC, but in practical applications, one needs to increase that).

## estimating the model
mcmc = list(burn.in=1, ## MCMC burn-in period
            n.iter =50) ## number of MCMC iterations to keep
mod = hdpGLM(support ~ inequality + income + ideology, 
             support ~ gap,
         data=welfare, mcmc=mcmc)
summary(mod)
#>  
#> -------------------------------- 
#> hdpGLM Object 
#> 
#> Maximum number of clusters activated during the estimation: 1
#> Number of MCMC iterations: 50
#> Burn-in: 1
#> 
#> Number of contexts : 5
#> 
#> Number of clusters (summary across contexts): 
#> 
#>   Average Std.Dev Median Min. Max.
#> 1     5.2 1.30384      5    4    7
#> -------------------------------- 
#> 
#> 
#> Summary statistics of clusters with data points in each context
#> 
#> --------------------------------
#> Coefficients and clusters for context 1
#> 
#>                 Post.Mean Post.Median  HPD.lower  HPD.upper Cluster
#> 1  (Intercept) -3.8841575   -3.879188 -3.9902821 -3.7880063       1
#> 2   inequality  1.8058294    1.983845  0.2079764  2.1325004       1
#> 3       income  3.8574880    3.854883  3.7856183  3.9420201       1
#> 4     ideology -8.3361642   -8.321998 -8.4967581 -8.2203243       1
#> 5  (Intercept) -3.5991163   -3.719730 -3.8246016 -2.1982179       6
#> 6   inequality -1.5639314   -1.569052 -1.7399255 -1.2252465       6
#> 7       income  3.7731219    3.838517  2.8133599  4.1750102       6
#> 8     ideology -8.1560634   -8.275696 -8.4520919 -6.5941962       6
#> 9  (Intercept) -3.0125207   -3.464573 -4.2767353 -1.1473754      10
#> 10  inequality -0.7558041   -1.183349 -1.4910115  0.9288202      10
#> 11      income  3.6775047    3.766372  2.9699235  3.9771585      10
#> 12    ideology -8.3063336   -8.280738 -8.8428573 -8.0072417      10
#> 13 (Intercept) -3.9488450   -4.004067 -4.3765174 -2.9314164      14
#> 14  inequality -1.8271488   -1.677325 -3.8154916 -1.4416672      14
#> 15      income  4.0810206    4.013971  3.7566780  5.4560701      14
#> 16    ideology -8.0800905   -8.232592 -8.5663449 -6.3118063      14
#> 
#> --------------------------------
#> Coefficients and clusters for context 2
#> 
#>                  Post.Mean Post.Median  HPD.lower    HPD.upper Cluster
#> 1  (Intercept)  0.80804562   0.8100729 -2.8103754  2.591036103       1
#> 2   inequality -0.52237977   0.3817195 -6.4382050  4.533437127       1
#> 3       income  1.45463243   2.0064825 -2.9249629  3.321467855       1
#> 4     ideology -0.93452135  -1.6654477 -4.2025127  2.503816155       1
#> 5  (Intercept)  0.50952335   0.2828967 -0.0479151  4.423020258       2
#> 6   inequality  0.44860561   0.5115109 -2.3006784  0.993777075       2
#> 7       income  1.09693695   1.2265367 -1.2445896  1.880596639       2
#> 8     ideology -1.34274110  -1.0644957 -7.1919967 -0.177167238       2
#> 9  (Intercept) -0.25221578  -0.2513427 -0.4170621 -0.009104313       3
#> 10  inequality -1.27454179  -1.2612260 -1.3975678 -0.970366949       3
#> 11      income -0.57813108  -0.5479061 -0.7588544 -0.399355307       3
#> 12    ideology -2.35320525  -2.3515798 -2.4946324 -2.149321931       3
#> 13 (Intercept)  0.39586192   0.4429418 -0.3769101  0.812090986       4
#> 14  inequality  1.59929221   1.4700802  1.1814407  3.218480072       4
#> 15      income  1.04631125   0.9663337  0.6210959  2.392055791       4
#> 16    ideology -2.49795172  -2.3568233 -4.1602833 -2.164991799       4
#> 17 (Intercept)  1.26627202   1.4283255 -1.6658262  1.696750389       5
#> 18  inequality  0.89829469   0.8900292  0.6479782  1.731379388       5
#> 19      income  0.83263654   0.8123727  0.3530394  1.795182450       5
#> 20    ideology -1.84174593  -1.6685808 -6.6278217 -0.702463064       5
#> 21 (Intercept)  3.01357586   2.9167497  2.5568696  4.499055261       6
#> 22  inequality  0.06491799   0.1477560 -1.6113203  0.584923358       6
#> 23      income  0.76833457   0.8716944  0.1438159  1.419711550       6
#> 24    ideology -2.33058102  -2.3171881 -2.8828469 -1.753159147       6
#> 25 (Intercept)  1.01552606   0.7725410 -1.2392453  3.349512236       7
#> 26  inequality -0.29477214  -0.9949605 -2.4131703  6.287260359       7
#> 27      income -0.17686132  -0.3648183 -3.5742683  3.827485478       7
#> 28    ideology -1.00291236  -1.0431485 -4.4905801  1.891418041       7
#> 
#> --------------------------------
#> Coefficients and clusters for context 5
#> 
#>                  Post.Mean Post.Median   HPD.lower   HPD.upper Cluster
#> 1  (Intercept) -1.26604540 -1.36483812 -1.73680858 -0.10512099       1
#> 2   inequality -0.01454340 -0.07618411 -0.85200304  1.63287376       1
#> 3       income -2.32677320 -2.70277790 -3.07027144  0.30186781       1
#> 4     ideology -2.73255802 -2.79988730 -5.35041445 -2.13030846       1
#> 5  (Intercept)  0.11797355  0.06184990 -0.21333366  1.06078343       2
#> 6   inequality  0.58583930  0.71429145 -0.44963670  1.00514365       2
#> 7       income -0.44917300 -0.45819732 -1.48621793  0.03948922       2
#> 8     ideology -2.46380268 -2.39793297 -3.75514845 -2.06849732       2
#> 9  (Intercept)  0.71930250  0.76505762 -1.92280320  3.26892921       3
#> 10  inequality  1.15265319  1.22402716  0.21053161  2.05334026       3
#> 11      income  1.81185843  1.43886346  0.56760726  5.42062042       3
#> 12    ideology -2.27915343 -2.26930891 -3.19957103 -0.63650023       3
#> 13 (Intercept)  0.55447919  0.48513922  0.12094747  1.07314944       4
#> 14  inequality  1.28625956  1.47659055 -0.89961225  1.86332340       4
#> 15      income  0.22541977 -0.12697221 -0.67189315  3.91149539       4
#> 16    ideology -0.91672868 -1.09095570 -1.37551901  1.63463436       4
#> 17 (Intercept)  0.28722880  0.30370678 -0.03818824  0.49374569       5
#> 18  inequality -2.58784570 -2.65695500 -2.87965157 -1.76428233       5
#> 19      income  0.05578646 -0.05409568 -0.26150459  1.31024619       5
#> 20    ideology -2.38120221 -2.45654350 -2.71030877 -1.80080474       5
#> 21 (Intercept) -1.70104047 -1.72678667 -2.17798018 -1.27872060       8
#> 22  inequality -0.68776725 -0.67848525 -1.40065443 -0.25930916       8
#> 23      income -0.13872365 -0.08846898 -1.49985993  0.44496304       8
#> 24    ideology -1.36746404 -1.40521922 -1.93332685 -0.80473526       8
#> 
#> --------------------------------
#> Coefficients and clusters for context 3
#> 
#>                  Post.Mean Post.Median  HPD.lower   HPD.upper Cluster
#> 1  (Intercept) -0.10314862 -0.09924156 -0.3184211  0.18145561       2
#> 2   inequality  0.64300276  0.60504167  0.3110996  0.83886805       2
#> 3       income -2.94705787 -2.88762145 -3.3894483 -2.58374803       2
#> 4     ideology  2.47547917  2.45831842  2.1642578  2.66734849       2
#> 5  (Intercept)  0.11162275  0.12516330 -0.2224866  0.48270864       3
#> 6   inequality -0.48451067 -0.57069412 -0.7194890  0.01501887       3
#> 7       income -4.03411119 -4.09771359 -4.4633897 -3.22234242       3
#> 8     ideology  0.61561982  0.64810943  0.2780929  0.83291029       3
#> 9  (Intercept) -1.36558499 -1.17965469 -4.1818829 -0.35383704       4
#> 10  inequality -0.08181583 -0.26923349 -0.8606431  2.22487046       4
#> 11      income -3.69831828 -3.75840197 -3.9611267 -3.17335672       4
#> 12    ideology  0.76617862  0.68177590  0.5125095  1.67824735       4
#> 13 (Intercept)  0.75329692  0.82922589 -1.7461138  1.16267859       8
#> 14  inequality -0.95087909 -0.92077438 -1.4383107 -0.49386574       8
#> 15      income -2.89589073 -2.99452685 -3.5661296 -2.47323829       8
#> 16    ideology  1.21813613  1.35397063  0.1364900  1.83155481       8
#> 17 (Intercept) -0.10985366 -0.05218560 -2.1367333  2.43823121      11
#> 18  inequality  0.70198749  1.04774394 -1.4410098  2.46692002      11
#> 19      income -3.68925045 -3.79846314 -6.8471672 -0.38079912      11
#> 20    ideology  1.16052528  1.21083714 -0.8308626  4.58753135      11
#> 
#> --------------------------------
#> Coefficients and clusters for context 4
#> 
#>                  Post.Mean Post.Median  HPD.lower  HPD.upper Cluster
#> 1  (Intercept) -0.10064164 -0.11187339 -0.4403255  0.4748144       2
#> 2   inequality -0.88468291 -0.92690202 -1.2118796 -0.3427767       2
#> 3       income -2.45271017 -2.31552588 -3.7939021 -1.9537443       2
#> 4     ideology  0.40278274  0.38751289  0.1042487  0.8831231       2
#> 5  (Intercept)  1.24016478  1.28032661 -0.6645880  3.1225240       4
#> 6   inequality -1.10897400 -1.10864321 -4.4500717 -0.5903909       4
#> 7       income -1.90095619 -2.32510479 -3.1666118  2.2410497       4
#> 8     ideology  0.35219536  0.32614568 -1.6109105  2.3166473       4
#> 9  (Intercept) -0.08192269 -0.12481767 -0.4631251  0.4916621       5
#> 10  inequality -1.52958330 -1.54995771 -1.8575317 -1.1338077       5
#> 11      income -2.47824498 -2.65598129 -3.3176944 -0.9713846       5
#> 12    ideology  0.10990656  0.09929389 -0.8088110  0.6510149       5
#> 13 (Intercept) -0.03189533  0.12797553 -0.6659895  0.3340582       8
#> 14  inequality -1.03897461 -1.07510746 -1.3647869 -0.2876543       8
#> 15      income -2.62772505 -2.67228755 -2.9211040 -1.6050985       8
#> 16    ideology -0.47869260 -0.51620698 -0.8173754 -0.1331302       8
#> 
#> --------------------------------
#> Context-level coefficients:
#>                Description   Post.Mean HPD.lower HPD.upper
#> 1     Intercept of beta[0] -0.23000613 -2.138938  2.431786
#> 2     Intercept of beta[1]  0.28580157 -1.453169  2.793789
#> 3     Intercept of beta[2] -0.11422402 -2.316071  2.348582
#> 4     Intercept of beta[3] -1.02705493 -3.630482  1.418515
#> 5 Effect of gap on beta[0] -0.21025617 -2.301029  1.728968
#> 6 Effect of gap on beta[1] -0.03102791 -1.658788  2.032760
#> 7 Effect of gap on beta[2] -0.65271845 -2.332343  1.453613
#> 8 Effect of gap on beta[3]  0.39517038 -1.874573  2.523599
#> 
#> --------------------------------

The summary contains more information now. As before, the column k indicates the estimated clusters. The column j indicates the country (context) of the estimated value for the respective cluster’s coefficient. The second summary ($tau) shows the marginal effect of the context-level feature (gap). Details of the interpretation can be found in Ferrari (2020).

There are a series of built-in functions to visualize the output. The function plot_tau() displays the estimation of the effect of the context-level variables.

plot_tau(mod)

The function plot_pexp_beta() displays the association between the context-level features and the latent heterogeneity in the effect of the linear coefficients in each context. The paramter ‘smooth.line’ plots a line representing the linear association between the context-level feature (gap) and the posterior averages of the marginal effects in each cluster. The parameter ncol.beta controls the number of columns in the figure for the panels. For more options, see help(plot_pexp_beta)

plot_pexp_beta(mod, smooth.line=TRUE, ncol.beta=2)
#> 
#> 
#> Generating plots ...
#> Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
#> of ggplot2 3.3.4.
#> ℹ The deprecated feature was likely used in the hdpGLM package.
#>   Please report the issue at <https://github.com/DiogoFerrari/hdpGLM/issues>.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'

Reference

Ferrari, Diogo. 2020. “Modeling Context-Dependent Latent Effect Heterogeneity.” Political Analysis 28 (1): 20–46.