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:
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.
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
read.csv2('welfare.csv')
welfare =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 : http://www.diogoferrari.com/hdpGLM/index.html
#>
#> Attaching package: 'hdpGLM'
#> The following object is masked _by_ '.GlobalEnv':
#>
#> welfare
## estimating the model
list(burn.in=10, ## MCMC burn-in period
mcmc =n.iter =500) ## number of MCMC iterations to keep
hdpGLM(support ~ inequality + income + ideology, data=welfare,
mod =mcmc=mcmc)
## printing the outcome
summary(mod)
#>
#> --------------------------------
#> dpGLM model object
#>
#> Maximum number of clusters activated during the estimation: 12
#> Numer 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.8766549 -3.8762561 -3.954394 -3.807241
#> 2 inequality 2.0009303 2.0001177 1.930459 2.070054
#> 3 income 3.8500937 3.8515191 3.776237 3.913414
#> 4 ideology -8.3079045 -8.3067380 -8.386052 -8.234707
#> 5 sigma 0.9994476 0.9974916 0.911302 1.081114
#>
#> --------------------------------
#> Coefficients for cluster 2 (cluster label 9)
#>
#> Post.Mean Post.Median HPD.lower HPD.upper
#> 1 (Intercept) -3.8178022 -3.8164733 -3.8891423 -3.752033
#> 2 inequality -1.5299334 -1.5310725 -1.5964951 -1.444499
#> 3 income 3.8762521 3.8762120 3.8076461 3.947288
#> 4 ideology -8.2607123 -8.2631734 -8.3256751 -8.182940
#> 5 sigma 0.9569949 0.9631322 0.8356505 1.076317
#>
#> --------------------------------
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.
classify(welfare, mod)
welfare_clustered =head(welfare_clustered)
#> Cluster support inequality income ideology
#> 1 1 -18.5649610 0.3392724 0.1425111 1.9225985
#> 2 1 -9.3905812 -0.9906646 -0.5117102 0.2483346
#> 3 1 0.9276234 -2.2318510 -0.3856288 -1.3619216
#> 4 1 -12.3594498 -3.0079501 -0.9440585 -0.2088675
#> 5 9 -2.4834411 0.1000455 0.8322192 0.1321378
#> 6 1 -11.4187853 -0.9543883 -0.8810503 0.2916444
tail(welfare_clustered)
#> Cluster support inequality income ideology
#> 1995 9 -1.5230053 1.055855140 -0.7295937 -0.7067871
#> 1996 9 0.4814892 0.582588091 2.0051082 0.3090389
#> 1997 9 -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...
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
read.csv2('welfare2.csv')
welfare =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
list(burn.in=1, ## MCMC burn-in period
mcmc =n.iter =50) ## number of MCMC iterations to keep
hdpGLM(support ~ inequality + income + ideology,
mod =~ gap,
support data=welfare, mcmc=mcmc)
summary(mod)
#>
#> --------------------------------
#> hdpGLM Object
#>
#> Maximum number of clusters activated during the estimation: 1
#> Numer 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.4 1.140175 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.887406 -3.889733 -3.9524584 -3.8294832 1
#> 2 inequality 1.811406 1.991428 0.5342274 2.0848752 1
#> 3 income 3.851390 3.842039 3.7579345 3.9456482 1
#> 4 ideology -8.297937 -8.295970 -8.3512394 -8.2304261 1
#> 5 (Intercept) -2.386234 -2.496836 -2.9619195 -0.9711331 2
#> 6 inequality -1.129147 -1.098115 -1.5867003 -0.7941822 2
#> 7 income 2.937843 3.115669 1.2963659 3.4812127 2
#> 8 ideology -7.007027 -7.471715 -8.1668285 -3.6964808 2
#> 9 (Intercept) -3.309177 -3.253068 -3.8551265 -2.4912131 4
#> 10 inequality -2.062393 -1.945819 -3.5074560 -1.6456891 4
#> 11 income 3.462499 3.541612 2.9112611 3.8883838 4
#> 12 ideology -7.419628 -7.493822 -8.5389665 -4.7686982 4
#> 13 (Intercept) -3.745419 -3.825127 -4.1148460 -2.3955851 8
#> 14 inequality -1.764315 -1.556746 -5.8155569 -0.9941412 8
#> 15 income 3.774992 3.791904 3.5882901 4.0074574 8
#> 16 ideology -7.855510 -8.300597 -8.6447032 -2.1693618 8
#> 17 (Intercept) -3.988740 -3.905868 -6.8486857 -3.7834531 11
#> 18 inequality -1.815823 -1.526820 -4.3820407 -1.3782321 11
#> 19 income 3.650944 3.916298 0.6564623 4.0444630 11
#> 20 ideology -8.282682 -8.257330 -9.0463051 -8.1082943 11
#>
#> --------------------------------
#> Coefficients and clusters for context 2
#>
#> Post.Mean Post.Median HPD.lower HPD.upper Cluster
#> 1 (Intercept) 0.35729053 0.29902368 -3.7564036 2.67549032 1
#> 2 inequality 0.33079511 -0.46965659 -2.3663870 4.71806449 1
#> 3 income -0.29399859 -0.40682684 -4.2875518 2.69502075 1
#> 4 ideology -2.75370133 -1.91325857 -11.0783772 -1.00085833 1
#> 5 (Intercept) 0.69522932 0.69492584 -0.1710911 1.19775092 2
#> 6 inequality 0.04884859 0.10617609 -1.0841676 0.57704308 2
#> 7 income 1.00828218 0.87356357 0.5021207 2.68180514 2
#> 8 ideology -0.29352492 -0.24631990 -0.9078077 0.39498737 2
#> 9 (Intercept) 0.30728697 1.00164077 -4.4135507 1.32197864 3
#> 10 inequality 0.75251323 1.16975950 -1.2019897 1.66849726 3
#> 11 income 1.01520519 1.17487671 -0.3625672 1.57285365 3
#> 12 ideology -1.79222274 -2.02754040 -2.6891285 0.79849198 3
#> 13 (Intercept) -0.16466110 -0.31014043 -0.4957907 1.35428162 5
#> 14 inequality -1.14229914 -1.08809816 -1.9480098 -0.87230262 5
#> 15 income -0.41614284 -0.45364701 -0.5956526 -0.01610671 5
#> 16 ideology -2.09577812 -2.22734759 -2.4700199 -0.89977780 5
#> 17 (Intercept) -1.21237395 -1.42422466 -1.8454540 0.05232803 6
#> 18 inequality -0.84125798 -0.70068081 -1.8408971 0.02486480 6
#> 19 income 0.38318551 0.46881459 -0.5679960 0.94454263 6
#> 20 ideology -2.24995587 -2.05515481 -3.2587563 -1.11545566 6
#> 21 (Intercept) 0.03848019 0.05917049 -0.8565010 0.78123500 7
#> 22 inequality -1.31643596 -1.60775467 -1.9191311 -0.17175292 7
#> 23 income -1.15166452 -1.00484236 -2.0649472 -0.36659268 7
#> 24 ideology -2.51160210 -2.25818051 -5.3609523 -1.39851939 7
#> 25 (Intercept) 1.62515173 1.48677578 0.9543978 2.31653855 8
#> 26 inequality 0.91291094 0.87431004 -0.2845926 2.02316455 8
#> 27 income 0.88735633 0.76154908 0.6023874 3.32835526 8
#> 28 ideology -1.88065596 -1.73276600 -2.7690109 -1.34679730 8
#>
#> --------------------------------
#> Coefficients and clusters for context 3
#>
#> Post.Mean Post.Median HPD.lower HPD.upper Cluster
#> 1 (Intercept) -1.2152615 -0.15813766 -8.16845118 6.0639439 1
#> 2 inequality 0.4009729 -0.12211616 -6.22833613 6.8809617 1
#> 3 income -2.1101485 -3.60898924 -8.12267832 5.0368713 1
#> 4 ideology 0.5171932 1.03150143 -4.34054568 6.8195244 1
#> 5 (Intercept) 0.7426601 0.63544880 0.10943945 2.6624223 3
#> 6 inequality 0.2994772 0.21013724 -0.51096100 2.3687113 3
#> 7 income -2.8671288 -2.80811039 -4.71907413 -1.9721455 3
#> 8 ideology 2.0815647 1.86699035 1.30163440 5.0107179 3
#> 9 (Intercept) -1.5583823 -1.33793563 -3.80206773 0.9432470 4
#> 10 inequality 0.7775064 0.63961105 0.06173066 3.0042640 4
#> 11 income -3.4592646 -3.11666769 -6.04879115 -2.4516255 4
#> 12 ideology 1.7897732 1.49166992 -0.09373731 3.9062594 4
#> 13 (Intercept) -0.2710966 -0.24971149 -0.44817785 0.8823788 6
#> 14 inequality -0.4454703 -0.62994923 -0.81107629 -0.4066958 6
#> 15 income -4.2916029 -4.33192099 -4.50710182 -3.9514987 6
#> 16 ideology 0.7046987 0.49278160 -0.18007676 0.6898407 6
#> 17 (Intercept) -0.1447786 0.03751696 -1.46087526 0.8085698 7
#> 18 inequality 0.6095130 0.55713631 0.04751095 1.7548533 7
#> 19 income -3.1735905 -3.17719234 -3.49736036 -2.8668752 7
#> 20 ideology 2.1316667 2.13630406 1.68295302 2.7926486 7
#> 21 (Intercept) -0.2934536 -0.09825841 -1.94534830 1.1473553 9
#> 22 inequality -0.5482645 -0.64255207 -1.80347212 0.4134749 9
#> 23 income -3.4154486 -3.49933675 -4.28976808 -2.3229397 9
#> 24 ideology 0.6928662 0.84273470 -0.59341647 1.5623440 9
#>
#> --------------------------------
#> Coefficients and clusters for context 4
#>
#> Post.Mean Post.Median HPD.lower HPD.upper Cluster
#> 1 (Intercept) -0.8903994 0.09817723 -6.24956563 1.9396862 1
#> 2 inequality -0.5194063 -1.05545098 -5.52732410 2.4806480 1
#> 3 income -1.8389976 -2.61416887 -7.33547948 3.1977064 1
#> 4 ideology -0.6308724 -0.12154014 -6.78243150 3.4991991 1
#> 5 (Intercept) 0.5407982 0.59339828 0.06196529 0.8238545 2
#> 6 inequality -0.7063873 -0.82207010 -1.13055013 0.1456299 2
#> 7 income -2.5730135 -2.55637861 -2.78934519 -2.3605590 2
#> 8 ideology -0.1455892 -0.17907481 -0.61556235 0.5337165 2
#> 9 (Intercept) -0.9552767 -0.83459163 -1.70539095 -0.5881316 3
#> 10 inequality -1.0049065 -1.04867463 -1.31639928 -0.5462079 3
#> 11 income -2.4123263 -2.36443824 -3.06492627 -1.9969965 3
#> 12 ideology -0.1440615 -0.31125791 -0.70342970 0.7672854 3
#> 13 (Intercept) -0.1838479 -0.08481470 -1.16365197 0.6153201 4
#> 14 inequality -1.0728628 -1.01875787 -1.86828531 -0.2981425 4
#> 15 income -2.7935725 -2.68230795 -3.91928513 -2.3144236 4
#> 16 ideology -0.6985899 -0.81464541 -1.61887220 0.1197860 4
#> 17 (Intercept) -0.1482050 -0.13644327 -0.38816906 0.1721607 6
#> 18 inequality -1.3670085 -1.43066950 -1.61851347 -0.7921525 6
#> 19 income -2.8902555 -2.89259794 -3.28874805 -2.5394833 6
#> 20 ideology 0.3993412 0.37923937 0.13283908 0.7740129 6
#>
#> --------------------------------
#> Coefficients and clusters for context 5
#>
#> Post.Mean Post.Median HPD.lower HPD.upper Cluster
#> 1 (Intercept) -0.77632266 -0.70626030 -2.0749416 6.85907654 1
#> 2 inequality -1.88996740 -2.01160752 -4.2907064 0.16820646 1
#> 3 income -1.67099013 -2.04266796 -3.6834548 1.53804966 1
#> 4 ideology -0.89297105 -0.62510269 -3.3556511 1.26357057 1
#> 5 (Intercept) 0.96974113 0.81342717 0.5525663 2.18190776 3
#> 6 inequality -2.62719381 -2.86362603 -3.1415367 -0.98703809 3
#> 7 income -0.22611205 -0.19117495 -0.5508760 -0.00517246 3
#> 8 ideology -2.24683923 -2.25060230 -2.4629584 -2.02570916 3
#> 9 (Intercept) -0.98164698 -0.83956942 -2.2318856 -0.56927650 4
#> 10 inequality -2.39673305 -2.61257362 -2.9327072 -0.81301555 4
#> 11 income -0.20409740 -0.23489673 -0.4441429 0.08477517 4
#> 12 ideology -2.19752953 -2.17054464 -2.5173243 -2.01586715 4
#> 13 (Intercept) -0.07794535 -0.07694925 -0.2588631 0.15828061 5
#> 14 inequality 0.98877605 0.98320702 0.7372542 1.25283159 5
#> 15 income -0.23365961 -0.29316412 -0.6544515 0.17999867 5
#> 16 ideology -2.00913850 -1.99415985 -2.1675912 -1.59688152 5
#>
#> --------------------------------
#> Context-level coefficients:
#> Description Post.Mean HPD.lower HPD.upper
#> 1 Intercept of beta[0] -0.5221868 -2.656193 1.816293
#> 2 Intercept of beta[1] -0.6730372 -3.222630 2.500552
#> 3 Intercept of beta[2] -0.2304794 -2.540405 2.140931
#> 4 Intercept of beta[3] -1.3304998 -4.191367 1.333457
#> 5 Effect of gap on beta[0] -0.3153792 -2.629037 1.824582
#> 6 Effect of gap on beta[1] 0.1709194 -2.048199 3.547572
#> 7 Effect of gap on beta[2] -0.7401711 -3.284754 1.469172
#> 8 Effect of gap on beta[3] 0.6979591 -1.088054 2.251056
#>
#> --------------------------------
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: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
Ferrari, Diogo. 2020. “Modeling Context-Dependent Latent Effect Heterogeneity.” Political Analysis 28 (1): 20–46.