knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
estimateEffect
in the stm packageExtracts the effect of a covariate on a set of topics selected by the user. Different effect types available depending on type of covariate. Before running this, the user should run a function to simulate necessary confidence intervals. See estimateEffect
of the stm package.
You need the devtools
package in order to install tidystm
. You can install it using the follow code (note that you only need to run this once):
if (!require(devtools)) install.packages("devtools")
You can then install tidystm
by running:
devtools::install_github("mikajoh/tidystm", dependencies = TRUE)
The package, for now, includes one function: extract.estimateEffect()
. You run it just as you would run the plot.estimateEffect()
function included in the stm
package. E.g.:
## Load the packages and set seed library(stm) library(tidystm) set.seed(2016) ## Load the example data from the stm pacakge data(gadarian) ## Estimate the effect on all three topics and return the point ## estimates in a tidy data frame prep <- estimateEffect(1:3 ~ treatment, gadarianFit, gadarian) effect <- extract.estimateEffect(prep, "treatment", model = gadarianFit, method = "pointestimate") knitr::kable(effect)
You can then use the results however you like. This is especially helpful if you want to plot it for yourself when the included plot functions doesnt cut it. For example:
``` {r results = "hold"}
prep <- estimateEffect(formula = 1:3 ~ treatment + pid_rep + treatment:pid_rep, stmobj = gadarianFit, metadata = gadarian)
op <- par(mfrow = c(1, 2)) for (i in c(0, 1)) { plot.estimateEffect(x = prep, covariate = "pid_rep", method = "continuous", model = gadarianFit, labeltype = "frex", n = 4, moderator = "treatment", moderator.value = i) } par(op)
Not very easy to see what going on. Instead, lets extract the estimates in a tidy format so that we can plot it ourselves ```r ## Lets extract the estimates in a tidy format so that we can plot it ## ourselves. We can now use lapply instead to first run it with ## moderator.value 0 and then with moderator.value 1, and then bind ## the two data frames together. effect <- lapply(c(0, 1), function(i) { extract.estimateEffect(x = prep, covariate = "pid_rep", method = "continuous", model = gadarianFit, labeltype = "frex", n = 4, moderator = "treatment", moderator.value = i) }) effect <- do.call("rbind", effect) ## And, for example, plot it with ggplot2 and facet by topic instead. library(ggplot2) ggplot(effect, aes(x = covariate.value, y = estimate, ymin = ci.lower, ymax = ci.upper, group = moderator.value, fill = factor(moderator.value))) + facet_wrap(~ label, nrow = 2) + geom_ribbon(alpha = .5) + geom_line() + scale_x_continuous(labels = function(x) ifelse(x == 1, "1\nREP", ifelse(x == 0, "0\nDEM", x))) + labs(x = "Party ID", y = "Expected Topic Proportion", fill = "Treated (0/1)") + theme(legend.position = "bottom")
Much better :)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.