Package texteffect. November 27, 2017

Similar documents
Package MIICD. May 27, 2017

Package cgh. R topics documented: February 19, 2015

Package sparsereg. R topics documented: March 10, Type Package

Package intccr. September 12, 2017

Package DPBBM. September 29, 2016

Package OptimaRegion

Package beast. March 16, 2018

Package PTE. October 10, 2017

Package sure. September 19, 2017

Package gains. September 12, 2017

Package esabcv. May 29, 2015

Package bayesdp. July 10, 2018

Package bayescl. April 14, 2017

Package DTRreg. August 30, 2017

Package bisect. April 16, 2018

Package BayesCR. September 11, 2017

Package lcc. November 16, 2018

Package MatchingFrontier

Package misclassglm. September 3, 2016

Package CausalGAM. October 19, 2017

Package bayeslongitudinal

Package treedater. R topics documented: May 4, Type Package

Package merror. November 3, 2015

Package TVsMiss. April 5, 2018

Package manet. September 19, 2017

Package distdichor. R topics documented: September 24, Type Package

Package weco. May 4, 2018

Package CausalImpact

Package GLDreg. February 28, 2017

Package ArCo. November 5, 2017

Package tidylpa. March 28, 2018

Package panelview. April 24, 2018

Package SSLASSO. August 28, 2018

Package sigqc. June 13, 2018

Package mixphm. July 23, 2015

Package MixSim. April 29, 2017

Package MatchIt. April 18, 2017

Package cosinor. February 19, 2015

Package ANOVAreplication

Package LVGP. November 14, 2018

Package pnmtrem. February 20, Index 9

Package gibbs.met. February 19, 2015

Package clustvarsel. April 9, 2018

Package ICEbox. July 13, 2017

Package epitab. July 4, 2018

Package milr. June 8, 2017

Package rpst. June 6, 2017

Package fbroc. August 29, 2016

Package rereg. May 30, 2018

Package binomlogit. February 19, 2015

Package kirby21.base

Package clusterpower

Package deming. June 19, 2018

Package endogenous. October 29, 2016

Package gee. June 29, 2015

Package SEMrushR. November 3, 2018

Package robustreg. R topics documented: April 27, Version Date Title Robust Regression Functions

Package spark. July 21, 2017

Package mgc. April 13, 2018

Package RPMM. August 10, 2010

Package bdots. March 12, 2018

Package anidom. July 25, 2017

Package rknn. June 9, 2015

Package parfossil. February 20, 2015

Package QCAtools. January 3, 2017

Package Modeler. May 18, 2018

Package dgo. July 17, 2018

Package robets. March 6, Type Package

Package oaxaca. January 3, Index 11

Package HMRFBayesHiC

Package EnQuireR. R topics documented: February 19, Type Package Title A package dedicated to questionnaires Version 0.

Package quickreg. R topics documented:

Package EBglmnet. January 30, 2016

Package Bergm. R topics documented: September 25, Type Package

Package SIDES. May 30, 2017

Package gtrendsr. October 19, 2017

Introduction to Mplus

Package clusternomics

Package mfa. R topics documented: July 11, 2018

Package tsbugs. February 20, 2015

Package nearfar. June 3, 2017

Package simsurv. May 18, 2018

Package automl. September 13, 2018

Package r2d2. February 20, 2015

Package addhaz. September 26, 2018

Package sglr. February 20, 2015

Package cncagui. June 21, 2015

Package DBfit. October 25, 2018

Package svalues. July 15, 2018

Package tpr. R topics documented: February 20, Type Package. Title Temporal Process Regression. Version

CHAPTER 7 EXAMPLES: MIXTURE MODELING WITH CROSS- SECTIONAL DATA

Package RNAseqNet. May 16, 2017

Package raker. October 10, 2017

Package SeleMix. R topics documented: November 22, 2016

Package nonmem2r. April 5, 2018

Package dynsim. R topics documented: August 29, 2016

Package PDN. November 4, 2017

Package snm. July 20, 2018

Package mmeta. R topics documented: March 28, 2017

Package SparseFactorAnalysis

Transcription:

Version 0.1 Date 2017-11-27 Package texteffect November 27, 2017 Title Discovering Latent Treatments in Text Corpora and Estimating Their Causal Effects Author Christian Fong <christianfong@stanford.edu> Maintainer Christian Fong <christianfong@stanford.edu> Depends R (>= 3.3.3), MASS, boot, ggplot2 Imports Implements the approach described in Fong and Grimmer (2016) <https://aclweb.org/anthology/p/p16/p16-1151.pdf> for automatically discovering latent treatments from a corpus and estimating the average marginal component effect (AMCE) of each treatment. The data is divided into a training and test set. The supervised Indian Buffet Process (sibp) is used to discover latent treatments in the training set. The fitted model is then applied to the test set to infer the values of the latent treatments in the test set. Finally, Y is regressed on the latent treatments in the test set to estimate the causal effect of each treatment. LazyLoad yes LazyData yes License GPL (>= 2) NeedsCompilation no Repository CRAN Date/Publication 2017-11-27 16:49:24 UTC R topics documented: BioSample.......................................... 2 get_training_set....................................... 2 infer_z........................................... 4 1

2 get_training_set sibp............................................. 5 sibp_amce.......................................... 8 sibp_exclusivity....................................... 9 sibp_param_search..................................... 11 sibp_top_words....................................... 13 Index 15 BioSample Sample from the Fong and Grimmer Wikipedia Biography Data Format Source This data set gives a small sample of the data used in Discovery of Treatments from Text Corpora by Christian Fong and Justin Grimmer. This sample is intended as a toy data set for use in the examples of this package s documentation. A real data set should include far more observations. BioSample A data frame consisting of 51 columns (including an outcome measure and counts for each word in a 50 word vocabulary) and 250 observations. Data collected using the Wikipedia API and an original survey experiment by Fong and Grimmer. Fong, Christian and Justin Grimmer. (2016). Discovery of Treatments from Text Corpora. Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics, 1600-1609. get_training_set Divide Data into Training and Test Sets get_training_set divides the data into a training set and test set and returns the indices of the observations in the training set. get_training_set(x, Y, training.prop = 0.5)

get_training_set 3 Arguments X Y training.prop The covariate matrix. The outcome vector. The proportion of observations to be placed in the training set, rounded to the nearest whole number. Details Value This package implements a two-step procedure, in which a mapping from text (X) to latent treatments is inferred on a training set, and then the relationship between the latent treatments and the outcome is estimated using a test set. This function divides the data into a training and test set. The user will not have to explicitly specify the training or test sets in sibp functions because this the training indices will be incorporated into the sibp object. train.ind The indices of the observation in the training set. This is used as an argument for sibp and sibp_param_search. Author(s) Christian Fong Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp Examples ##Load the Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Fit an sibp on the training data sibp.fit <- sibp(x, Y, K = 2, alpha = 4, sigmasq.n = 0.8, train.ind = train.ind) sibp_top_words(sibp.fit, colnames(x))

4 infer_z infer_z Infer Treatments on the Test Set infer_z uses an sibp object fitted on a training set to infer the treatments in a test set. infer_z(sibp.fit, X) Arguments sibp.fit X A sibp object. The covariates for the data set where Z is to be inferred. These covariates should be preprocessed by subtracting the mean of X in the training set and dividing by the standard deviation. See the example for more details. Details This function applies the mapping from words to treatments discovered in the training set to infer which observations have which treatments in the test set. Usually, users will be better served by calling sibp_amce, which calls this function internally before returning estimates and confidence intervals for the average marginal component effects. Value nu Informally, the probability that the row document has the column treatment. Formally, the parameter for the variatioanl approximation of z_i,k, which is a Bernoulli distribution. Author(s) Christian Fong Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp, sibp_amce

sibp 5 Examples ##Load the Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Fit an sibp on the training data sibp.fit <- sibp(x, Y, K = 2, alpha = 4, sigmasq.n = 0.8, train.ind = train.ind) # Pre-process the test set X.test <- t(apply(x, 1, function(x) (x - sibp.fit$meanx)/sibp.fit$sdx)) # Infer the latent treatments in the test set infer_z(sibp.fit, X.test) sibp Supervised Indian Buffet Process (sibp) for Discovering Treatments sibp discovers latent binary treatments within a corpus, as described by Fong and Grimmer (2016). Arguments sibp(x, Y, K, alpha, sigmasq.n, a = 0.1, b = 0.1, sigmasq.a = 5, train.ind, silent = FALSE ) X Y K alpha sigmasq.n a b The covariates for all observations in the training set, where each row is a document and each column is the count of a word. The outcome for all observations in the training set. The number of treatments to be discovered. A parameter that influences how common the treatments are. When alpha is large, the treatments are common. A parameter determining the variance of the word counts conditional on the treatments. When sigmasq.n is large, the treatments must explain most of the variation in X. A parameter that, together with b, influences the variance of the treatment effects and the outcomes. a = 0.1 is a reasonably diffuse choice. A parameter that, together with a, influences the variance of the treatment effects and the outcomes. b = 0.1 is a reasonably diffuse choice.

6 sibp sigmasq.a train.ind silent A parameter determining the variance of the effect of the treatments on word counts. A diffuse choice, such as 5, is usually appropriate. The indices of the observations in the training set, usually obtained from get_training_set(). If TRUE, prints how much the parameters have moved every 10 iterations of sibp. Details Value Fits a supervised Indian Buffet Process using variational inference. Before running this function, the data should be divided into a training set and a test set. This function should be run on the training set to discover latent treatments in the data that seem to be correlated with the outcome. It is recommended to use linksibp_param_search instead of this function to search over multiple configurations of the most important parameters. So long as only the training data is used, the analyst can freely experimient with as many parameter configurations as he likes without corrupting his causal inferences. Once a parameter configuration is chosen, the user can then use sibp_amce on the test set to estimate the average marginal component effect (AMCE) for each treatment. nu m S lambda phi big.phi c d K D alpha a Informally, the probability that the row document has the column treatment. Formally, the parameter for the variational approximation of z_i,k, which is a Bernoulli distribution. Informally, the effect of having each treatment on the outcome. Formally, the mean parameter for the variational approximation of the posterior distribution of beta, which is a normal distribution. Note that this is in the training sample, and it is inappropriate to use this posterior as the basis for causal inference. It is instead necessary to estimate effects using the test set, see sibp_amce. The variance parameter for the posterior distribution of beta, which is a normal distribution. A matrix where the kth row contains the shape parameters for the variational approximation of the posterior distribution of pi_k, which is a beta distribution. Informally, the effect of the row treatment on the column word. Formally, the mean parameter for the variational approximation of the posterior distribution of A, which is a normal distribution. The variance parameter for the variational approximation of the posterior distribution of A, which is a normal distribution. The kth element of the list corresponds to a treatment k. The shape parameter for the variational approximation of the posterior distribution of tau, which is a gamma distribution. The rate parameter for the variational approximation of the posterior distribution of tau, which is a gamma distribution. The number of treatments. The number of words in the vocabulary. The alpha used to call this function. The a used to call this function.

sibp 7 b sigmasq.a sigmasq.n train.ind test.ind The b used to call this function. The sigmasq.a used to call this function. The sigmasq.n used to call this function. The indices of the observations in the training set. The indices of the observations in the test set. Author(s) Christian Fong Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp_param_search, sibp_top_words, sibp_amce Examples ##Load the Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Search sibp for several parameter configurations; fit each to the training set sibp.search <- sibp_param_search(x, Y, K = 2, alphas = c(2,4), sigmasq.ns = c(0.8, 1), iters = 1, train.ind = train.ind) ## Not run: # Get metric for evaluating most promising parameter configurations sibp_rank_runs(sibp.search, X, 10) # Qualitatively look at the top candidates sibp_top_words(sibp.search[["4"]][["0.8"]][[1]], colnames(x), 10, verbose = TRUE) sibp_top_words(sibp.search[["4"]][["1"]][[1]], colnames(x), 10, verbose = TRUE) # Select the most interest treatments to investigate sibp.fit <- sibp.search[["4"]][["0.8"]][[1]] # Estimate the AMCE using the test set amce<-sibp_amce(sibp.fit, X, Y) # Plot 95% confidence intervals for the AMCE of each treatment sibp_amce_plot(amce)

8 sibp_amce ## End(Not run) sibp_amce Infer Treatments on the Test Set sibp_amce uses an sibp object fitted on a training set to estimate the AMCE with the test set. sibp_amce(sibp.fit, X, Y, seed = 0, level = 0.05, nboot = 1000) sibp_amce_plot(sibp.amce, xlab = "Feature", ylab = "Outcome") Arguments sibp.fit X Y seed level nboot sibp.amce xlab ylab A sibp object. The covariates for the full data set. The division between the training and test set is handled inside the function. The outcomes for the full data set. The division between the training and test set is handled inside the function. The seed The level of the confidence intervals to be obtained. The number of bootstrap replicates used to calculate the confidence intervals for the AMCE. The table returned by codesibp_amce. The label for the x-axis of the plot. The label for the y-axis of the plot. Details Nothing Value sibp.amce A table where the first column is the index of the treatment, the second column ("effect") is the estimated AMCE, the third column ("L") is the lower bound of the confidence interval, and the fourth column ("U") is the upper bound of the confidence interval. Author(s) Christian Fong

sibp_exclusivity 9 Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp Examples ##Load the sample of Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Fit an sibp on the training data sibp.fit <- sibp(x, Y, K = 2, alpha = 4, sigmasq.n = 0.8, train.ind = train.ind) sibp.amce <- sibp_amce(sibp.fit, X, Y, nboot = 100) sibp_amce_plot(sibp.amce) sibp_exclusivity Calculate Exclusivity Metric sibp_exculsivity calculates the coherence metric for an sibp object fit on a training set. sibp_rank_runs runs sibp_exclusivity on each element in the list returned by sibp_param_search, and ranks the parameter configurations from most to least promising. Arguments sibp_exclusivity(sibp.fit, X, num.words = 10) sibp_rank_runs(sibp.search, X, num.words = 10) sibp.fit sibp.search X num.words A sibp object. A list of sibp object fit using the training set, obtained using sibp_param_search. The covariates for the full data set. The division between the training and test set is handled inside the function. The top words whose coherence will be evaluated.

10 sibp_exclusivity Details The metric is formally described at the top of page 1605 of https://aclweb.org/anthology/p/ P16/P16-1151.pdf. The purpose of this metric is merely to suggest which parameter configurations might contain the most interesting treatments to test if there are too many configurations to investigate manually. The choice of the parameter configuration should always be made on the basis of which treatments are substantively the most interesting, see sibp_top_words. Value exclusivity An exclusivity matrix which quantifies the degree to which the top words in a treatment appear in documents that have that treatment but not in documents that lack that treatment. exclusivity_rank A table that ranks the treatments discovered by the various runs from sibp.search from most exclusive to least exclusive. Author(s) Christian Fong Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp_param_search, sibp_top_words Examples ##Load the sample of Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Search sibp for several parameter configurations; fit each to the training set sibp.search <- sibp_param_search(x, Y, K = 2, alphas = c(2,4), sigmasq.ns = c(0.8, 1), iters = 1, train.ind = train.ind) # Get metric for evaluating most promising parameter configurations sibp_rank_runs(sibp.search, X, 10)

sibp_param_search 11 sibp_param_search Search Parameter Configurations for Supervised Indian Buffet Process (sibp) sibp_param_search runs sibp for a variety of parameter configurations, so that the user can then test the effects fo the most interesting treatments. Arguments sibp_param_search(x, Y, K, alphas, sigmasq.ns, iters, a = 0.1, b = 0.1, sigmasq.a = 5, train.ind = train.ind, seed = 0) X Y K alphas sigmasq.ns iters a b sigmasq.a train.ind seed The covariates for the full data set. The division between the training and test set is handled inside the function. The outcomes for the full data set. The division between the training and test set is handled inside the function. The number of treatments to be discovered. A vector of values of alpha to try. A vector of values of sigmasq.n to try. The number of starting values to attempt for each combination of alpha and sigmasq.n. A parameter. A parameter. A parameter. The indices of the observations in the training set, usually obtained from get_training_set(). The seed to be used, so the result can be replicated. Details Fits a supervised Indian Buffet Process using variational inference for combinations of alpha and sigmasq.n. alpha influences how common the treatments are (where larger alphas imply more common treatments) and sigmasq.n influences how much of the variation of the outcome must be explained by the treatments. These parameters are the most important for determining the quality of the treatments discovered, so it is usually a good idea to experiment with many combinations. Because the treatments discovered can be sensitive to starting values, it is also usually a good idea to try each combination of alpha and sigmasq.n several times by setting iters > 1. Because this function uses only the training data, the user can experiment with many parameter configurations without corrupting the inferences made with the test set. The choice of parameters is equivalent to the choice of hypotheses to test, so the analyst should choose the parameter configuration that leads to the most substantively interesting treatments. sibp_top_words can be applied

12 sibp_param_search to each element of the list returned by this function to determine which parameter configurations lead to interesting treatments. Often, it will be impractical to manually investigate every parameter configuration. In such cases, sibp_rank_runs can be used to automatically identify some of the most promising candidates. Value paramslist Author(s) Christian Fong Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp_rank_runs, sibp_top_words, sibp_amce Examples ##Load the sample of Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Search sibp for several parameter configurations; fit each to the training set sibp.search <- sibp_param_search(x, Y, K = 2, alphas = c(2,4), sigmasq.ns = c(0.8, 1), iters = 1, train.ind = train.ind) ## Not run: # Get metric for evaluating most promising parameter configurations sibp_rank_runs(sibp.search, X, 10) # Qualitatively look at the top candidates sibp_top_words(sibp.search[["4"]][["0.8"]][[1]], colnames(x), 10, verbose = TRUE) sibp_top_words(sibp.search[["4"]][["1"]][[1]], colnames(x), 10, verbose = TRUE) ## End(Not run)

sibp_top_words 13 sibp_top_words Report Words Most Associated with each Treatment sibp_top_words returns a data frame of the words most associated with each treatment. sibp_top_words(sibp.fit, words, num.words = 10, verbose = FALSE) Arguments sibp.fit words num.words verbose A sibp object. The actual words, usually obtained through rownames(x). The number of top words to report. If set to true, reports how common each treatment is (so that the analyst can focus on the common treatments) and how closely associated each word is with each treatment. Details The choice of the parameter configuration should always be made on the basis of which treatments are substantively the most interesting. This function provides one natural way of discovering which words are most associated with each treatment (the mean parameter for the posterior distribution of phi, where phi is the effect of the treatment on the count of word w) and therefore helps to determine which treatments are most interesting. Value top.words A data frame where each column consists of the top ten words (in order) associated with a given treatment. Author(s) Christian Fong Fong, Christian and Justin Grimmer. 2016. Discovery of Treatments from Text Corpora Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. https: //aclweb.org/anthology/p/p16/p16-1151.pdf See Also sibp

14 sibp_top_words Examples ##Load the Wikipedia biography data data(biosample) # Divide into training and test sets Y <- BioSample[,1] X <- BioSample[,-1] train.ind <- get_training_set(x, Y, training.prop = 0.5) # Fit an sibp on the training data sibp.fit <- sibp(x, Y, K = 2, alpha = 4, sigmasq.n = 0.8, train.ind = train.ind) sibp_top_words(sibp.fit, colnames(x))

Index Topic datasets BioSample, 2 BioSample, 2 get_training_set, 2 infer_z, 4 sibp, 3, 4, 5, 9, 13 sibp_amce, 4, 6, 7, 8, 12 sibp_amce_plot (sibp_amce), 8 sibp_exclusivity, 9 sibp_param_search, 7, 10, 11 sibp_rank_runs, 12 sibp_rank_runs (sibp_exclusivity), 9 sibp_top_words, 7, 10 12, 13 15