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: 10
#> 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.8253205  -3.8212877 -3.9479184 -3.712970
#> 2  inequality -1.5380567  -1.5320841 -1.6694138 -1.424607
#> 3      income  3.8778405   3.8782527  3.7749549  4.007466
#> 4    ideology -8.2604687  -8.2590602 -8.3710152 -8.153842
#> 5       sigma  0.9694118   0.9732333  0.8299246  1.094337
#> 
#> --------------------------------
#> Coefficients for cluster 2 (cluster label 2)
#> 
#>                Post.Mean Post.Median  HPD.lower HPD.upper
#> 1 (Intercept) -3.8690336  -3.8697111 -3.9410974 -3.800133
#> 2  inequality  2.0034245   2.0035207  1.9323804  2.071562
#> 3      income  3.8486576   3.8493377  3.7795885  3.924084
#> 4    ideology -8.3072004  -8.3075016 -8.3801567 -8.235290
#> 5       sigma  0.9964865   0.9919586  0.9175808  1.080289
#> 
#> --------------------------------

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       2 -18.5649610  0.3392724  0.1425111  1.9225985
#> 2       2  -9.3905812 -0.9906646 -0.5117102  0.2483346
#> 3       2   0.9276234 -2.2318510 -0.3856288 -1.3619216
#> 4       2 -12.3594498 -3.0079501 -0.9440585 -0.2088675
#> 5       1  -2.4834411  0.1000455  0.8322192  0.1321378
#> 6       2 -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       2  -8.2396789 0.074437376  1.2020300  1.0874928
#> 1999       2 -23.1583753 0.434223018 -0.6176438  2.0387294
#> 2000       2  -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     3.6 1.140175      4    2    5
#> -------------------------------- 
#> 
#> 
#> 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.820354   -3.823624 -3.8858820 -3.7463185       1
#> 2   inequality -1.416097   -1.519672 -1.5986176 -0.2081428       1
#> 3       income  3.872660    3.871340  3.8075292  3.9586040       1
#> 4     ideology -8.261226   -8.256737 -8.3785696 -8.1688875       1
#> 5  (Intercept) -4.193188   -4.144393 -5.0208259 -3.4319159       3
#> 6   inequality  2.084990    2.110496  1.0356793  2.6574895       3
#> 7       income  2.701594    3.153616  0.3632189  4.1470902       3
#> 8     ideology -6.915076   -6.773444 -7.8668724 -6.1432562       3
#> 9  (Intercept) -3.869704   -3.850744 -4.2787600 -3.7338982       5
#> 10  inequality  1.987821    1.981703  1.8498567  2.0708541       5
#> 11      income  3.765141    3.828349  2.7729041  3.9342823       5
#> 12    ideology -8.097036   -8.283027 -8.3738885 -5.8251756       5
#> 13 (Intercept) -4.059788   -4.086112 -4.5468686 -3.2771709       8
#> 14  inequality  2.203680    2.185945  1.6485887  3.0417192       8
#> 15      income  4.123274    4.141528  3.5216825  4.8660704       8
#> 16    ideology -8.239081   -8.393415 -8.8213943 -5.0458091       8
#> 
#> --------------------------------
#> Coefficients and clusters for context 4
#> 
#>                  Post.Mean Post.Median  HPD.lower  HPD.upper Cluster
#> 1 (Intercept) -0.586289541 -0.89633773 -0.9874865  0.2537829       1
#> 2  inequality -1.318225184 -1.37522122 -1.6396783 -0.9920018       1
#> 3      income -2.229963893 -2.08438968 -2.8219701 -1.9585430       1
#> 4    ideology -0.054801738 -0.05611873 -0.3508337  0.2211642       1
#> 5 (Intercept)  0.118014709  0.14391590 -0.1308201  0.3343123       2
#> 6  inequality -1.039392628 -1.06393078 -1.2265200 -0.7570197       2
#> 7      income -2.541365886 -2.56215349 -2.7404327 -2.1574399       2
#> 8    ideology  0.004132025 -0.04244368 -0.2109076  0.5844064       2
#> 
#> --------------------------------
#> Coefficients and clusters for context 2
#> 
#>                 Post.Mean Post.Median  HPD.lower  HPD.upper Cluster
#> 1  (Intercept) -0.2146407  -0.1988552 -0.8055996  1.0868900       2
#> 2   inequality -1.2098796  -1.2169124 -1.4625468 -0.8739150       2
#> 3       income -0.5269897  -0.5311587 -0.8106611 -0.2502868       2
#> 4     ideology -2.5077576  -2.5184980 -3.1606227 -1.5809831       2
#> 5  (Intercept)  1.1862654   1.1483116  0.4465457  1.8068309       4
#> 6   inequality -0.1680770  -0.1037405 -0.7482682  0.2815992       4
#> 7       income  0.1209901   0.6191291 -2.2120228  0.9547377       4
#> 8     ideology  0.4942422   0.1659999 -0.2866668  2.1926666       4
#> 9  (Intercept) -0.4967490  -0.4431106 -1.0877614  0.0293697       6
#> 10  inequality -0.9801037  -1.1458425 -1.4887154 -0.1941581       6
#> 11      income -0.1598415  -0.1705052 -0.7547504  0.7871562       6
#> 12    ideology -2.0732962  -2.0628466 -2.4273686 -1.5725070       6
#> 13 (Intercept)  2.2276890   2.6708815 -1.2437235  3.2702125       7
#> 14  inequality  0.5291045   0.4653838 -2.0249304  1.9606579       7
#> 15      income  0.7588775   0.6753471 -0.2067942  2.6759615       7
#> 16    ideology -2.5103023  -2.3020300 -5.7748165 -0.7921070       7
#> 17 (Intercept)  1.4725646   1.1653999 -0.8041077  4.3565400       8
#> 18  inequality  0.7353571   0.9320817 -0.5984169  1.2450616       8
#> 19      income  0.8619173   0.9177917  0.1746398  1.1169308       8
#> 20    ideology -1.9303452  -1.8143124 -3.2573827  2.7308555       8
#> 
#> --------------------------------
#> Coefficients and clusters for context 3
#> 
#>                 Post.Mean Post.Median  HPD.lower   HPD.upper Cluster
#> 1  (Intercept) -0.2069074  0.11903547 -5.4681335  3.09143191       2
#> 2   inequality -1.7160834 -2.37846047 -3.7684090  6.92268375       2
#> 3       income -3.1149666 -3.35333100 -7.1480648  0.87914843       2
#> 4     ideology  0.3402481  0.66685070 -5.8590543  4.51344011       2
#> 5  (Intercept) -0.1195990 -0.09539944 -0.3917075  0.08962169       3
#> 6   inequality  0.4701923  0.45220317  0.1595455  0.78286514       3
#> 7       income -2.9602481 -3.02940039 -3.2740807 -1.92759779       3
#> 8     ideology  2.0813725  2.08424895  1.7226196  2.34532331       3
#> 9  (Intercept) -0.4131959 -0.32867682 -1.2980527 -0.08205546       4
#> 10  inequality -0.6981612 -0.64945564 -1.2673002 -0.42393818       4
#> 11      income -4.3514140 -4.35055457 -4.5132661 -4.13132679       4
#> 12    ideology  0.5486466  0.49232731  0.2749558  1.39017651       4
#> 13 (Intercept) -0.7060134 -0.72691422 -1.2911303 -0.15341660       7
#> 14  inequality  0.1671256  0.22476335 -2.1362418  1.80126096       7
#> 15      income -3.7928273 -3.84187357 -4.6974788 -3.08393154       7
#> 16    ideology  0.8901621  0.94537173  0.2888574  1.66466753       7
#> 
#> --------------------------------
#> Coefficients and clusters for context 5
#> 
#>                   Post.Mean Post.Median  HPD.lower   HPD.upper Cluster
#> 1  (Intercept)  1.557367943  1.62249215  0.8195209  2.24267109       3
#> 2   inequality -3.243023324 -3.23250010 -3.7260959 -2.71602955       3
#> 3       income -0.650102472 -0.70561836 -1.1565273  0.09137604       3
#> 4     ideology -0.950632471 -1.03251370 -2.0668614  0.19624451       3
#> 5  (Intercept)  0.007373084 -0.01096501 -0.5497075  0.32028610       4
#> 6   inequality -2.750121991 -2.80528143 -3.0997694 -1.90050364       4
#> 7       income  0.001389123  0.01987331 -0.3811467  0.37846679       4
#> 8     ideology -2.068638274 -2.23338776 -2.3814212 -0.56140719       4
#> 9  (Intercept)  0.091549832 -0.06534395 -0.3272785  2.07256565       7
#> 10  inequality  1.078420468  0.93442422  0.7195064  2.63182050       7
#> 11      income -0.370507410 -0.22946310 -1.5373890  0.07033499       7
#> 12    ideology -1.765934027 -1.91561655 -2.1567634 -0.37360555       7
#> 
#> --------------------------------
#> Context-level coefficients:
#>                Description  Post.Mean HPD.lower HPD.upper
#> 1     Intercept of beta[0] -0.2028091 -3.113130  2.192975
#> 2     Intercept of beta[1] -0.4221686 -2.552358  2.848865
#> 3     Intercept of beta[2] -0.1377849 -2.291153  3.075177
#> 4     Intercept of beta[3] -1.2382128 -3.710844  1.613151
#> 5 Effect of gap on beta[0] -0.1914918 -1.927734  1.671910
#> 6 Effect of gap on beta[1]  0.1724977 -2.778039  2.486732
#> 7 Effect of gap on beta[2] -0.6287755 -2.642006  1.651181
#> 8 Effect of gap on beta[3]  0.6659892 -1.599831  2.554767
#> 
#> --------------------------------

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.