-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
126 lines (96 loc) · 4.26 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
### tidystm: Extract (tidy) effect from `estimateEffect` in the [stm package](http://www.structuraltopicmodel.com/)
[](https://travis-ci.org/)
[](https://ci.appveyor.com/project/mikaelpoul/tidystm)
Extracts 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](http://www.structuraltopicmodel.com/).
#### Install
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):
```{r}
if (!require(devtools)) install.packages("devtools")
```
You can then install `tidystm` by running:
```{r, eval = FALSE}
devtools::install_github("mikajoh/tidystm", dependencies = TRUE)
```
#### Use
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.:
```{r}
## 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"}
## This time, lets estimate treatment effect as a function of party
## id. We can than get an idea of whether the treatment effect vary
## for people with different ids.
prep <- estimateEffect(formula = 1:3 ~ treatment + pid_rep + treatment:pid_rep,
stmobj = gadarianFit,
metadata = gadarian)
## And lets plot it using the included plotting function.
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 :)