{'fraction_non_alphanumeric': 0.029849532330215534, 'fraction_numerical': 0.007442049613664091, 'mean_word_length': 4.653103448275862, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
A new LDA formulation with covariates

The Latent Dirichlet Allocation (LDA) model is a popular method for creating mixed-membership clusters. Despite having been originally developed for text analysis, LDA has been used for a wide range of other applications. We propose a new formulation for the LDA model which incorporates covariates. In this model, a negative binomial regression is embedded within LDA, enabling straight-forward interpretation of the regression coefficients and the analysis of the quantity of cluster-specific elements in each sampling units (instead of the analysis being focused on modeling the proportion of each cluster, as in Structural Topic Models). We use slice sampling within a Gibbs sampling algorithm to estimate model parameters. We rely on simulations to show how our algorithm is able to successfully retrieve the true parameter values and the ability to make predictions for the abundance matrix using the information given by the covariates. The model is illustrated using real data sets from three different areas: text-mining of Coronavirus articles, analysis of grocery shopping baskets, and ecology of tree species on Barro Colorado Island (Panama). This model allows the identification of mixed-membership clusters in discrete data and provides inference on the relationship between covariates and the abundance of these clusters.


Introduction
Unsupervised machine learning methods allow the analysis of multivariate data sets in which no response variable is available. This type of analysis is especially useful as the amount of unstructured information grows (in the form of texts, for example), enabling the unveiling of latent structure in the data. In particular, the Latent Dirichlet Allocation (LDA) method is an unsupervised technique that focuses on identifying unobservable groups. This method is different from traditional unsupervised methods such as hard clustering, where sampling units can only be classified into a single group. In LDA, soft clustering is performed, that is, a sample unit can belong to several groups at the same time.
The LDA model was originally proposed by Pritchard et al. [18] in the context of population genetics, but it became popular in the context of machine learning through the work of Blei et al. [3] on text-mining applications. In these text-mining applications, the goal is to discover topics that are present in each document based on the words that appear on these documents. This model has been applied to several areas of knowledge. For example, Lukins et al. [14] used LDA to understand software bug reports. In another application, Lienou et al. [12] applied this model to create annotation of satellite imagery. LDA was also used by Xing and Girolami [22] to detect fraudulent calls in the telecommunications industry based on the patterns found for each customer. Finally, Valle et al. [20] used LDA on biodiversity data to describe groups of trees.
Several variations of LDA exist. For instance, Mcauliffe and Blei [15] introduced the supervised LDA model where documents are labeled with continuous or discrete response variables. Wang and Grimson [21] considered a spatial structure to group spatially close elements (such as words that are close in the text). Blei and Lafferty [2] analyzed the evolution of topics over time through a family of probabilistic time series models. Albuquerque et al. [1] adapted the LDA model for different types of data (multinomial, binomial and bernoulli) and used a special prior called truncated stick-breaking (TSB) prior to identify the optimal number of groups.
In many problems, one also has access to additional information about instances that comes in the form of features (covariates). For example, a company may have socioeconomic information about its customers, such as age or income, that can help in understanding customer interactions via chat. In these cases, it can be useful to explore the relationship between these covariates and the identified groups.
Related work. Roberts et al. [19] developed Structural Topic Models (STM), in which covariates were incorporated into LDA through a Multinomial regression model so that the probability of each topic in a given instance is allowed to depend on covariates. The focus on the probability of each topic, instead of the abundance of each topic, is an important limitation. For example, the type of scientific article (e.g., a commentary or a review article) can significantly change the number of words associated with each topic. However, STM's might fail to identify this effect if the proportion of the different topics remains the same. Like STM, Jacobs et al. [9] also considers covariates to explain the proportions of each cluster through a logarithmic link function but with a focus on predictions, not inference. Furthermore, the approach proposed in Jacobs et al. [9] requires running the model multiple times to determine the optimal number of clusters. On the other hand, our approach can readily determine the optimal number of clusters by running the model just once and can be used both for prediction and for inference on regression coefficients.
In this work, we propose a new formulation to the LDA model where we use covariates to explain the number of elements (e.g., number of words) in each group, rather than the proportion of each group. In a sense, our model is more general than STM's because the probabilities of each group can also be derived from it. Another important advantage of our approach is that the covariate coefficients (i.e., the slope parameters) can be interpreted more easily through the logarithmic link function of the Negative-Binomial regression rather than the logistic link function used within the Multinomial regression in STM's. The log link function ( Fig. 1 (a)) allows a straightforward interpretation of the coefficients in the sense that a positive (negative) coefficient describes a positive (negative) relationship between the corresponding covariate and the abundance of each group. On the other hand, as illustrated in Fig. 1 (b), a positive coefficient in the multinomial logistic link function can imply a positive or negative relationship between the proportion of the group and the corresponding covariate.
In section 2 we present the proposed Bayesian model and the full conditional distributions required for our Gibbs sampler. In section 3 we describe the estimation method and the software used. Then, in section 4, we apply our  model to simulated data sets to demonstrate its effectiveness in providing inferences on the parameters of interest. In section 5, we illustrate the versatility of our model by applying it to data sets from different fields. Section 6 compares our model to STMs using the probabilistic coherence metric. We conclude with a discussion of the advantages and disadvantages of the method and suggestions for future research.

Model
We start by introducing the proposed model. Let denote the total number of instances, be the total number of clusters and be the total number of categories each element from each instance can belong to. For example, in text analysis we can have topics, a vocabulary of distinct words and documents. In this case, the words in each document are the elements. We denote by , * , * ∶= ∑ , , , the total number of elements at instance , where , , is a latent variable representing the total number of elements of category and cluster in instance . The data that we observe consist of • , ∈ {1, … , }, the category of the -th element of instance , = 1, … , , * , * and = 1, … , .
The data , are often summarized as an instance-by-category abundance matrix. More specifically, each cell in this matrix contains the total number of elements of category on instance , given by , ∶= ∑ ( , = ), = 1, … , and = 1, … , .
In our model, the link between the covariates and the abundance of each cluster in each instance , * , = ∑ =1 , , is given by a Negative-Binomial regression: where is a -dimension vector, is the overdispersion parameter and , ∶= [ , * , ] =∶ exp ( ). Notice that , * , are latent variables (and thus are not observed), and therefore we need to estimate the coefficients of the regression function at the same time we estimate , * , (see details in Section 2.1).
The model also assumes that where ∈ ℝ is a vector on a simplex that represents the composition of categories inside cluster . We call Φ the matrix with elements , . Furthermore, note that within the standard LDA model, a parameter of primary interest is , , which is the proportion of cluster in instance . This parameter can be easily retrieved based on the , * , results from our model by calculating , = , * , . We call Θ the matrix with elements , .
We use the following prior distributions: where is a diagonal matrix. The hyper parameters 0 , and are a priori set by the modeler.
The joint density function induced by the likelihood function and prior distributions is given by Unif( | 0, 0 ).

Full Conditional Distributions
We can obtain samples from the posterior distribution by using a Gibbs sampler [7]. In order to do that, we first derive the full conditional distributions for the parameters in our model. First, we derive the conditional distribution of each given all the other quantities: Thus, which is straightforward to sample from.
The conditional distribution of given all of the other quantities is Because of lack of conjugacy, we rely on a slice-sampler algorithm (see Appendix C) to sample from this FCD.
For the parameter , we obtain Again, we rely on a slice-sampler algorithm to sample from this FCD.
Finally, we obtain the conditional distribution of , (the latent group membership of the -th element in the -th instance): After integrating out and simplifying this expression (a detailed derivation of these results is provided in Appendix A and B), we obtain: where ′ , = + ′ , . Thus, , … , which is easy to sample from.

Estimation and Software
In order to fit our model, we first need to define , the number of clusters that will be used. We do this by using the LDA model proposed by [1]. Instead of relying on the standard but computationally inefficient approach of fitting multiple models (one for each K value) to then determine the optimal number of clusters, the model in [1] relies on a truncated stick-breaking prior distribution to identify the optimal number of clusters. We use the following values for the hyperparameters: 0 = 1000, 1 = ⋯ = = 0.1 and is a diagonal matrix where the diagonal elements are equal to 10.
In our experiments, we use the Gibbs sampler implementation based on the conditional distributions described in Section 2.1 with one exception: the samples from Φ were generated using the model without covariates described in [1]. We took this approach because preliminary results revealed that this model had difficulty estimating the Φ matrix even in situations where the model without covariates estimated this matrix well. This problem arises because, differently from a standard regression in which the response variable is observed, the response variable here is latent and has to be estimated together with the regression parameters. As a result, a misspecified regression model can negatively impact the latent response variable, potentially mischaracterizing the identified clusters. Our simulated data studies reveal that this two-stage estimation results in good estimates for the parameters of interest.
In each experiment, we assessed convergence by visually evaluating trace-plots of the generated MCMC chains.
Part of our algorithm was developed in R while part of the code was made in C++ using the Rcpp package [6]. An R package and a tutorial on how to use the model can be found at https://github.com/gilsonshimizu/ldacov.

Simulated experiments
First, we apply our model to two simulated data sets where all parameters are known, enabling the assessment of whether the model is estimating the parameters of interest well.

Simulation set 1
The first simulated dataset consists of 1000 instances, 100 categories and four clusters. Four covariates are also used, where each covariate explains only one of the four clusters, that is, the matrix with the regression slope coefficients is an identity matrix. We also choose covariate values such that some elements of the Θ are equal to 1 (i.e., some instances have elements from only one cluster). Similarly, we assume that some categories are only present in a single cluster. We do this to help model identifiability. Figures 2 (a) and (b) show a high correlation between the true and estimated elements of Φ and Θ, indicating that the true parameter values can be recovered from the model when it is estimated using the strategy described in Section 3. Our method can be used to make predictions for the abundance matrix on the data samples using the information given by the covariates. Figure 3 shows that the method leads to high prediction accuracy on a hold-out set with 1000 instances.

Simulation set 2
The second set of simulated data is similar to the one described previously. However, instead of using the correct set of covariates, we relied on randomly generated covariates. As result, these covariates were independent of the number of individuals in each cluster. The purpose of this data set is to verify whether the model is able to infer when none of the covariates are relevant.

Figures 4 (a) and (b)
show that both Φ and Θ were well estimated. Table 2 shows that all 99% credible intervals for 's contained the value zero, which are the correct values given that the covariates were independent of the number of elements in each cluster.
Again, the model proposed by Roberts et al. [19] manages to estimate the Θ matrix well but has difficulties in estimating the Φ matrix (Figures 4 (c) and (d)).  Taken together, these results reveal that our model is able to estimate the matrices Φ and Θ as well as identify the relevant variables to explain the quantities in each cluster.

Applications
To demonstrate the model's effectiveness and flexibility, we applied it to three real data sets from different areas: • Before analyzing these data, we removed species that were extremely rare (defined as those species with less than 10 trees across the entire 50-ha plot). Our criteria resulted in the removal of 70 (21%) species, representing less than 0.1% of the total number of trees in our dataset.

Covid Articles
The number of monthly articles about coronavirus practically doubled in the first quarter of 2020 in relation to the monthly average of publications in 2019. Given this significant increase and relevance of the subject, our goal here is to find and understand the differences between possible clusters of articles. Other works involving the analysis of Twitter data and medical documents about Covid-19 can be found at [13,16,17]. Instead of only analyzing tabular information from articles, our analysis complements the work of Liu et al. [13] by extracting information from the abstracts of these articles.
In this text-mining application, we follow the literature in referring to topics instead of clusters. We set the maximum number of topics to 10 and use the TSB prior model proposed by Albuquerque et al. [1] to identify the optimal number of topics. This analysis identifies that the optimal number of topics was equal to 5 for this dataset. Table 3 shows the most relevant words found in each topic. A word is defined to be relevant for a given topic if it appears at least twice as frequently in this topic than in any other topic. Table 4 shows the estimated regression weights for each topic.
Based on these results we can describe the topics found as follows: • Topic 1: This topic is related to virology and genetics. Furthermore, this topic is more common in older articles.
• Topic 2: This topic is related to recent public health articles. This topic seems to be strongly associated with the current pandemic given that the prominent words were China, countries, outbreak, prevention, etc.
• Topic 3: This topic is related to recent articles on infectious diseases and medicine. The most relevant words in this topic suggest that it is related to tests and symptoms of Covid-19.
• Topic 4: This topic is related to older articles on vaccines, immunology and veterinary. Words like "mice" and "animal" indicate vaccine tests on non-human animals.
• Topic 5: This topic is related to older articles on viruses and influenza. Although it is natural that there is a strong relationship between keywords and topics, we emphasize that the use of these keywords as covariates allowed an easier interpretation of topics than just analyzing the relevant words of each topic.

Grocery Shopping
Our goal is to find clusters of grocery shopping baskets while also identifying how these clusters are associate with day of week and days since last purchase. Other works such as [4,9] also analyze this type of data using the LDA model.
After running the model without covariates, we obtain an ideal number of clusters equal to 4. Table 5 and Table 6 show the relevant products of each cluster and the estimates of the regression parameters, respectively.
Below we describe the clusters that were found: • Cluster 1: This cluster has many products with purchases made on any day of the week and with varying frequencies. The products are of daily use like breads, cereals, coffee and also cleaning products.
• Cluster 2: This cluster contains herbs, spices, vegetables, poultry, etc. This type of purchase might be associated with the preparation of a special meal. This purchase is usually made on a Saturday.
• Cluster 3: This cluster contains frozen meals and prepared soups.
• Cluster 4: This cluster is very peculiar with baby formulas, beers and wines. This cluster is reflective of the classic example of basket analysis, where parents go to buy baby diapers and take the opportunity to buy beer and wine. These purchases are made less frequently than other clusters and are generally not made on Saturdays.
We note here that a customer can buy more than one basket (cluster) at the same time, which is a very interesting feature of the LDA models (compared to traditional cluster analysis) and more realistic for this data set. In a conventional cluster analysis a customer would be classified into just one cluster.

Barro Colorado Island
We ran the LDA model without covariates and, from a total of 20 potential groups, found 11 dominant groups that together comprised approximately 91% of all individuals. We find relatively strong spatial patterns in the distribution of these groups ( Figure 5). For instance, group 10 is clearly restricted to the areas with steep slopes while group 3 has much higher abundance in flat areas. Interestingly, several of the groups identified here seems to closely correspond to the BCI habitat classification proposed by Harms et al. [8]. For example, group 11 seems to match the "old forest, swamp" class while group 10 seems to match the "Old forest, Streamside". However, different from this discrete classification of habitats, we find spatial patterns that reflect substantial mixed membership. Similar results could have been obtained from the LDA model without covariates. The novelty of the proposed model is the ability to make formal inference on the effect of covariates (Table 7). We find that all groups, except for group 2, were strongly associated with one or more covariates. For example, as expected, groups 10 and 3 were positively and negatively associated with slope, respectively. The variables that tended to influence a large number of groups were slope, convexity, magnesium and zinc.

Model comparison using probabilistic coherence
Up to this point, we have compared STM to our model using simulated data because it enables us to determine how well these approaches estimate the true parameter values. The problem is that this approach implicitly assumes that the data follow our generative model. To avoid this assumption, we can also compare our model to STM [19] using a measure of the quality of each cluster, known as probabilistic coherence, as proposed by Jones [11].
The probabilistic coherence calculates for each pair of categories the measure ( 1 | 2 ) − ( 1 ), where category 1 is more likely than category 2 in the focus cluster. Note that ( 1 | 2 ) − ( 1 ) will be close to zero if 1 and 2 are independent and, as a result, probabilistic coherence measures how strongly associated are categories 1 and 2 . A well delineated cluster with a high frequency of these categories should have a high probabilistic coherence. By definition, we consider only the top most frequent categories in the clusters and use the sum of the probabilistic coherence of all clusters as a measure of the quality of the estimated Φ matrix. For a specific cluster , the probabilities ( 1 | 2 ) and ( 1 ) are estimated by |number of instances of categories 1 and 2 in | |number of instances of category 2 in | and |number of instances of category 1 in | |number of instances in | , respectively. We used the textmineR package [10] in R to calculate probabilistic coherence with the default value of M (5). Table 8 shows these measures for all datasets analyzed in this work. In all cases, the proposed model obtained better probabilistic coherence measures than the STM model, indicating that the topics that are found with our method are more coherent.

Discussion
We propose a new formulation for the LDA model that allows the incorporation of covariates. This model differs from other LDA methods because it models how covariates affects the number of elements of each cluster rather than the proportions of the clusters. Because these proportions can be derived from the number of elements, our model generalizes existing LDA models that also incorporate covariates.
The main advantage of our model is that it enables a much more straight-forward interpretation of the regression coefficients. This is due to the use of the logarithmic link function on the quantities in each cluster instead of a multinomial logistic function on the proportions. Furthermore, by more faithfully representing uncertainty, inference on the regression coefficients is likely to be better with our Gibbs sampler algorithm than when using approximate variational estimation methods.
In our simulated examples, we are able to show that our model estimates well the Φ and Θ matrices with or without relevant covariates. We also illustrate the model's ability to make inferences about regression coefficients through credible intervals and the ability to make predictions for the abundance matrix on the data samples using the information given by the covariates. Importantly, our examples with real data sets demonstrate the flexibility of the model to be applied in different areas and for different types of data.
The dataset about Covid articles, for example, is a traditional text mining data set. The use of covariates, together with the main words of each topic, enabled us to determine how the focus of these articles has changed as the new coronavirus spurs a pandemic across the world. In the data set on supermarket purchases our model was able to create clusters and relate them to the time and day of the week covariates. We believe that this tool is very useful to segment customers and also to optimize the layout of products within a grocery store. Finally, when applied to the BCI dataset, our model was able to find clusters with distinct spatial patterns and at the same time relate these patterns to some of the soil and topography features.
A disadvantage of our method is the computational cost (especially in large datasets with many sample units and categories) compared to models using variational Bayes methods. A possible future work would be to consider a version of this model with a variational inference approach.

Acknowledgement
This study was financed in part by the Coordenação de Aperfeiçoamento ] .
The integral involving is available in closed form (see Appendix B). Furthermore, several elements in the equation above can be eliminated because they are constants. As a result, we obtain the following expression: We drop additional terms that are constants to obtain: Because , ′ is either equal to 1 or 2, we can divide both by 1 1 + 2 2 and, using factorial and gamma function rules, we obtain:

B. Multinomial Integration in
To simplify the calculation of the conditional distribution of , we can integrate out as shown below.