```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
	cache=T)
library(quanteda)
library(dplyr)
library(stm)
```

This script follows the actions on the slides


## Preparing the data

```{r}
# data prep
gadarian_corpus <- corpus(gadarian,text_field="open.ended.response")
gadarian_toks <- tokens(gadarian_corpus,
                         remove_punct=T,
                         remove_numbers=T) %>%
  tokens_remove(stopwords("en"))
gadarian_dfm <- dfm(gadarian_toks)
```



## Estimating a topic model

```{r}
gad_stm_1 <- stm(gadarian_dfm,K=10,seed=2020)

```

## Interpreting results


```{r}
labelTopics(gad_stm_1)

# formatting the plot (optional)
par(bty="n",col="grey40",lwd=5)
# plotting
plot(gad_stm_1,topics=1:5,type="summary",xlim=c(0,0.2))

labelTopics(gad_stm_1,topics=3)

findThoughts(gad_stm_1, gadarian$open.ended.response, topics=3, n=10)

```

# Prevalence and Content covariates

```{r}
gad_stm_2 <- stm(gadarian_dfm,K=10,
                 prevalence=~treatment,
                 data=docvars(gadarian_dfm),verbose=F)

labelTopics(gad_stm_2)

gad_stm_3 <- stm(gadarian_dfm,K=10,
                 content=~treatment,
                 data=docvars(gadarian_dfm),verbose=F)
labelTopics(gad_stm_3,topics=3)
```

## Estimation of effects

```{r}
gad_est <- estimateEffect(1:10~treatment,
                          gad_stm_2,docvars(gadarian_dfm))
plot(gad_est,topics=1,covariate="treatment")

plot(gad_est,method="difference",cov.value1="0",
  cov.value2="1",covariate="treatment",topics=1)
```

## Validation

```{r}
stm_obj<- convert(gadarian_dfm,"stm")
ksearch<-searchK(stm_obj$documents,stm_obj$vocab,
                     K = seq(5, 15, by = 2), max.em.its = 15,verbose=F)
plot(ksearch)
```
