Category: nuggets

Dealing with “missing”/out of bounds values in heatmaps

I was tinkering around in R to see if I could plot better looking heatmaps, when I encountered an issue regarding how specific values are represented in plots with user-specified restricted ranges. When I’m plotting heatmaps, I usually want to restrict the range of the data being plotted in a specific way. In this case, I was plotting classifier accuracy across frequencies and time of Fourier-transformed EEG data, and I want to restrict the lowest value of the heatmap to be chance level (in this case, 50%).

I used the following code to generate a simple heatmap:

decoding <- readMat("/Users/Pablo/Desktop/testDecoding.mat")$plotData
plotDecoding <- melt(decoding)

quartz(width=5,height=4)
ggplot(plotDecoding, aes(x = Var2, y = Var1)) +
geom_tile(aes(fill = value)) +
scale_fill_viridis(name = "",limits = c(0.5,0.75))

And this is what I got:

heatmap1

See the gray areas? What’s happening here is that any data points that fall outside of my specified data range (see the limits argument in scale_fill_viridis) are being converted to NAs. Apparently, NA values are plotted as gray by default. Obviously these values are not missing in the actual data; how can we actually ensure that they're plotted? The solution comes from the scales package that comes with ggplot. Whenever you create a plot with specified limits, include the argument oob = squish (oob = out of bounds) in the same line where you set the limits (make sure that the scales package is loaded). Ex:

scale_fill_viridis(name = "",limits = c(0.5,0.75),oob=squish)

What this does is that values that fall below the specified range are represented as the minimum value (color, in this case), and values that fall above the range are represented as the maximum value (which seems to be what Matlab defaults to). The resulting heatmap looks like this:

heatmap2

A simple solution to an annoying little quirk in R!

Looking at interactions of continuous variables

So we had an unexpected interaction effect between two continuous predictors in our dataset. I thought it was some kind of ceiling effect so I did some plots to see what was causing it. I still think it’s a ceiling effect, but I also found it interesting that the effect of predictor 1 is only really present at average levels of predictor 2. Anyway, I’ve attached my code. If you have an interaction that you can’t quite understand, these plots may help: http://rpubs.com/abcharlton/interaction-plots.

download-1

download

Bringing in Qualtrics (and other data)

While a lot of us have grown comfortable using Excel to clean and manipulate our data, there is a growing trend toward transparency and reproduciblity that make it difficult to keep going down that road. It’s also just too easy to make a mistake with Excel. For example, someone I was helping out with an email campaign recently sent me an Excel file that was sorted only by one column. I just trusted them, rather than properly checking the file, and sent the email out and over 400 people got an email with the wrong name in the greeting. That was awkward and frustrating. For me, I love being able to look at my code, see where I brought in the raw data, and see all of the manipulations I did to clean it up. Excel doesn’t do that for me. Thanks to Hadley Wickham’s “dplyr” package it is surprisingly easy to manipulate data in R. I recommend printing out RStudio’s “Data Wrangling Cheat Sheet” and hanging it up somewhere visible if you do regularly manipulate data in R. Here is an example of some data manipulation that I recently did in R.

Step 1. Setup

I’ve set my working directory so that R knows what folder to retrieve the raw data files from. Alternatively, you can give R the whole file name including folders when you read in the CSV file and not bother setting a working directory.

setwd(“~/Research/PPAuthenticity/studies/Study1”)

I’ve loaded the “dplyr” package, which I installed earlier using the command install.packages(“dplyr”). One problem with dplyr is that it uses some function names that mean something different in base R or in other packages. I’ve run into a lot of errors and found that the best workaround is to simply tell R that when I say “select”, what I mean is use select from the dplyr package.

library(dplyr)
filter <- dplyr::filter
select <- dplyr::select

Step 2. Bring in Qualtrics data

Here are a couple of rules of thumb that I use:

  1. Only create one object per data file. It is really confusing to come back 6 months later and see that you have 15 objects that are different versions of the same dataset. I like to see only one version. Dplyr makes it easy to have only one object.
  2. I almost never refer to rows or columns by number. Column numbers and row numbers change every time you tweak the dataset. For best transparency, use some name or rule-based method to remove data or tweak it.

I’m going to read in the data as a CSV file. I recommend against trying to read it in as an Excel file. There are several packages that supposedly read Excel, but they don’t seem to have consistent performance, and there is no guarantee that your code will work later if you do read it in as Excel.

qualtrics <- read.csv("PPAuthenticity2SPAPR15.csv", stringsAsFactors = FALSE) %>%

Notice that I used the “stringsAsFactors = FALSE” argument. By default, R will try to turn everything into factors, which is generally not what I want at all. I used the pipe operator “%>%” to let R know that I’m not done. Now I’m going to make some changes to this data. The pipe operator comes included with dplyr.

In our lab, we have all of our grad students run through the surveys before the real participants do to test it. I want to get rid of the grad student responses, so I filter out all observations that don’t have a student ID that starts with “95”. The grad students are supposed to put “Test” in this field, though John for some reason puts “666”. Filtering out all student ID’s that don’t start with “95” takes care of all of these test observations. Again, it ends with a pipe so that R knows there is more. Because I’m using pipes, I don’t even have to tell it what data I want to execute this command on. It already knows to look at the previous pipe row.

filter(grepl(“^95”, ID)) %>%

An English translation of this would be “In the row above, filter out all results where ‘ID’ doesn’t start with ’95’.” Grepl matches the standard expression I want, and filter removes those rows. In Qualtrics there is a second header row with really long, unwieldy descriptions. This will remove that row too. If all you wanted to do was remove that row of labels, you could simply remove it by position when you bring it in. Normally I don’t like to refer to rows by number, but I don’t think it does any harm to only remove the first row:

read.csv(“yourfile.csv”)[-1, ] # this is alternative code that I’m not using for my analysis

I try to make good, proper names for my variables in Qualtrics, but they always seem to get messed up. I inevitably end up renaming some of them:

rename(adskep_2 = adskep_10,
adskep_3 = adskep_11,
adskep_4 = adskep_12,
adskep_5 = adskep_13,
adskep_6 = Q28_14,
adskep_7 = Q28_15,
adskep_8 = Q28_16,
adskep_9 = Q28_17,
Out_not_authentic = Symbol_5) %>%

Note that the name to the left of the equal sign is the new name. The name to the right is the messed up name that Qualtrics gave me.

Now, I’m telling R only to keep variables that have the stems I want:

select(matches(“cont|symbol|cred|integ|out|adskep|id|qpq”)) %>%

In plain English, this would say “keep only the columns that have ‘cont’ or ‘symbol’ or ‘cred’ or ‘integ’ or ‘out’ or ‘adskep’ or ‘id’ or ‘qpq’ as part of their name.”

All of my variables were read in as character strings, so I will need to transform relevant columns to numeric format:

mutate_each(funs(as.numeric), -ID) %>%

Using the “mutate_each” command from the dplyr package, I’ve transformed every column except for “ID” to numeric format.

I need a composite variable that is the mean of all variables from the four dimensions of my scale. You can use “mutate” to create a new variable.

mutate(authenticity = rowMeans(select(.,matches(“cont|Symbol|cred|Integ”)))) %>%

In Qualtrics, I ran two conditions. I need a factor variable that tells me which condition the person was in. Right now I have two variables representing the two conditions. Each is a string of 1’s and NA’s. I only need one of these variables to make my new variable since condition “qpq” and “bonus” are mutually exclusive.

mutate(condition = factor(.$qpq, labels=c(“qpq”,”bonus”), exclude=NULL)) %>%
  select(-qpq)

I created a new variable called “condition”, which is a factored version of “qpq”. When you create a factor in R, you can use the “exclude=NULL” argument to tell it that you want “NA” to be a factor level, rather than just representing missing data. Next, I used “select” to drop the “qpq” variable that has now become obsolete. Since I didn’t include a pipe operator at the end of my last command, all the code will now run and return my cleaned up data.

Step 3. Bring in a second dataset and merge it with the first

In our lab, we have students answer all of the demographic questions separately. We end up having to merge the data. This is ridiculously easy to do in R:

demos <- read.csv("Spring 2015 Demos UPPER 3-8.csv", stringsAsFactors = FALSE) %>%
   distinct(ID)
alldata <- left_join(qualtrics,demos, by="ID")

I’ve brought in the second dataset. People often end up filling out the demos multiple times for whatever reason. I don’t want duplicates because I will end up with duplicate data after I do the join. I have used the “distinct” function to get rid of redundant student ID’s. Then I used “left_join” to keep everything in my dataset on the left “qualtrics,” and to tack on data from my other dataset, “demos” wherever there is a match by the “ID” column, which both datasets have in this case. Again, it’s pretty easy to join two datasets.

The output of this process is three objects:

  1. qualtrics
  2. demos
  3. alldata

There is no data1, data2, data3, etc. Very clean, very transparent, and very easy to look back and see exactly what I did.

Here is all of the code in one place:

Setting up a lovely new project

I’ve been doing two things recently that make me want to improve my workflow: starting new projects, and trying to revive old ones.

I remember back in the day when I first started using R instead of point-and-click stuff in SPSS or excel, and I was SO PROUD that my work was all in code, super reproducible. I also remember when I read my first book all by myself, and I was so proud then, too. How standards change, eh? My ability to read Amelia Bedelia cover to cover no longer seems as impressive, and – not unlike the work of that bumbling parlor maid – my old code now looks more like a series of misunderstandings and cryptic jokes than clean, reproducible analyses.

Looking back through my old projects and (gasp!) needing to share old code with new collaborators has brought to light some common problems:

1) Where even is that R script?

2) This code totally worked the last time I used it, and it simply doesn’t now.

3) Is this the code I used for that conference poster, or was it another version? Wasn’t there some code in here for sequence plots? Where did that go?

In an attempt to solve these problems* for Future Rosie, I’m setting up my new projects much better. We’ve talked about tools for workflow and reproducible code on this blog before, and of course there’s lots of tutorials for this online. This is just my version.

* Note that the “where even is that r script” problem is not really solved by nifty reproducible code tools. You just need to pick a naming convention and file organization system and then effing stick to it (I’m looking at you, Past Rosie!).

Setting up a pretty project

I’m using R studio and git. These are the important pieces for my own workflow (solving problems 2 and 3 above). I also use github to host my code, so it’s easy to share, etc. It’s also a nice way to keep track of my to-do list for each project (via “issues”). You don’t need to set up a github account to take advantage of git version control, though, and if you start a git project in Rstudio without associating it with a github account, you can always add it later. So definitely set up git, and connect it to github if you want.

Install stuff

If you haven’t already, you’ll need to install Rstudio (https://www.rstudio.com/products/rstudio/download/) and git (http://git-scm.com/downloads).

Set up your project

Open Rstudio and click File>New Project… Then select “start new directory” and then “empty project.” Type in the name of the project (this will be the folder name). Select “create a git repository” to use version control, and select “use packrat* with this project” to save all of the useful information about which versions of every package you need.

Screenshot 2015-09-21 12.37.16

*”What is packrat,” you ask? Excellent question. It keeps track of which versions of all of your R packages are used for a project, so you can run old code with old package versions if you need to. Read about it and see if you want to use it.

Using git

In your new, empty project, you’ll notice a couple things: There’s a new tab by Environment and History

Screenshot 2015-09-21 12.40.02

and there are some automatically generated files hanging out in your directory.

Screenshot 2015-09-21 12.39.53

When you use git for version control, you save “commits”, which are like snapshots of your work. I recommend you commit every time you’re done adding something or making some change to your code. You’ll save a short message with each commit describing what you’ve done, so I recommend you commit every time you feel like you could describe your work with a pithy little message. For example, “added code for histograms of age and PPVT” is good, as is “fixed typos in intro paragraph”, “deleted sequence plots”, or “switched dataframe transformations to tidyr functions”. I like to wait to commit until I have working code, not before (so something like “trying to get LDA to work”, “still trying to get LDA to work” etc. are probably not good commits). Your commits will be the history of your project, and you’ll be able to click on each commit to see what your code looked like then. Committing is not the same as saving – I recommend you save compulsively, like every second, whether you’ve done anything worthwhile or not. Saving will update the file(s) on your computer (for example, the .r file in your working directory). Committing is a way to preserve snapshots of your project at useful or interesting points, so you (or someone else) can go back and look at it later.

Your first commit is usually just to start the project. I generally just include the message “init” for my first commit. Committing in Rstudio is a breeze. 🙂 Just click the little boxes next to each of the files you want to commit, and then click “commit”

Screenshot 2015-09-21 13.53.47

This opens the git window in Rstudio, where you type your commit message. The bottom shows you any changes, line by line, in your files from the previous commit (green means lines added – it’s all green now since these are new files being added). This is great for helping you remember what you want to write in your message. Neat, huh?

Screenshot 2015-09-21 13.55.07

Then hit “commit”! Bam. 🙂 You can also use this window to browse through your previous commits, which is nice when you’re looking for an older version of something or you want to see how or when your code changed.

Note: Sometimes I get the following error message when I try to commit:

Screenshot 2015-09-21 13.58.28

I’m not sure why. But it’s easy to fix: Close this popup, then hit “refresh” on the git window, then hit “commit” again. That usually works for me.

When you’re done checking out your git stuff, just close the git window and go back to regular Rstudio land.

Which files to commit?

Note that I committed several files above:

  1. the .Rprofile (you can use this to save options and preferences and stuff),
  2. .gitignore (more on this below)
  3. .Rproj file (roughly, this one saves my current state in Rstudio, including history, any variables I’ve made, packages loaded, dataframes open, etc. You may or may not want to version control this guy, depending on what you’re doing)
  4. all the packrat stuff (you can read about packrat here)
  5. If I had text files I was working on for this project (for me, they’re usually .r, .md, or .rmd files) I would have committed those, too.

The .gitignore file is important. It’s a list of stuff you DON’T want version controlled. For example, if you have some other stuff in this directory (slides from a conference presentation of this project, stimuli from the study, data, etc.) you can tell git to not bother version-controlling that stuff. Version control works best on plain text files (.txt, .r, .md, .rmd, .tex, .html, .csv, etc.), so I don’t recommend you try to version control other stuff (any mircosoft office files, images, movies, sound files, etc.).

I have an example pdf in my directory, so I’m going to add that to my .gitignore list. Click on it, then click the settings wheel, and click “ignore”. This will add it to the .gitignore list. It’s also possible to edit the .gitignore list yourself (it’s just a text file, so you can type right in it).

Screenshot 2015-09-21 14.08.02

Screenshot 2015-09-21 14.08.12

You’ll see that there are a couple things automatically added to the .gitignore list for you when you start a project in Rstudio this way, and now my .pdf file is added to that list! Joy.

When you look at your git window, you’ll notice the .gitignore file is there:

Screenshot 2015-09-21 14.10.37

That’s because it’s been changed, and the change has not yet been committed.  You can click the box next to it, then hit “commit” to commit that change now (your message might be something like “added old .pdf from previous workshop to .gitignore”).

Note that if you use a Mac, you might also see a .DS_Store file in your directory. This is just your computer keeping track of folder preferences, etc. You most likely don’t need to version control this, so add it to your .gitignore list as well.

Want to combine this awesomeness with a github repo?

Damn straight you do. You can use SSH to connect your RStudio project git stuff to your remote github repository. For example, here’s a repo of mine that connects to an RStudio project I have stored on my laptop: https://github.com/rosemm/rexamples This means that I have all of the lovely version control stuff going on in RStudio when I work on this code AND I can make all of that publicly available so cool, smart people like John can contribute to my code! It’s also a handy way to share code with others – I can just send them a link to my github repo, and they have everything there at their fingertips (and if I update it, they’ll always have access to the most recent version, as well as all of the old commits in case they want an old version).

If you’re new to github, check out the extremely excellent materials available in Jenny Bryan‘s course at UBC: http://stat545.com/git00_index.html. And here is a much quicker and less comprehensive resource: http://www.r-bloggers.com/rstudio-and-github/.

Plotting logistic regressions, Part 3

If you haven’t already, check out plotting logistic regression part 1 (continuous by categorical interactions) and part 2 (continuous by continuous interactions).

All of this code is available on Rose’s github: https://github.com/rosemm/rexamples/blob/master/logistic_regression_plotting_part3.Rmd









Plotting the results of your logistic regression Part 3: 3-way interactions

If you can interpret a 3-way interaction without plotting it, go find a mirror and give yourself a big sexy wink. wink That’s impressive.

For the rest of us, looking at plots will make understanding the model and results so much easier. And even if you are one of those lucky analysts with the working memory capacity of a super computer, you may want this code so you can use plots to help communicate a 3-way interaction to your readers.

Use the model from the Part 1 code.

Here’s that model:

summary(model)
## 
## Call:
## glm(formula = DV ~ (X1 + X2 + group)^2, family = "binomial", 
##     data = data, na.action = "na.exclude")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.44094  -0.45991   0.04136   0.52301   2.74705  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -0.5873     0.3438  -1.708 0.087631 .  
## X1            2.6508     0.5592   4.740 2.13e-06 ***
## X2           -2.2599     0.4977  -4.540 5.61e-06 ***
## groupb        2.2111     0.5949   3.717 0.000202 ***
## groupc        0.6650     0.4131   1.610 0.107456    
## X1:X2         0.1201     0.2660   0.452 0.651534    
## X1:groupb     2.7323     1.2977   2.105 0.035253 *  
## X1:groupc    -0.6816     0.7078  -0.963 0.335531    
## X2:groupb     0.8477     0.7320   1.158 0.246882    
## X2:groupc     0.4683     0.6558   0.714 0.475165    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 412.88  on 299  degrees of freedom
## Residual deviance: 205.46  on 290  degrees of freedom
## AIC: 225.46
## 
## Number of Fisher Scoring iterations: 7

Let’s add a 3-way interaction. Instead of re-running the whole model, we can use the nifty update() function. This will make the change to the model (adding the 3-way interaction), and automatically refit the whole thing. (It is also fine to just re-run the model — you’ll get the exact same results. I just wanted to show off the update() function.)

new.model <- update(model, ~ . + X1:X2:group) # the . stands in for the whole formula we had before

# if you wanted to specify the whole model from scratch instead of using update():
new.model <- glm(DV ~ X1*X2*group, 
             data=data, na.action="na.exclude",  family="binomial")  

summary(new.model)
## 
## Call:
## glm(formula = DV ~ X1 * X2 * group, family = "binomial", data = data, 
##     na.action = "na.exclude")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.36818  -0.39475   0.02394   0.45860   2.82512  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   -0.8959     0.4037  -2.219 0.026491 *  
## X1             2.7511     0.5914   4.652 3.29e-06 ***
## X2            -2.3070     0.5076  -4.545 5.49e-06 ***
## groupb         2.5457     0.6776   3.757 0.000172 ***
## groupc         1.3403     0.5201   2.577 0.009958 ** 
## X1:X2          0.6779     0.4314   1.572 0.116057    
## X1:groupb      3.8321     1.6589   2.310 0.020882 *  
## X1:groupc     -0.6709     0.7412  -0.905 0.365349    
## X2:groupb      1.1732     0.7623   1.539 0.123806    
## X2:groupc      0.1871     0.6975   0.268 0.788483    
## X1:X2:groupb   1.1108     0.8198   1.355 0.175458    
## X1:X2:groupc  -1.4068     0.5899  -2.385 0.017082 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 412.88  on 299  degrees of freedom
## Residual deviance: 194.51  on 288  degrees of freedom
## AIC: 218.51
## 
## Number of Fisher Scoring iterations: 7

Calculate probabilities for the plot

Again, we’ll put X1 on the x-axis. That’s the only variable we’ll enter as a whole range.

X1_range <- seq(from=min(data$X1), to=max(data$X1), by=.01)

Next, compute the equations for each line in logit terms.

Pick some representative values for the other continuous variable

Just like last time, we’ll plug in some representative values for X2, so we can have separate lines for each representative level of X2.

X2_l <- mean(data$X2) - sd(data$X2) 
X2_m <- mean(data$X2)
X2_h <- mean(data$X2) + sd(data$X2)
# check that your representative values actually fall within the observed range for that variable
summary(data$X2)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -3.52900 -0.70950 -0.02922 -0.04995  0.67260  2.38000
c(X2_l, X2_m, X2_h)
## [1] -1.0621963 -0.0499475  0.9623013

Now we can go ahead and plug those values into the rest of the equation to get the expected logits across the range of X1 for each of our “groups” (hypothetical low X2 people, hypothetical average X2 people, hypothetical high X2 people). We’ll also plug in the dummy codes for each of the three groups (a, b, and c). And we’ll calculate the predicted probabilities of the DV for each combination of X2 level and group.

But instead of literally writing out all of the equations (9 of them!!), we’ll just use the fun-and-easy predict() function.

If you ran your model in SPSS, so you only have the coefficients and not the whole model as an R object, you can still make the plots — you just need to spend some quality time writing out those equations. For examples of how to do this (for just 3 equations, but you get the idea) see Part 1 and Part 2 in this series.

To use predict(), you make a new data frame with the predictor values you want to use (i.e. the whole range for X1, group a, and the representative values we picked for X2), and then when you run predict() on it, for each row in the data frame it will generate the predicted value for your DV from the model you saved. The expand.grid() function is a quick and easy way to make a data frame out of all possible combinations of the variables provided. Perfect for this situation!

#make a new data frame with the X values you want to predict 
generated_data <- as.data.frame(expand.grid(X1=X1_range, X2=c(X2_l, X2_m, X2_h), group=c("a", "b", "c") ))
head(generated_data)
##          X1        X2 group
## 1 -2.770265 -1.062196     a
## 2 -2.760265 -1.062196     a
## 3 -2.750265 -1.062196     a
## 4 -2.740265 -1.062196     a
## 5 -2.730265 -1.062196     a
## 6 -2.720265 -1.062196     a
#use `predict` to get the probability using type='response' rather than 'link' 
generated_data$prob <- predict(new.model, newdata=generated_data, type = 'response')
head(generated_data) 
##          X1        X2 group       prob
## 1 -2.770265 -1.062196     a 0.01675787
## 2 -2.760265 -1.062196     a 0.01709584
## 3 -2.750265 -1.062196     a 0.01744050
## 4 -2.740265 -1.062196     a 0.01779198
## 5 -2.730265 -1.062196     a 0.01815042
## 6 -2.720265 -1.062196     a 0.01851595
# let's make a factor version of X2, so we can do gorgeous plotting stuff with it later :)
generated_data$X2_level <- factor(generated_data$X2, labels=c("low (-1SD)", "mean", "high (+1SD)"), ordered=T)
summary(generated_data)
##        X1                 X2           group         prob        
##  Min.   :-2.77027   Min.   :-1.06220   a:1683   Min.   :0.00000  
##  1st Qu.:-1.37027   1st Qu.:-1.06220   b:1683   1st Qu.:0.02416  
##  Median : 0.02974   Median :-0.04995   c:1683   Median :0.56304  
##  Mean   : 0.02974   Mean   :-0.04995            Mean   :0.51727  
##  3rd Qu.: 1.42973   3rd Qu.: 0.96230            3rd Qu.:0.99162  
##  Max.   : 2.82973   Max.   : 0.96230            Max.   :1.00000  
##         X2_level   
##  low (-1SD) :1683  
##  mean       :1683  
##  high (+1SD):1683  
##                    
##                    
## 

Plot time!

This kind of situation is exactly when ggplot2 really shines. We want multiple plots, with multiple lines on each plot. Of course, this is totally possible in base R (see Part 1 and Part 2 for examples), but it is so much easier in ggplot2. To do this in base R, you would need to generate a plot with one line (e.g. group a, low X2), then add the additional lines one at a time (group a, mean X2; group a, high X2), then generate a new plot (group b, low X2), then add two more lines, then generate a new plot, then add two more lines. Sigh.

Not to go down too much of a rabbit hole, but this illustrates what is (in my opinion) the main difference between base R graphics and ggplot2: base graphics are built for drawing, whereas ggplot is built for visualizing data. It’s the difference between specifying each line and drawing them on your plot vs. giving a whole data frame to the plotting function and telling it which variables to use and how. Depending on your needs and preferences, base graphics or ggplot may be a better choice for you. For plotting complex model output, like a 3-way interaction, I think you’ll generally find that ggplot2 saves the day.

library(ggplot2)

plot.data <- generated_data

# check out your plotting data
head(plot.data)
##          X1        X2 group       prob   X2_level
## 1 -2.770265 -1.062196     a 0.01675787 low (-1SD)
## 2 -2.760265 -1.062196     a 0.01709584 low (-1SD)
## 3 -2.750265 -1.062196     a 0.01744050 low (-1SD)
## 4 -2.740265 -1.062196     a 0.01779198 low (-1SD)
## 5 -2.730265 -1.062196     a 0.01815042 low (-1SD)
## 6 -2.720265 -1.062196     a 0.01851595 low (-1SD)
ggplot(plot.data, aes(x=X1, y=prob, color=X2_level)) + 
  geom_line(lwd=2) + 
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome") +
  facet_wrap(~group) # i love facet_wrap()! it's so great. you should fall in love, too, and use it all the time.

# let's try flipping it, so the facets are by X2 level and the lines are by group
ggplot(plot.data, aes(x=X1, y=prob, color=group)) + 
  geom_line(lwd=2) + 
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome") +
  facet_wrap(~X2_level) 

# want it all on one plot? you can set the color to the interaction of group and X2_level:
ggplot(plot.data, aes(x=X1, y=prob, color=group:X2_level)) + 
  geom_line(lwd=2) + 
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome")

For something like this, you may want to manually specify the colors, to make the plot easier to read. For more details about manually setting colors in ggplot2, see this R Club post.

library(RColorBrewer)
# pick some nice colors. I want all shades of red for a, green for b, and blue for c.
a_colors <- brewer.pal(9,"Reds")[c(4,6,9)] # I'm getting all 9 shades and just picking the 3 I want to use 
b_colors <- brewer.pal(9,"Greens")[c(4,6,9)]
c_colors <- brewer.pal(9,"Blues")[c(4,6,9)] 
colors <- c(a_colors, b_colors, c_colors)
colors # this is how R saves color values
## [1] "#FC9272" "#EF3B2C" "#67000D" "#A1D99B" "#41AB5D" "#00441B" "#9ECAE1"
## [8] "#4292C6" "#08306B"
ggplot(plot.data, aes(x=X1, y=prob, color=group:X2_level)) + 
  geom_line(lwd=2) + 
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome") +
  scale_color_manual(values=colors) 

# you can also change the line type based on a factor
ggplot(plot.data, aes(x=X1, y=prob, color=group:X2_level)) + 
  geom_line(aes(linetype=X2_level), lwd=2) + # because linetype is instead aes(), it can vary according to the data (i.e. by X2_level)
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome") +
  scale_color_manual(values=colors) 



reformatting data to wide

Here’s some thing we were working on in R Club today.

This example shows how to reformat data from (sort of) long to wide. We’ve got pre and post measurements for each subject, some subject-level variables (called dyad vars in the example), and two variables that vary by pre and post. These are the outcome of interest (DV) and a covariate (the length of the video). The data is “sort of” long in that pre and post are each on their own line, but the type of measure (DV or video length) is still each in its own column. To make it fully wide, where there’s only one line for each subject, first we need to make it fully long, and then spread it all out. I’m using dplyr and tidyr here since they’re such lovely tools, but of course there’s more than one way to go about this.

Enjoy!









Here’s the whole thing:

data <- data.frame(subj=sort(c(1:10, 1:10)), cond=rep(c("pre", "post"), 10), DV=rnorm(20), video=rnorm(20), dyadvar1=sort(c(1:10, 1:10)), dyadvar2=sort(c(1:10, 1:10)))

data
##    subj cond          DV      video dyadvar1 dyadvar2
## 1     1  pre -0.09424282 -0.5553683        1        1
## 2     1 post  1.50787647  0.1086909        1        1
## 3     2  pre  1.63471884 -0.5022278        2        2
## 4     2 post  0.06910946 -0.1067023        2        2
## 5     3  pre  0.60311557 -0.3074021        3        3
## 6     3 post  1.34918285  1.0210010        3        3
## 7     4  pre -0.52288040  0.3636248        4        4
## 8     4 post -1.14408452  0.9635112        4        4
## 9     5  pre -1.67596739 -0.5665085        5        5
## 10    5 post  0.45287833  0.1465894        5        5
## 11    6  pre  0.96739333 -0.2250043        6        6
## 12    6 post  0.20794296  1.0133354        6        6
## 13    7  pre  1.25381345 -1.0679324        7        7
## 14    7 post  0.73957481 -1.3426321        7        7
## 15    8  pre -1.26477564 -0.4130490        8        8
## 16    8 post -1.80489529 -0.5602744        8        8
## 17    9  pre -0.85829518  0.8735288        9        9
## 18    9 post  0.52721178  0.1642551        9        9
## 19   10  pre  0.08717665 -0.3085623       10       10
## 20   10 post -1.28951524 -1.7575259       10       10
library(tidyr); library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
wide.data <- data %>%
  gather(key="measure", value="value", DV:video) %>%
  unite(col=key, cond, measure)  %>%
  spread(key=key, value=value)

Here’s each step:

library(knitr)
data %>%
  gather(key="measure", value="value", DV:video)
##    subj cond dyadvar1 dyadvar2 measure       value
## 1     1  pre        1        1      DV -0.09424282
## 2     1 post        1        1      DV  1.50787647
## 3     2  pre        2        2      DV  1.63471884
## 4     2 post        2        2      DV  0.06910946
## 5     3  pre        3        3      DV  0.60311557
## 6     3 post        3        3      DV  1.34918285
## 7     4  pre        4        4      DV -0.52288040
## 8     4 post        4        4      DV -1.14408452
## 9     5  pre        5        5      DV -1.67596739
## 10    5 post        5        5      DV  0.45287833
## 11    6  pre        6        6      DV  0.96739333
## 12    6 post        6        6      DV  0.20794296
## 13    7  pre        7        7      DV  1.25381345
## 14    7 post        7        7      DV  0.73957481
## 15    8  pre        8        8      DV -1.26477564
## 16    8 post        8        8      DV -1.80489529
## 17    9  pre        9        9      DV -0.85829518
## 18    9 post        9        9      DV  0.52721178
## 19   10  pre       10       10      DV  0.08717665
## 20   10 post       10       10      DV -1.28951524
## 21    1  pre        1        1   video -0.55536830
## 22    1 post        1        1   video  0.10869087
## 23    2  pre        2        2   video -0.50222782
## 24    2 post        2        2   video -0.10670226
## 25    3  pre        3        3   video -0.30740210
## 26    3 post        3        3   video  1.02100096
## 27    4  pre        4        4   video  0.36362482
## 28    4 post        4        4   video  0.96351116
## 29    5  pre        5        5   video -0.56650848
## 30    5 post        5        5   video  0.14658942
## 31    6  pre        6        6   video -0.22500427
## 32    6 post        6        6   video  1.01333537
## 33    7  pre        7        7   video -1.06793244
## 34    7 post        7        7   video -1.34263213
## 35    8  pre        8        8   video -0.41304900
## 36    8 post        8        8   video -0.56027441
## 37    9  pre        9        9   video  0.87352882
## 38    9 post        9        9   video  0.16425512
## 39   10  pre       10       10   video -0.30856231
## 40   10 post       10       10   video -1.75752594
data %>%
  gather(key="measure", value="value", DV:video) %>%
  unite(col=key, cond, measure)
##    subj        key dyadvar1 dyadvar2       value
## 1     1     pre_DV        1        1 -0.09424282
## 2     1    post_DV        1        1  1.50787647
## 3     2     pre_DV        2        2  1.63471884
## 4     2    post_DV        2        2  0.06910946
## 5     3     pre_DV        3        3  0.60311557
## 6     3    post_DV        3        3  1.34918285
## 7     4     pre_DV        4        4 -0.52288040
## 8     4    post_DV        4        4 -1.14408452
## 9     5     pre_DV        5        5 -1.67596739
## 10    5    post_DV        5        5  0.45287833
## 11    6     pre_DV        6        6  0.96739333
## 12    6    post_DV        6        6  0.20794296
## 13    7     pre_DV        7        7  1.25381345
## 14    7    post_DV        7        7  0.73957481
## 15    8     pre_DV        8        8 -1.26477564
## 16    8    post_DV        8        8 -1.80489529
## 17    9     pre_DV        9        9 -0.85829518
## 18    9    post_DV        9        9  0.52721178
## 19   10     pre_DV       10       10  0.08717665
## 20   10    post_DV       10       10 -1.28951524
## 21    1  pre_video        1        1 -0.55536830
## 22    1 post_video        1        1  0.10869087
## 23    2  pre_video        2        2 -0.50222782
## 24    2 post_video        2        2 -0.10670226
## 25    3  pre_video        3        3 -0.30740210
## 26    3 post_video        3        3  1.02100096
## 27    4  pre_video        4        4  0.36362482
## 28    4 post_video        4        4  0.96351116
## 29    5  pre_video        5        5 -0.56650848
## 30    5 post_video        5        5  0.14658942
## 31    6  pre_video        6        6 -0.22500427
## 32    6 post_video        6        6  1.01333537
## 33    7  pre_video        7        7 -1.06793244
## 34    7 post_video        7        7 -1.34263213
## 35    8  pre_video        8        8 -0.41304900
## 36    8 post_video        8        8 -0.56027441
## 37    9  pre_video        9        9  0.87352882
## 38    9 post_video        9        9  0.16425512
## 39   10  pre_video       10       10 -0.30856231
## 40   10 post_video       10       10 -1.75752594
data %>%
  gather(key="measure", value="value", DV:video) %>%
  unite(col=key, cond, measure)  %>%
  spread(key=key, value=value)
##    subj dyadvar1 dyadvar2     post_DV post_video      pre_DV  pre_video
## 1     1        1        1  1.50787647  0.1086909 -0.09424282 -0.5553683
## 2     2        2        2  0.06910946 -0.1067023  1.63471884 -0.5022278
## 3     3        3        3  1.34918285  1.0210010  0.60311557 -0.3074021
## 4     4        4        4 -1.14408452  0.9635112 -0.52288040  0.3636248
## 5     5        5        5  0.45287833  0.1465894 -1.67596739 -0.5665085
## 6     6        6        6  0.20794296  1.0133354  0.96739333 -0.2250043
## 7     7        7        7  0.73957481 -1.3426321  1.25381345 -1.0679324
## 8     8        8        8 -1.80489529 -0.5602744 -1.26477564 -0.4130490
## 9     9        9        9  0.52721178  0.1642551 -0.85829518  0.8735288
## 10   10       10       10 -1.28951524 -1.7575259  0.08717665 -0.3085623



Plotting logistic regression models, part 2

If you haven’t already, check out plotting logistic regression part 1 (continuous by categorical interactions).

All of this code is available on Rose’s github: https://github.com/rosemm/rexamples/blob/master/logistic_regression_plotting_part2.Rmd









Plotting the results of your logistic regression Part 2: Continuous by continuous interaction

Last time, we ran a nice, complicated logistic regression and made a plot of the a continuous by categorical interaction. This time, we’ll use the same model, but plot the interaction between the two continuous predictors instead, which is a little weirder (hence part 2).

Use the model from the Part 1 code.

Here’s that model:

summary(model)
## 
## Call:
## glm(formula = DV ~ (X1 + X2 + group)^2, family = "binomial", 
##     data = data, na.action = "na.exclude")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.44094  -0.45991   0.04136   0.52301   2.74705  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -0.5873     0.3438  -1.708 0.087631 .  
## X1            2.6508     0.5592   4.740 2.13e-06 ***
## X2           -2.2599     0.4977  -4.540 5.61e-06 ***
## groupb        2.2111     0.5949   3.717 0.000202 ***
## groupc        0.6650     0.4131   1.610 0.107456    
## X1:X2         0.1201     0.2660   0.452 0.651534    
## X1:groupb     2.7323     1.2977   2.105 0.035253 *  
## X1:groupc    -0.6816     0.7078  -0.963 0.335531    
## X2:groupb     0.8477     0.7320   1.158 0.246882    
## X2:groupc     0.4683     0.6558   0.714 0.475165    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 412.88  on 299  degrees of freedom
## Residual deviance: 205.46  on 290  degrees of freedom
## AIC: 225.46
## 
## Number of Fisher Scoring iterations: 7

And we already saved the coefficients individually for use in the equations last time.

Calculate probabilities for the plot

Again, we’ll put X1 on the x-axis. That’s the only variable we’ll enter as a whole range.

X1_range <- seq(from=min(data$X1), to=max(data$X1), by=.01)

Next, compute the equations for each line in logit terms.

Pick some representative values for the other continuous variable

Just like last time, we’ll need to plug in values for all but one variable (X1, which is going on the x-axis of the plot), but this time we’ll pick some representative values for the other continuous predictor, X2, and plug those in to get a separate line for each representative value of X2. Typical choices are high (1SD above the mean), medium (the mean), and low (1SD below the mean) X2. Another great choice is max, median, and min. You can also do 4 or 5 lines instead of just 3, if you want. It’s your party.

Whatever you decide, I recommend checking to make sure the “representative” values you’re plugging in actually make sense given your data. For example, you may not actually have any cases with X2 value 1SD above the mean, in which case maybe you just want to put in max(X2) for the high case instead. It’s kinda weird to plot your models at values that don’t actually exist in your data (cue Twilight Zone music).

summary(data$X2)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -3.52900 -0.70950 -0.02922 -0.04995  0.67260  2.38000
(X2_l <- mean(data$X2) - sd(data$X2) )
## [1] -1.062196
(X2_m <- mean(data$X2) )
## [1] -0.0499475
(X2_h <- mean(data$X2) + sd(data$X2) )
## [1] 0.9623013

Now we can go ahead and plug those values into the rest of the equation to get the expected logits across the range of X1 for each of our “groups” (hypothetical low X2 people, hypothetical average X2 people, hypothetical high X2 people).

If you ran your model in SPSS and you just have the coefficients…

You’ll need to actually calculate the predicted probabilities yourself. Write out the equation for your model and plug in values for everything except the variable that will go on the x-axis.

Remember, these equations need to include every coefficient for the model you ran, whether or not you actually care about plotting them.

In this case, lots of it will just drop out because I’ll be plugging in 0’s for all of the dummy codes (we’re only looking at group a), but I encourage you to keep the terms in your code, so you don’t forget that all of those predictors are still in your model, you’re just holding them constant while you plot. We’re not interested in plotting the categorical predictor right now, but it’s still there in the model, so we need to just pick a group from it and enter the dummy codes for it. The plot will show us the interaction between X1 and X2 for the reference group (for 3-way interactions, you’ll have to wait for part 3!).

X2_l_logits <- b0 + 
  X1*X1_range + 
  X2*X2_l + 
  groupb*0 + 
  groupc*0 + 
  X1.X2*X1_range*X2_l + 
  X1.groupb*X1_range*0 + 
  X1.groupc*X1_range*0 + 
  X2.groupb*X2_l*0 + 
  X2.groupc*X2_l*0 

X2_m_logits <- b0 + 
  X1*X1_range + 
  X2*X2_m + 
  groupb*0 + 
  groupc*0 + 
  X1.X2*X1_range*X2_m + 
  X1.groupb*X1_range*0 + 
  X1.groupc*X1_range*0 + 
  X2.groupb*X2_m*0 + 
  X2.groupc*X2_m*0 

X2_h_logits <- b0 + 
  X1*X1_range + 
  X2*X2_h + 
  groupb*0 + 
  groupc*0 + 
  X1.X2*X1_range*X2_h + 
  X1.groupb*X1_range*0 + 
  X1.groupc*X1_range*0 + 
  X2.groupb*X2_h*0 + 
  X2.groupc*X2_h*0 

# Compute the probibilities (this is what will actually get plotted):
X2_l_probs <- exp(X2_l_logits)/(1 + exp(X2_l_logits))
X2_m_probs <- exp(X2_m_logits)/(1 + exp(X2_m_logits))
X2_h_probs <- exp(X2_h_logits)/(1 + exp(X2_h_logits))

If you ran your model in R, then you can just use predict()

Easy peasy! And, most importantly, less typing — which means fewer errors. Thanks to John for reminding me of this handy function! You make a new data frame with the predictor values you want to use (i.e. the whole range for X1, group a, and the representative values we picked for X2), and then when you run predict() on it, for each row in the data frame it will generate the predicted value for your DV from the model you saved. The expand.grid() function is a quick and easy way to make a data frame out of all possible combinations of the variables provided. Perfect for this situation!

#make a new data frame with the X values you want to predict 
generated_data <- as.data.frame(expand.grid(X1=X1_range, X2=c(X2_l, X2_m, X2_h), group="a") )
head(generated_data)
##          X1        X2 group
## 1 -2.770265 -1.062196     a
## 2 -2.760265 -1.062196     a
## 3 -2.750265 -1.062196     a
## 4 -2.740265 -1.062196     a
## 5 -2.730265 -1.062196     a
## 6 -2.720265 -1.062196     a
summary(generated_data)
##        X1                 X2           group   
##  Min.   :-2.77027   Min.   :-1.06220   a:1683  
##  1st Qu.:-1.37027   1st Qu.:-1.06220           
##  Median : 0.02974   Median :-0.04995           
##  Mean   : 0.02974   Mean   :-0.04995           
##  3rd Qu.: 1.42973   3rd Qu.: 0.96230           
##  Max.   : 2.82973   Max.   : 0.96230
#use `predict` to get the probability using type='response' rather than 'link' 
generated_data$prob <- predict(model, newdata=generated_data, type = 'response')
head(generated_data) 
##          X1        X2 group        prob
## 1 -2.770265 -1.062196     a 0.005614200
## 2 -2.760265 -1.062196     a 0.005756836
## 3 -2.750265 -1.062196     a 0.005903074
## 4 -2.740265 -1.062196     a 0.006053005
## 5 -2.730265 -1.062196     a 0.006206719
## 6 -2.720265 -1.062196     a 0.006364312
# let's make a factor version of X2, so we can do gorgeous plotting stuff with it later :)
generated_data$X2_level <- factor(generated_data$X2, labels=c("low (-1SD)", "mean", "high (+1SD)"), ordered=T)
head(generated_data) 
##          X1        X2 group        prob   X2_level
## 1 -2.770265 -1.062196     a 0.005614200 low (-1SD)
## 2 -2.760265 -1.062196     a 0.005756836 low (-1SD)
## 3 -2.750265 -1.062196     a 0.005903074 low (-1SD)
## 4 -2.740265 -1.062196     a 0.006053005 low (-1SD)
## 5 -2.730265 -1.062196     a 0.006206719 low (-1SD)
## 6 -2.720265 -1.062196     a 0.006364312 low (-1SD)

Plot time!

In base R…

# We'll start by plotting the low X2 group:
plot(X1_range, X2_l_probs, 
     ylim=c(0,1),
     type="l", 
     lwd=3, 
     lty=2, 
     col="red", 
     xlab="X1", ylab="P(outcome)", main="Probability of super important outcome")


# Add the line for mean X2
lines(X1_range, X2_m_probs, 
      type="l", 
      lwd=3, 
      lty=3, 
      col="green")

# Add the line for high X2
lines(X1_range, X2_h_probs, 
      type="l", 
      lwd=3, 
      lty=4, 
      col="blue")

# add a horizontal line at p=.5
abline(h=.5, lty=2)

Or, you can do it in ggplot2!

library(ggplot2); library(tidyr)
# first you have to get the information into a long dataframe, which is what ggplot likes :)

# if you calculated the predicted probabilities by writing out the equations, you can combine that all into a dataframe now, and use gather() to make it long
plot.data <- data.frame(low=X2_l_probs, mean=X2_m_probs, high=X2_h_probs, X1=X1_range)
plot.data <- gather(plot.data, key=X2_level, value=prob, -X1) # this means gather all of the columns except X1

# if you used predict(), then everything is already in a nice dataframe for you
plot.data <- generated_data

# check out your plotting data
head(plot.data)
##          X1        X2 group        prob   X2_level
## 1 -2.770265 -1.062196     a 0.005614200 low (-1SD)
## 2 -2.760265 -1.062196     a 0.005756836 low (-1SD)
## 3 -2.750265 -1.062196     a 0.005903074 low (-1SD)
## 4 -2.740265 -1.062196     a 0.006053005 low (-1SD)
## 5 -2.730265 -1.062196     a 0.006206719 low (-1SD)
## 6 -2.720265 -1.062196     a 0.006364312 low (-1SD)
ggplot(plot.data, aes(x=X1, y=prob, color=X2_level)) + 
  geom_line(lwd=2) + 
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome") 



Plotting your logistic regression models

This code is all available on Rose’s github: https://github.com/rosemm/rexamples/blob/master/logistic_regression_plotting_part1.Rmd






Plotting the results of your logistic regression Part 1: Continuous by categorical interaction

We’ll run a nice, complicated logistic regresison and then make a plot that highlights a continuous by categorical interaction.

set.seed(24601) # setting this so the random results will be repeatable 

library(MASS)
covmat <- matrix(c(1.0,   0.2,   0.6, 
                   0.2,   1.0,  -0.5, 
                   0.6,  -0.5,   1.0), nrow=3) # the true cov matrix for my data
data <- mvrnorm(300, mu=c(0,0,0), Sigma=covmat) # generate random data that match that cov matrix
colnames(data) <- c("X1", "X2", "DV")
data <- as.data.frame(data)
data$group <- gl(n=3, k=ceiling(nrow(data)/3), labels=c("a", "b", "c", "d"))
# add some group differences and interaction stuff...
data$DV <- with(data, ifelse(group=="c" & X1 > 0, DV+rnorm(n=1, mean=1), 
                             ifelse(group=="b" & X1 > 0, DV+rnorm(n=1, mean=2) , DV)))
# make DV binary
data$DV <- ifelse(data$DV > 0, 1, 0)
head(data)
##            X1         X2 DV group
## 1  0.75191377  0.2978933  1     a
## 2 -1.45574399 -0.6868934  0     a
## 3 -0.07297941  0.2450438  0     a
## 4  0.91090328 -1.6934376  1     a
## 5  1.19905258  0.4765307  0     a
## 6  0.42466903 -0.9937526  1     a

Get the coefficients from your logistic regression model

First, whenever you’re using a categorical predictor in a model in R (or anywhere else, for that matter), make sure you know how it’s being coded!! For this example, we want it dummy coded (so we can easily plug in 0’s and 1’s to get equations for the different groups). This is called contr.treatment() in R.

contrasts(data$group)
##   b c d
## a 0 0 0
## b 1 0 0
## c 0 1 0
## d 0 0 1
# Great, that's what we wanted. And we can see that a is the reference group.
# If you want to change what contrasts will run, you can add an argument to the glm() call. For example contrasts="contr.treatment" will make it traditional dummy coding if it isn't already.

Now we can run that model.

# note this use of exponent in a formula will give us all 2-way interactions
model <- glm(DV ~ (X1 + X2 + group)^2, 
             data=data, na.action="na.exclude",  family="binomial") 
             
summary(model)
## 
## Call:
## glm(formula = DV ~ (X1 + X2 + group)^2, family = "binomial", 
##     data = data, na.action = "na.exclude")
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.44094  -0.45991   0.04136   0.52301   2.74705  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -0.5873     0.3438  -1.708 0.087631 .  
## X1            2.6508     0.5592   4.740 2.13e-06 ***
## X2           -2.2599     0.4977  -4.540 5.61e-06 ***
## groupb        2.2111     0.5949   3.717 0.000202 ***
## groupc        0.6650     0.4131   1.610 0.107456    
## X1:X2         0.1201     0.2660   0.452 0.651534    
## X1:groupb     2.7323     1.2977   2.105 0.035253 *  
## X1:groupc    -0.6816     0.7078  -0.963 0.335531    
## X2:groupb     0.8477     0.7320   1.158 0.246882    
## X2:groupc     0.4683     0.6558   0.714 0.475165    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 412.88  on 299  degrees of freedom
## Residual deviance: 205.46  on 290  degrees of freedom
## AIC: 225.46
## 
## Number of Fisher Scoring iterations: 7
model$coef
## (Intercept)          X1          X2      groupb      groupc       X1:X2 
##  -0.5872841   2.6508212  -2.2599250   2.2110951   0.6649971   0.1201166 
##   X1:groupb   X1:groupc   X2:groupb   X2:groupc 
##   2.7323113  -0.6816327   0.8476695   0.4682893
# save the coefficient values so we can use them in the equations
b0 <- model$coef[1] # intercept
X1 <- model$coef[2]
X2 <- -model$coef[3]
groupb <- model$coef[4]
groupc <- model$coef[5]
X1.X2 <- model$coef[6]
X1.groupb <- model$coef[7]
X1.groupc <- model$coef[8]
X2.groupb <- model$coef[9]
X2.groupc <- model$coef[10]

Note: If you were working in SPSS (or for some other reason you have run a model but can’t generate a plot for it), you can enter in your coefficients here, like this:

b0 <- -0.5872841 # intercept
X1 <- 2.6508212
X2 <- -2.2599250
groupb <- 2.2110951
groupc <- 0.6649971
X1.X2 <- 0.1201166
X1.groupb <- 2.7323113
X1.groupc <- -0.6816327
X2.groupb <- 0.8476695
X2.groupc <- 0.4682893

Calculate probabilities for the plot

First, decide what variable you want on your x-axis. That’s the only variable we’ll enter as a whole range. (The range we set here will determine the range on the x-axis of the final plot, by the way.)

X1_range <- seq(from=min(data$X1), to=max(data$X1), by=.01)

Next, compute the equations for each group in logit terms. These equations need to include every coefficient for the model you ran. You’ll need to plug in values for all but one variable – whichever variable you decided will be displayed on the x-axis of your plot. You make a separate equation for each group by plugging in different values for the group dummy codes.

X2_val <- mean(data$X2) # by plugging in the mean as the value for X2, I'll be generating plots that show the relationship between X1 and the outcome "for someone with an average X2".

a_logits <- b0 + 
  X1*X1_range + 
  X2*X2_val + 
  groupb*0 + 
  groupc*0 + 
  X1.X2*X1_range*X2_val + 
  X1.groupb*X1_range*0 + 
  X1.groupc*X1_range*0 + 
  X2.groupb*X2_val*0 + 
  X2.groupc*X2_val*0 # the reference group

b_logits <- b0 + 
  X1*X1_range + 
  X2*X2_val + 
  groupb*1 + 
  groupc*0 + 
  X1.X2*X1_range*X2_val + 
  X1.groupb*X1_range*1 + 
  X1.groupc*X1_range*0 + 
  X2.groupb*X2_val*1 + 
  X2.groupc*X2_val*0

c_logits <- b0 + 
  X1*X1_range + 
  X2*X2_val + 
  groupb*0 + 
  groupc*1 + 
  X1.X2*X1_range*X2_val + 
  X1.groupb*X1_range*0 + 
  X1.groupc*X1_range*1 + 
  X2.groupb*X2_val*0 + 
  X2.groupc*X2_val*1

# Compute the probibilities (this is what will actually get plotted):
a_probs <- exp(a_logits)/(1 + exp(a_logits))
b_probs <- exp(b_logits)/(1 + exp(b_logits))
c_probs <- exp(c_logits)/(1 + exp(c_logits))

Plot time!

# We'll start by plotting the ref group:
plot(X1_range, a_probs, 
     ylim=c(0,1),
     type="l", 
     lwd=3, 
     lty=2, 
     col="gold", 
     xlab="X1", ylab="P(outcome)", main="Probability of super important outcome")


# Add the line for people who are in the b group
lines(X1_range, b_probs, 
      type="l", 
      lwd=3, 
      lty=3, 
      col="turquoise2")

# Add the line for people who are in the c group
lines(X1_range, c_probs, 
      type="l", 
      lwd=3, 
      lty=4, 
      col="orangered")

# add a horizontal line at p=.5
abline(h=.5, lty=2)

Or, you can do it in ggplot2!

library(ggplot2); library(tidyr)
# first you have to get the information into a long dataframe, which is what ggplot likes :)
plot.data <- data.frame(a=a_probs, b=b_probs, c=c_probs, X1=X1_range)
plot.data <- gather(plot.data, key=group, value=prob, a:c)
head(plot.data)
##          X1 group         prob
## 1 -2.770265     a 0.0003264137
## 2 -2.760265     a 0.0003351590
## 3 -2.750265     a 0.0003441385
## 4 -2.740265     a 0.0003533586
## 5 -2.730265     a 0.0003628255
## 6 -2.720265     a 0.0003725460
ggplot(plot.data, aes(x=X1, y=prob, color=group)) + # asking it to set the color by the variable "group" is what makes it draw three different lines
  geom_line(lwd=2) + 
  labs(x="X1", y="P(outcome)", title="Probability of super important outcome") 



structural topic modeling






Structural topic modeling

How to use the stm package (from the stm vignette): stm workflow

The topic modeling part

Find topics in your data!

install.packages("stm", "SnowballC") # probably new
install.packages("dplyr", "tidyr") # if you don't have them already

Getting my data ready. Note that your data should be a data frame where each row has one document. You should have a column called “documents” that has all of the text. Any other variables can be added as additional columns.

df <- read.table("/Users/TARDIS/Documents/STUDIES/context_word_seg/utt_orth_phon_KEY.txt", header=1, sep="\t", stringsAsFactors=F, quote="", comment.char ="") # this gets used for word-lists contexts, it will get over-written for other contexts
library(dplyr); library(tidyr)
data <- df %>%
  select(-phon) %>%
  extract(col=utt, into=c("child", "age_weeks"), regex="^([[:alpha:]]{2})([[:digit:]]{2})")
data$temp <- gl(n=ceiling(nrow(data)/30), k=30)[1:nrow(data)]
data <- group_by(data, child, age_weeks, temp) %>%
  summarize(documents=paste(orth, collapse=" ")) %>%
  select(-temp)

Now bring it to stm for processing there.

library(stm)
## stm v1.1.3 (2016-01-14) successfully loaded. See ?stm for help.
processed <- textProcessor(data$documents, metadata = data)
## Building corpus... 
## Converting to Lower Case... 
## Removing stopwords... 
## Removing numbers... 
## Removing punctuation... 
## Stemming... 
## Creating Output...
out <- prepDocuments(processed$documents, processed$vocab, processed$meta) # removes infrequent terms depending on user-set parameter lower.thresh (the minimum number of documents a word needs to appear in in order for the word to be kept within the vocabulary)
## Removing 504 of 1222 terms (504 of 9742 tokens) due to frequency 
## Your corpus now has 460 documents, 718 terms and 9238 tokens.

Take a look at those messages. I left everything at default here, but you may or may not want to, depending on your research question.

Now let’s get some topics!! (This might take a looooong time to run.)

fit0 <- stm(out$documents, # the documents
            out$vocab, # the words
            K = 10, # 10 topics
            max.em.its = 75, # set to run for a maximum of 75 EM iterations
            data = out$meta, # all the variables (we're not actually including any predictors in this model, though)
            init.type = "Spectral")  
## Beginning Initialization.
##   Calculating the gram matrix...
##   Finding anchor words...
##      ..........
##   Recovering initialization...
##      .......
## Initialization complete.
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 1 (approx. per word bound = -5.240) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 2 (approx. per word bound = -4.973, relative change = 5.097e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 3 (approx. per word bound = -4.893, relative change = 1.603e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 4 (approx. per word bound = -4.865, relative change = 5.813e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 5 (approx. per word bound = -4.851, relative change = 2.764e-03) 
## Topic 1: come, darl, yes, ssh, bath 
##  Topic 2: look, want, hey, like, can 
##  Topic 3: dear, hmm, come, want, smile 
##  Topic 4: come, now, tell, got, yes 
##  Topic 5: hello, hmm, boo, gillian, hey 
##  Topic 6: yes, come, hey, can, tell 
##  Topic 7: yes, got, tell, come, can 
##  Topic 8: hey, mummi, yes, dear, hannah 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, hey, come, got 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 6 (approx. per word bound = -4.844, relative change = 1.453e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 7 (approx. per word bound = -4.840, relative change = 8.955e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 8 (approx. per word bound = -4.837, relative change = 5.594e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 9 (approx. per word bound = -4.835, relative change = 4.705e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 10 (approx. per word bound = -4.833, relative change = 4.139e-04) 
## Topic 1: come, darl, yes, bath, ssh 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, good, want, smile 
##  Topic 4: come, now, got, alright, know 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, come, well, tell, can 
##  Topic 7: yes, got, tell, come, want 
##  Topic 8: hey, mummi, yes, smile, hannah 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, clever, hey, come 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 11 (approx. per word bound = -4.832, relative change = 3.135e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 12 (approx. per word bound = -4.830, relative change = 2.621e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 13 (approx. per word bound = -4.829, relative change = 2.242e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 14 (approx. per word bound = -4.828, relative change = 2.136e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 15 (approx. per word bound = -4.827, relative change = 2.183e-04) 
## Topic 1: darl, come, yes, bath, alright 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, good, got, smile 
##  Topic 4: come, now, alright, know, got 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, well, come, tell, can 
##  Topic 7: yes, got, tell, come, want 
##  Topic 8: hey, mummi, smile, yes, hannah 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, clever, hey, boy 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 16 (approx. per word bound = -4.826, relative change = 2.327e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 17 (approx. per word bound = -4.825, relative change = 2.282e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 18 (approx. per word bound = -4.824, relative change = 1.872e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 19 (approx. per word bound = -4.823, relative change = 1.281e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 20 (approx. per word bound = -4.823, relative change = 9.551e-05) 
## Topic 1: darl, come, yes, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, good, got, girl 
##  Topic 4: come, now, know, alright, got 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, well, come, tell, can 
##  Topic 7: yes, got, tell, come, two 
##  Topic 8: hey, mummi, smile, hannah, yes 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, clever, hey, come 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 21 (approx. per word bound = -4.823, relative change = 8.411e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 22 (approx. per word bound = -4.822, relative change = 9.121e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 23 (approx. per word bound = -4.822, relative change = 9.178e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 24 (approx. per word bound = -4.821, relative change = 8.362e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 25 (approx. per word bound = -4.821, relative change = 6.609e-05) 
## Topic 1: darl, come, yes, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, good, got, girl 
##  Topic 4: come, now, know, alright, get 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, come, well, tell, can 
##  Topic 7: yes, got, tell, two, come 
##  Topic 8: hey, mummi, smile, hannah, yes 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, clever, hey, well 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 26 (approx. per word bound = -4.821, relative change = 6.486e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 27 (approx. per word bound = -4.820, relative change = 7.197e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 28 (approx. per word bound = -4.820, relative change = 8.080e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 29 (approx. per word bound = -4.819, relative change = 8.420e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 30 (approx. per word bound = -4.819, relative change = 8.447e-05) 
## Topic 1: darl, come, yes, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, good, got, girl 
##  Topic 4: come, now, know, alright, get 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, come, tell, well, can 
##  Topic 7: yes, tell, got, two, come 
##  Topic 8: hey, smile, mummi, hannah, look 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, clever, well, hey 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 31 (approx. per word bound = -4.819, relative change = 6.516e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 32 (approx. per word bound = -4.818, relative change = 6.256e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 33 (approx. per word bound = -4.818, relative change = 7.905e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 34 (approx. per word bound = -4.818, relative change = 6.690e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 35 (approx. per word bound = -4.818, relative change = 4.168e-05) 
## Topic 1: darl, come, yes, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, got, good, girl 
##  Topic 4: come, now, know, alright, want 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, come, tell, well, can 
##  Topic 7: yes, tell, got, two, one 
##  Topic 8: hey, smile, mummi, hannah, look 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, well, clever, hey 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 36 (approx. per word bound = -4.817, relative change = 3.416e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 37 (approx. per word bound = -4.817, relative change = 3.527e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 38 (approx. per word bound = -4.817, relative change = 3.889e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 39 (approx. per word bound = -4.817, relative change = 4.008e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 40 (approx. per word bound = -4.817, relative change = 3.828e-05) 
## Topic 1: darl, yes, come, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, got, good, girl 
##  Topic 4: come, now, know, alright, want 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, tell, come, can, well 
##  Topic 7: yes, got, tell, two, one 
##  Topic 8: hey, smile, mummi, look, hannah 
##  Topic 9: mummi, yes, girl, hello, hannah 
##  Topic 10: tickl, yes, well, clever, christoph 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 41 (approx. per word bound = -4.817, relative change = 3.309e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 42 (approx. per word bound = -4.816, relative change = 3.105e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 43 (approx. per word bound = -4.816, relative change = 2.992e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 44 (approx. per word bound = -4.816, relative change = 2.875e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 45 (approx. per word bound = -4.816, relative change = 2.589e-05) 
## Topic 1: darl, yes, come, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, got, good, girl 
##  Topic 4: come, now, know, alright, want 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, tell, come, can, stori 
##  Topic 7: yes, got, tell, two, one 
##  Topic 8: hey, smile, mummi, look, hannah 
##  Topic 9: mummi, yes, girl, hello, alright 
##  Topic 10: tickl, well, yes, clever, christoph 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 46 (approx. per word bound = -4.816, relative change = 1.884e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 47 (approx. per word bound = -4.816, relative change = 1.983e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 48 (approx. per word bound = -4.816, relative change = 1.887e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 49 (approx. per word bound = -4.816, relative change = 1.402e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 50 (approx. per word bound = -4.816, relative change = 1.453e-05) 
## Topic 1: darl, yes, come, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, got, good, girl 
##  Topic 4: come, now, know, alright, want 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, tell, come, can, stori 
##  Topic 7: yes, got, tell, two, one 
##  Topic 8: hey, smile, mummi, look, hannah 
##  Topic 9: mummi, yes, girl, hello, alright 
##  Topic 10: tickl, well, yes, clever, christoph 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 51 (approx. per word bound = -4.815, relative change = 1.471e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 52 (approx. per word bound = -4.815, relative change = 1.221e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 53 (approx. per word bound = -4.815, relative change = 1.442e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 54 (approx. per word bound = -4.815, relative change = 1.921e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 55 (approx. per word bound = -4.815, relative change = 1.180e-05) 
## Topic 1: darl, yes, come, bath, clean 
##  Topic 2: look, want, can, like, nice 
##  Topic 3: dear, come, got, good, girl 
##  Topic 4: come, now, alright, know, want 
##  Topic 5: hello, hmm, boo, gillian, matter 
##  Topic 6: yes, tell, come, can, stori 
##  Topic 7: yes, got, two, tell, can 
##  Topic 8: hey, smile, mummi, look, hannah 
##  Topic 9: mummi, yes, girl, hello, alright 
##  Topic 10: tickl, well, yes, clever, christoph 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Model Converged

Note: “The default is init.type =”LDA" but in practice researchers on personal computers with vocabularies less that 10,000 can utilize the spectral initialization successfully." And spectral initialization is better, so you should do that. If you have a very large dataset, read up on how to correctly use the LDA option in the stm vignette.

Yay! Let’s look:

labelTopics(fit0)
## Topic 1 Top Words:
##       Highest Prob: darl, yes, come, bath, clean, nice, put 
##       FREX: clean, bath, wee, pet, darl, dirti, dri 
##       Lift: rid, took, vesti, armi, bobo, petal, soapi 
##       Score: bobo, juli, darl, clean, pet, bath, wee 
## Topic 2 Top Words:
##       Highest Prob: look, want, can, like, nice, littl, make 
##       FREX: lewi, make, bit, foot, rattl, play, push 
##       Lift: bite, cupboard, cupsi, lew, lewi, newspap, pour 
##       Score: juli, newspap, lewi, foot, charl, boy, joseph 
## Topic 3 Top Words:
##       Highest Prob: dear, come, got, good, girl, windi, littl 
##       FREX: dear, windi, sleepi, bless, whoop, head, downstair 
##       Lift: unhappi, milki, gracious, hurri, downstair, sock, dear 
##       Score: juli, unhappi, dear, windi, sleepi, head, fatti 
## Topic 4 Top Words:
##       Highest Prob: come, now, alright, know, want, get, got 
##       FREX: keith, shut, readi, bib, attent, bottl, first 
##       Lift: cake, dessert, dream, five, self, allow, attent 
##       Score: self, keith, crocodil, alright, attent, first, juic 
## Topic 5 Top Words:
##       Highest Prob: hello, hmm, boo, gillian, matter, want, cheeki 
##       FREX: boo, gillian, cheeki, hmm, hello, ticki, monkey 
##       Lift: pinch, poke, cheeki, gillian, boo, mayb, moan 
##       Score: cheeki, juli, hello, boo, gillian, hmm, ticki 
## Topic 6 Top Words:
##       Highest Prob: yes, tell, come, can, stori, mum, good 
##       FREX: quack, stori, thumb, tell, finger, suck, scalp 
##       Lift: bodi, cmon, concentr, pretend, scalp, silent, sober 
##       Score: concentr, juli, quack, tell, scalp, stori, clap 
## Topic 7 Top Words:
##       Highest Prob: yes, got, two, tell, can, one, bad 
##       FREX: cold, two, pretti, nose, walk, happi, bad 
##       Lift: color, nuddi, cardigan, cold, mother, punch, tale 
##       Score: cold, juli, pram, yes, two, nose, pretti 
## Topic 8 Top Words:
##       Highest Prob: hey, smile, mummi, look, hannah, hold, yes 
##       FREX: shh, hey, smile, lambchop, chou, hold, hannah 
##       Lift: contempl, fine, juli, wiggler, shh, botti, friend 
##       Score: juli, wiggler, hey, hannah, lambchop, shh, smile 
## Topic 9 Top Words:
##       Highest Prob: mummi, yes, girl, hello, alright, hannah, big 
##       FREX: girli, mummi, sweetheart, alright, girl, iron, stretch 
##       Lift: globe, pick, prove, rubber, scream, splish, wobbl 
##       Score: juli, splish, hannah, hello, mummi, girl, alright 
## Topic 10 Top Words:
##       Highest Prob: tickl, well, yes, clever, christoph, kick, boy 
##       FREX: tickl, christoph, well, bash, clever, parrot, boy 
##       Lift: ahhphroooowp, tickl, bash, plenti, kitten, music, chris 
##       Score: ahhphroooowp, juli, tickl, well, christoph, clever, boy

For more information on FREX and high probability rankings, see Roberts et al. (2013, 2015, 2014); Lucas et al. (2015). For more information on score, see the lda R package. For more information on lift, see Taddy (2013).

plot.STM(fit0, type = "labels") 

plot.STM(fit0, type = "summary") 

# topicCorr(fit0) # lots of output :)
round(topicCorr(fit0)$cor, 2) # just the correlations between topics
##        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
##  [1,]  1.00 -0.24  0.21 -0.08 -0.21 -0.11  0.00 -0.12  0.02 -0.15
##  [2,] -0.24  1.00 -0.29 -0.03 -0.08 -0.15 -0.10 -0.10 -0.22 -0.06
##  [3,]  0.21 -0.29  1.00 -0.11  0.00 -0.08  0.08 -0.12 -0.18 -0.11
##  [4,] -0.08 -0.03 -0.11  1.00 -0.19 -0.04 -0.10 -0.15 -0.15 -0.09
##  [5,] -0.21 -0.08  0.00 -0.19  1.00 -0.18 -0.14 -0.05 -0.26 -0.05
##  [6,] -0.11 -0.15 -0.08 -0.04 -0.18  1.00 -0.12 -0.10 -0.21  0.02
##  [7,]  0.00 -0.10  0.08 -0.10 -0.14 -0.12  1.00 -0.14 -0.17 -0.05
##  [8,] -0.12 -0.10 -0.12 -0.15 -0.05 -0.10 -0.14  1.00 -0.02 -0.13
##  [9,]  0.02 -0.22 -0.18 -0.15 -0.26 -0.21 -0.17 -0.02  1.00 -0.21
## [10,] -0.15 -0.06 -0.11 -0.09 -0.05  0.02 -0.05 -0.13 -0.21  1.00

How many topics?

The right answer depends on your corpus, the size of your documents, and your research question. Even when you have a clear research question, it still may be tricky to decide how many topics will make the most sense. Especially when you’re starting out and you don’t necessarily know how the chracteristics of your corpus/documents will affect your topic models, you might want to try out several different numbers of topics. You can run several models with different numbers of topics and compare them to help you figure out what makes the most sense using the searchk() function. This code runs a model with 7 topics and another with 10.

ntopics <- searchK(out$documents, out$vocab, K = c(7, 10), data = meta)
## Beginning Initialization.
##   Calculating the gram matrix...
##   Finding anchor words...
##      .......
##   Recovering initialization...
##      .......
## Initialization complete.
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 1 (approx. per word bound = -5.320) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 2 (approx. per word bound = -5.187, relative change = 2.500e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 3 (approx. per word bound = -5.110, relative change = 1.477e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 4 (approx. per word bound = -5.068, relative change = 8.138e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 5 (approx. per word bound = -5.041, relative change = 5.343e-03) 
## Topic 1: clever, look, like, boy, want 
##  Topic 2: come, know, matter, hmm, littl 
##  Topic 3: dear, well, want, like, just 
##  Topic 4: hello, smile, boo, hmm, good 
##  Topic 5: yes, tickl, can, hey, dear 
##  Topic 6: come, dear, got, good, want 
##  Topic 7: hey, mummi, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 6 (approx. per word bound = -5.021, relative change = 4.082e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 7 (approx. per word bound = -5.005, relative change = 3.107e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 8 (approx. per word bound = -4.994, relative change = 2.305e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 9 (approx. per word bound = -4.984, relative change = 1.851e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 10 (approx. per word bound = -4.977, relative change = 1.517e-03) 
## Topic 1: look, want, like, clever, nice 
##  Topic 2: come, know, matter, bath, now 
##  Topic 3: dear, want, well, like, just 
##  Topic 4: hello, hmm, smile, boo, look 
##  Topic 5: yes, tickl, hey, can, tell 
##  Topic 6: dear, come, got, good, tell 
##  Topic 7: hey, mummi, girl, hannah, alright 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 11 (approx. per word bound = -4.971, relative change = 1.184e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 12 (approx. per word bound = -4.966, relative change = 9.019e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 13 (approx. per word bound = -4.963, relative change = 7.212e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 14 (approx. per word bound = -4.960, relative change = 6.433e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 15 (approx. per word bound = -4.957, relative change = 6.157e-04) 
## Topic 1: look, want, like, can, clever 
##  Topic 2: come, know, now, bath, matter 
##  Topic 3: want, like, get, just, put 
##  Topic 4: hello, hmm, smile, boo, look 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, good, tell 
##  Topic 7: hey, mummi, girl, hannah, alright 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 16 (approx. per word bound = -4.954, relative change = 5.870e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 17 (approx. per word bound = -4.951, relative change = 5.736e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 18 (approx. per word bound = -4.948, relative change = 5.389e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 19 (approx. per word bound = -4.946, relative change = 4.811e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 20 (approx. per word bound = -4.944, relative change = 4.405e-04) 
## Topic 1: look, well, like, can, want 
##  Topic 2: come, know, now, bath, hey 
##  Topic 3: get, want, like, put, just 
##  Topic 4: hello, hmm, smile, boo, look 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: hey, mummi, girl, hannah, yes 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 21 (approx. per word bound = -4.942, relative change = 4.003e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 22 (approx. per word bound = -4.940, relative change = 3.532e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 23 (approx. per word bound = -4.938, relative change = 3.193e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 24 (approx. per word bound = -4.937, relative change = 3.207e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 25 (approx. per word bound = -4.935, relative change = 3.742e-04) 
## Topic 1: look, well, can, like, nice 
##  Topic 2: come, know, now, hey, will 
##  Topic 3: get, want, like, put, babi 
##  Topic 4: hello, hmm, boo, smile, hey 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: hey, mummi, girl, hannah, yes 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 26 (approx. per word bound = -4.933, relative change = 3.510e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 27 (approx. per word bound = -4.932, relative change = 2.393e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 28 (approx. per word bound = -4.931, relative change = 1.867e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 29 (approx. per word bound = -4.930, relative change = 1.659e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 30 (approx. per word bound = -4.930, relative change = 1.534e-04) 
## Topic 1: well, look, can, like, nice 
##  Topic 2: come, know, now, hey, will 
##  Topic 3: get, want, like, come, put 
##  Topic 4: hello, hmm, boo, smile, hey 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: hey, mummi, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 31 (approx. per word bound = -4.929, relative change = 1.376e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 32 (approx. per word bound = -4.928, relative change = 1.238e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 33 (approx. per word bound = -4.928, relative change = 1.124e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 34 (approx. per word bound = -4.927, relative change = 1.029e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 35 (approx. per word bound = -4.927, relative change = 9.005e-05) 
## Topic 1: well, look, can, like, nice 
##  Topic 2: come, know, now, hey, will 
##  Topic 3: get, come, want, like, put 
##  Topic 4: hello, hmm, boo, smile, hey 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: hey, mummi, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 36 (approx. per word bound = -4.926, relative change = 7.793e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 37 (approx. per word bound = -4.926, relative change = 7.025e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 38 (approx. per word bound = -4.926, relative change = 6.147e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 39 (approx. per word bound = -4.925, relative change = 5.097e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 40 (approx. per word bound = -4.925, relative change = 4.265e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, now, know, hey, say 
##  Topic 3: get, come, want, like, put 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: hey, mummi, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 41 (approx. per word bound = -4.925, relative change = 4.111e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 42 (approx. per word bound = -4.925, relative change = 4.715e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 43 (approx. per word bound = -4.924, relative change = 5.951e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 44 (approx. per word bound = -4.924, relative change = 5.967e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 45 (approx. per word bound = -4.924, relative change = 5.443e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, now, hey, know, say 
##  Topic 3: come, get, want, like, got 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: hey, mummi, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 46 (approx. per word bound = -4.924, relative change = 5.656e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 47 (approx. per word bound = -4.923, relative change = 5.992e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 48 (approx. per word bound = -4.923, relative change = 6.316e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 49 (approx. per word bound = -4.923, relative change = 6.208e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 50 (approx. per word bound = -4.922, relative change = 5.230e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, want, like, got 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 51 (approx. per word bound = -4.922, relative change = 4.456e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 52 (approx. per word bound = -4.922, relative change = 3.996e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 53 (approx. per word bound = -4.922, relative change = 3.662e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 54 (approx. per word bound = -4.922, relative change = 3.427e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 55 (approx. per word bound = -4.922, relative change = 3.327e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, want, like, got 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, one, tell 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 56 (approx. per word bound = -4.921, relative change = 3.032e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 57 (approx. per word bound = -4.921, relative change = 2.993e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 58 (approx. per word bound = -4.921, relative change = 3.491e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 59 (approx. per word bound = -4.921, relative change = 3.423e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 60 (approx. per word bound = -4.921, relative change = 3.104e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, like, got, want 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, tell, one 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 61 (approx. per word bound = -4.921, relative change = 2.903e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 62 (approx. per word bound = -4.921, relative change = 2.340e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 63 (approx. per word bound = -4.920, relative change = 2.198e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 64 (approx. per word bound = -4.920, relative change = 2.342e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 65 (approx. per word bound = -4.920, relative change = 2.282e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, like, got, want 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, tell, one 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 66 (approx. per word bound = -4.920, relative change = 2.428e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 67 (approx. per word bound = -4.920, relative change = 2.718e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 68 (approx. per word bound = -4.920, relative change = 2.945e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 69 (approx. per word bound = -4.920, relative change = 2.687e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 70 (approx. per word bound = -4.920, relative change = 2.376e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, got, like, want 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, tell, one 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 71 (approx. per word bound = -4.919, relative change = 2.175e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 72 (approx. per word bound = -4.919, relative change = 2.256e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 73 (approx. per word bound = -4.919, relative change = 2.462e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 74 (approx. per word bound = -4.919, relative change = 2.732e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 75 (approx. per word bound = -4.919, relative change = 2.787e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, got, like, want 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, tell, one 
##  Topic 6: dear, come, got, tell, good 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 76 (approx. per word bound = -4.919, relative change = 2.709e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 77 (approx. per word bound = -4.919, relative change = 2.806e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 78 (approx. per word bound = -4.918, relative change = 3.203e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 79 (approx. per word bound = -4.918, relative change = 4.204e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 80 (approx. per word bound = -4.918, relative change = 5.751e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, got, like, want 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, hey, tell, one 
##  Topic 6: dear, come, got, good, tell 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 81 (approx. per word bound = -4.918, relative change = 6.615e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 82 (approx. per word bound = -4.917, relative change = 4.844e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 83 (approx. per word bound = -4.917, relative change = 2.848e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 84 (approx. per word bound = -4.917, relative change = 1.913e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 85 (approx. per word bound = -4.917, relative change = 1.439e-05) 
## Topic 1: well, can, look, like, nice 
##  Topic 2: come, hey, now, know, say 
##  Topic 3: come, get, got, like, want 
##  Topic 4: hello, hmm, boo, hey, smile 
##  Topic 5: yes, tickl, tell, hey, one 
##  Topic 6: dear, come, got, good, tell 
##  Topic 7: mummi, hey, yes, girl, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 86 (approx. per word bound = -4.917, relative change = 1.444e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 87 (approx. per word bound = -4.917, relative change = 1.642e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 88 (approx. per word bound = -4.917, relative change = 1.134e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Model Converged 
## Beginning Initialization.
##   Calculating the gram matrix...
##   Finding anchor words...
##      ..........
##   Recovering initialization...
##      .......
## Initialization complete.
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 1 (approx. per word bound = -5.279) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 2 (approx. per word bound = -5.038, relative change = 4.560e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 3 (approx. per word bound = -4.941, relative change = 1.922e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 4 (approx. per word bound = -4.904, relative change = 7.610e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 5 (approx. per word bound = -4.882, relative change = 4.401e-03) 
## Topic 1: yes, can, well, boy, clever 
##  Topic 2: yes, come, tell, got, good 
##  Topic 3: boo, say, hey, kick, want 
##  Topic 4: look, hey, see, littl, can 
##  Topic 5: hey, yes, mummi, hannah, look 
##  Topic 6: yes, tickl, come, dear, tell 
##  Topic 7: hello, hmm, hey, gillian, got 
##  Topic 8: dear, hey, alright, come, mummi 
##  Topic 9: come, mummi, big, yes, now 
##  Topic 10: mummi, yes, hannah, girl, nice 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 6 (approx. per word bound = -4.869, relative change = 2.573e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 7 (approx. per word bound = -4.861, relative change = 1.692e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 8 (approx. per word bound = -4.856, relative change = 1.166e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 9 (approx. per word bound = -4.851, relative change = 1.002e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 10 (approx. per word bound = -4.846, relative change = 8.826e-04) 
## Topic 1: yes, can, boy, clever, nice 
##  Topic 2: yes, come, tell, got, good 
##  Topic 3: boo, well, say, hey, kick 
##  Topic 4: look, see, littl, want, like 
##  Topic 5: hey, yes, mummi, hannah, hold 
##  Topic 6: yes, tickl, come, dear, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, hey, alright, come, darl 
##  Topic 9: come, big, mummi, now, smile 
##  Topic 10: mummi, yes, girl, hannah, nice 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 11 (approx. per word bound = -4.843, relative change = 7.865e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 12 (approx. per word bound = -4.839, relative change = 6.566e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 13 (approx. per word bound = -4.837, relative change = 5.541e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 14 (approx. per word bound = -4.835, relative change = 3.795e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 15 (approx. per word bound = -4.833, relative change = 3.252e-04) 
## Topic 1: can, yes, boy, clever, nice 
##  Topic 2: yes, come, tell, got, good 
##  Topic 3: well, boo, say, come, yes 
##  Topic 4: look, want, see, littl, like 
##  Topic 5: hey, yes, mummi, hannah, smile 
##  Topic 6: yes, tickl, come, dear, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, hey, alright, come, darl 
##  Topic 9: come, big, now, smile, mummi 
##  Topic 10: mummi, yes, girl, hannah, good 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 16 (approx. per word bound = -4.832, relative change = 3.171e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 17 (approx. per word bound = -4.831, relative change = 1.815e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 18 (approx. per word bound = -4.830, relative change = 9.873e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 19 (approx. per word bound = -4.830, relative change = 7.631e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 20 (approx. per word bound = -4.830, relative change = 8.756e-05) 
## Topic 1: can, yes, boy, clever, nice 
##  Topic 2: yes, come, tell, got, good 
##  Topic 3: well, boo, say, come, yes 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, hannah, smile 
##  Topic 6: yes, tickl, come, dear, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, alright, hey, darl 
##  Topic 9: come, big, now, smile, fat 
##  Topic 10: mummi, girl, yes, hannah, good 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 21 (approx. per word bound = -4.829, relative change = 1.390e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 22 (approx. per word bound = -4.828, relative change = 1.597e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 23 (approx. per word bound = -4.828, relative change = 1.278e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 24 (approx. per word bound = -4.827, relative change = 1.158e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 25 (approx. per word bound = -4.826, relative change = 1.131e-04) 
## Topic 1: can, yes, boy, clever, like 
##  Topic 2: yes, come, tell, good, got 
##  Topic 3: well, boo, say, come, yes 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, hannah, smile 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, alright, darl, ssh 
##  Topic 9: come, big, now, smile, fat 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 26 (approx. per word bound = -4.826, relative change = 8.500e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 27 (approx. per word bound = -4.826, relative change = 8.340e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 28 (approx. per word bound = -4.825, relative change = 8.284e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 29 (approx. per word bound = -4.825, relative change = 1.009e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 30 (approx. per word bound = -4.824, relative change = 1.199e-04) 
## Topic 1: can, yes, boy, clever, like 
##  Topic 2: yes, come, tell, good, got 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, alright, darl, ssh 
##  Topic 9: come, big, now, smile, get 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 31 (approx. per word bound = -4.824, relative change = 1.118e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 32 (approx. per word bound = -4.823, relative change = 1.126e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 33 (approx. per word bound = -4.823, relative change = 1.102e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 34 (approx. per word bound = -4.822, relative change = 1.108e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 35 (approx. per word bound = -4.822, relative change = 9.561e-05) 
## Topic 1: can, yes, clever, boy, like 
##  Topic 2: yes, come, tell, good, got 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, alright, got 
##  Topic 9: come, big, now, smile, get 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 36 (approx. per word bound = -4.821, relative change = 7.619e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 37 (approx. per word bound = -4.821, relative change = 8.315e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 38 (approx. per word bound = -4.820, relative change = 8.645e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 39 (approx. per word bound = -4.820, relative change = 6.973e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 40 (approx. per word bound = -4.820, relative change = 6.576e-05) 
## Topic 1: can, yes, clever, boy, like 
##  Topic 2: yes, come, tell, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, alright, got 
##  Topic 9: come, big, now, smile, get 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 41 (approx. per word bound = -4.819, relative change = 8.137e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 42 (approx. per word bound = -4.819, relative change = 9.252e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 43 (approx. per word bound = -4.818, relative change = 9.444e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 44 (approx. per word bound = -4.818, relative change = 8.206e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 45 (approx. per word bound = -4.818, relative change = 6.200e-05) 
## Topic 1: can, yes, clever, like, boy 
##  Topic 2: yes, come, tell, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, smile, mummi 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, alright, got 
##  Topic 9: come, big, now, smile, get 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 46 (approx. per word bound = -4.818, relative change = 5.607e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 47 (approx. per word bound = -4.817, relative change = 5.721e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 48 (approx. per word bound = -4.817, relative change = 5.900e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 49 (approx. per word bound = -4.817, relative change = 6.012e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 50 (approx. per word bound = -4.816, relative change = 6.268e-05) 
## Topic 1: can, yes, like, clever, tri 
##  Topic 2: yes, come, tell, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, smile, mummi 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 51 (approx. per word bound = -4.816, relative change = 5.711e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 52 (approx. per word bound = -4.816, relative change = 4.735e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 53 (approx. per word bound = -4.816, relative change = 4.113e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 54 (approx. per word bound = -4.815, relative change = 4.369e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 55 (approx. per word bound = -4.815, relative change = 5.259e-05) 
## Topic 1: can, yes, like, clever, tri 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, smile, mummi 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 56 (approx. per word bound = -4.815, relative change = 4.744e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 57 (approx. per word bound = -4.815, relative change = 4.242e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 58 (approx. per word bound = -4.815, relative change = 5.003e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 59 (approx. per word bound = -4.814, relative change = 6.421e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 60 (approx. per word bound = -4.814, relative change = 6.686e-05) 
## Topic 1: can, yes, like, clever, tri 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, smile, mummi 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 61 (approx. per word bound = -4.814, relative change = 6.098e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 62 (approx. per word bound = -4.813, relative change = 6.503e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 63 (approx. per word bound = -4.813, relative change = 7.060e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 64 (approx. per word bound = -4.813, relative change = 5.916e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 65 (approx. per word bound = -4.812, relative change = 4.233e-05) 
## Topic 1: can, yes, like, tri, clever 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, smile, mummi 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 66 (approx. per word bound = -4.812, relative change = 3.007e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 67 (approx. per word bound = -4.812, relative change = 2.289e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 68 (approx. per word bound = -4.812, relative change = 2.108e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 69 (approx. per word bound = -4.812, relative change = 3.370e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 70 (approx. per word bound = -4.812, relative change = 5.149e-05) 
## Topic 1: can, yes, like, nice, tri 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, smile, mummi 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 71 (approx. per word bound = -4.811, relative change = 4.136e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 72 (approx. per word bound = -4.811, relative change = 3.405e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 73 (approx. per word bound = -4.811, relative change = 3.108e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 74 (approx. per word bound = -4.811, relative change = 2.621e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 75 (approx. per word bound = -4.811, relative change = 3.038e-05) 
## Topic 1: can, yes, like, nice, tri 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, mummi, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 76 (approx. per word bound = -4.811, relative change = 3.308e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 77 (approx. per word bound = -4.811, relative change = 3.611e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 78 (approx. per word bound = -4.810, relative change = 3.891e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 79 (approx. per word bound = -4.810, relative change = 3.397e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 80 (approx. per word bound = -4.810, relative change = 2.829e-05) 
## Topic 1: can, yes, like, nice, tri 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, mummi, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 81 (approx. per word bound = -4.810, relative change = 3.003e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 82 (approx. per word bound = -4.810, relative change = 4.447e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 83 (approx. per word bound = -4.810, relative change = 4.149e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 84 (approx. per word bound = -4.809, relative change = 3.027e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 85 (approx. per word bound = -4.809, relative change = 2.585e-05) 
## Topic 1: can, yes, like, nice, tri 
##  Topic 2: yes, tell, come, good, mum 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, look, mummi, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 86 (approx. per word bound = -4.809, relative change = 2.408e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 87 (approx. per word bound = -4.809, relative change = 2.769e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 88 (approx. per word bound = -4.809, relative change = 3.707e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 89 (approx. per word bound = -4.809, relative change = 4.295e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 90 (approx. per word bound = -4.808, relative change = 3.493e-05) 
## Topic 1: can, yes, like, nice, tri 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hannah, hello 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 91 (approx. per word bound = -4.808, relative change = 3.001e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 92 (approx. per word bound = -4.808, relative change = 2.956e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 93 (approx. per word bound = -4.808, relative change = 3.409e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 94 (approx. per word bound = -4.808, relative change = 3.833e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 95 (approx. per word bound = -4.808, relative change = 3.496e-05) 
## Topic 1: can, yes, like, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, tell, got 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 96 (approx. per word bound = -4.808, relative change = 3.052e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 97 (approx. per word bound = -4.807, relative change = 3.032e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 98 (approx. per word bound = -4.807, relative change = 3.527e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 99 (approx. per word bound = -4.807, relative change = 2.665e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 100 (approx. per word bound = -4.807, relative change = 1.909e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 101 (approx. per word bound = -4.807, relative change = 1.807e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 102 (approx. per word bound = -4.807, relative change = 1.882e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 103 (approx. per word bound = -4.807, relative change = 2.111e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 104 (approx. per word bound = -4.807, relative change = 2.265e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 105 (approx. per word bound = -4.806, relative change = 2.207e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, make 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 106 (approx. per word bound = -4.806, relative change = 2.141e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 107 (approx. per word bound = -4.806, relative change = 1.712e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 108 (approx. per word bound = -4.806, relative change = 1.351e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 109 (approx. per word bound = -4.806, relative change = 1.212e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 110 (approx. per word bound = -4.806, relative change = 1.050e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 111 (approx. per word bound = -4.806, relative change = 1.141e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 112 (approx. per word bound = -4.806, relative change = 1.101e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 113 (approx. per word bound = -4.806, relative change = 1.211e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 114 (approx. per word bound = -4.806, relative change = 1.544e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 115 (approx. per word bound = -4.806, relative change = 1.829e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, look, smile 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, big, get, smile 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 116 (approx. per word bound = -4.806, relative change = 2.202e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 117 (approx. per word bound = -4.806, relative change = 2.304e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 118 (approx. per word bound = -4.805, relative change = 2.126e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 119 (approx. per word bound = -4.805, relative change = 1.923e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 120 (approx. per word bound = -4.805, relative change = 1.987e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, smile, look 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, get, big, know 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 121 (approx. per word bound = -4.805, relative change = 3.045e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 122 (approx. per word bound = -4.805, relative change = 2.173e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 123 (approx. per word bound = -4.805, relative change = 1.557e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 124 (approx. per word bound = -4.805, relative change = 1.552e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 125 (approx. per word bound = -4.805, relative change = 1.616e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, smile, look 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, get, big, know 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 126 (approx. per word bound = -4.805, relative change = 1.695e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 127 (approx. per word bound = -4.805, relative change = 1.937e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 128 (approx. per word bound = -4.805, relative change = 2.153e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 129 (approx. per word bound = -4.804, relative change = 1.865e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 130 (approx. per word bound = -4.804, relative change = 1.280e-05) 
## Topic 1: can, like, yes, tri, nice 
##  Topic 2: yes, tell, come, good, one 
##  Topic 3: well, boo, say, yes, come 
##  Topic 4: want, look, see, littl, nice 
##  Topic 5: hey, yes, mummi, smile, look 
##  Topic 6: yes, tickl, come, got, tell 
##  Topic 7: hello, hmm, gillian, got, matter 
##  Topic 8: dear, come, darl, got, alright 
##  Topic 9: come, now, get, big, know 
##  Topic 10: mummi, yes, girl, hello, hannah 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Completing Iteration 131 (approx. per word bound = -4.804, relative change = 1.213e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## Completed M-Step. 
## Model Converged
plot(ntopics)

The structural part

The idea is that you can use the idea of topic modeling within a larger model that includes predictors, etc. that you think may change what the topics are like or how often particular topics show up. There are two different flavors of predictors to think about: those that change how much each topic shows up (prevalence), and those that change

For example (from the stm vignette), maybe you have lots of posts from a handful of political blogs, and you have the blogs coded as either liberal or conserative, and you know the date each post was written. You might expect that some topics are more likely to be discussed more often on liberal blogs vs. conservative ones. That’s a difference in prevalence. Or you might think that some topics will be more or less likely as time goes on (e.g. maybe you expect an increase in posts about a certain candidate as he or she wins more primaries). Again, that’s prevalence.

If you put in no predictors at all, the model reduces to a fast implementation of the Correlated Topic Model (Blei and Lafferty 2007). In other words, it’s regular ol’ LDA, but the topics themselves are allowed to covary.

fit1 <- stm(out$documents, # the documents
            out$vocab, # the words
            content =~ child, 
            prevalence =~ age_weeks,
            K = 10, # 10 topics
            max.em.its = 75, # set to run for a maximum of 75 EM iterations
            data = out$meta, # all the variables (we're not actually including any predictors in this model, though)
            init.type = "Spectral") 
## Warning in sparse.model.matrix(termobj, data = data): variable 'age_weeks'
## converted to a factor
## Beginning Initialization.
##   Calculating the gram matrix...
##   Finding anchor words...
##      ..........
##   Recovering initialization...
##      .......
## Initialization complete.
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 1 (approx. per word bound = -5.240) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 2 (approx. per word bound = -4.760, relative change = 9.166e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 3 (approx. per word bound = -4.664, relative change = 2.011e-02) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 4 (approx. per word bound = -4.621, relative change = 9.143e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 5 (approx. per word bound = -4.598, relative change = 5.012e-03) 
## Topic 1: clean, head, bath, bobo, fatti 
##  Topic 2: goin, shake, oop, hand, watch 
##  Topic 3: interest, smile, bet, bye, wonder 
##  Topic 4: sorri, outsid, rude, chang, home 
##  Topic 5: yesterday, boo, puppet, cheeki, hot 
##  Topic 6: els, mouth, believ, blow, bounc 
##  Topic 7: walk, cold, bad, three, said 
##  Topic 8: smile, cheek, star, light, hey 
##  Topic 9: swim, teeth, drop, rub, soft 
##  Topic 10: tickl, done, feet, tick, tri 
## Aspect 1: gorgeous, grumpi, shush, hmm, moan 
##  Aspect 2: christoph, hour, microphon, allow, suck 
##  Aspect 3: precious, hide, silent, mop, plenti 
##  Aspect 4: keith, somat, violent, pup, grandma 
##  Aspect 5: mummi, quiet, busi, prove, nosh 
##  Aspect 6: lulu, cradl, appl, mobil, pussycat 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 6 (approx. per word bound = -4.586, relative change = 2.599e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 7 (approx. per word bound = -4.579, relative change = 1.676e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 8 (approx. per word bound = -4.573, relative change = 1.288e-03) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 9 (approx. per word bound = -4.569, relative change = 8.567e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 10 (approx. per word bound = -4.566, relative change = 5.746e-04) 
## Topic 1: bath, clean, head, cold, ssh 
##  Topic 2: shake, goin, watch, oop, hand 
##  Topic 3: wonder, interest, bet, forget, hungri 
##  Topic 4: sorri, babi, mum, chang, outsid 
##  Topic 5: yesterday, cheeki, boo, hot, puppet 
##  Topic 6: blow, els, bounc, can, quack 
##  Topic 7: walk, three, said, bad, pretti 
##  Topic 8: smile, light, star, cheek, hey 
##  Topic 9: swim, rub, drop, rusk, exercis 
##  Topic 10: tickl, done, feet, tick, tri 
## Aspect 1: hmm, grumpi, gorgeous, moan, fright 
##  Aspect 2: christoph, throat, allow, microphon, suck 
##  Aspect 3: precious, plenti, silent, curl, mop 
##  Aspect 4: keith, somat, pup, violent, grandma 
##  Aspect 5: mummi, prove, quiet, busi, nosh 
##  Aspect 6: lulu, chime, pussycat, mobil, cradl 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (5 seconds). 
## Completing Iteration 11 (approx. per word bound = -4.563, relative change = 6.549e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 12 (approx. per word bound = -4.561, relative change = 4.785e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 13 (approx. per word bound = -4.559, relative change = 4.990e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 14 (approx. per word bound = -4.557, relative change = 3.450e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 15 (approx. per word bound = -4.556, relative change = 2.834e-04) 
## Topic 1: bath, ssh, clean, cold, head 
##  Topic 2: shake, goin, watch, hand, quit 
##  Topic 3: wonder, interest, bet, hungri, forget 
##  Topic 4: sorri, mum, chang, babi, outsid 
##  Topic 5: cheeki, yesterday, boo, hot, puppet 
##  Topic 6: blow, els, can, dinner, sore 
##  Topic 7: walk, said, hiccough, stand, bad 
##  Topic 8: smile, light, star, cheek, pat 
##  Topic 9: swim, rub, exercis, drop, rusk 
##  Topic 10: tickl, feet, done, tick, tri 
## Aspect 1: grumpi, pinch, gorgeous, hmm, moan 
##  Aspect 2: christoph, music, throat, allow, microphon 
##  Aspect 3: precious, sock, silent, plenti, curl 
##  Aspect 4: keith, pup, somat, violent, grandma 
##  Aspect 5: mummi, prove, contempl, busi, quiet 
##  Aspect 6: lulu, cradl, chime, mobil, pussycat 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 16 (approx. per word bound = -4.554, relative change = 4.654e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 17 (approx. per word bound = -4.554, relative change = 3.129e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 18 (approx. per word bound = -4.552, relative change = 3.226e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 19 (approx. per word bound = -4.550, relative change = 4.019e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 20 (approx. per word bound = -4.548, relative change = 5.125e-04) 
## Topic 1: bath, ssh, clean, cold, head 
##  Topic 2: shake, goin, watch, bop, quit 
##  Topic 3: wonder, hungri, bet, forget, windi 
##  Topic 4: babi, outsid, mum, sorri, chang 
##  Topic 5: cheeki, boo, yesterday, hot, puppet 
##  Topic 6: blow, els, can, bounc, dribbl 
##  Topic 7: walk, said, stand, hiccough, three 
##  Topic 8: smile, light, window, star, pat 
##  Topic 9: swim, rub, rusk, drop, exercis 
##  Topic 10: tickl, done, feet, tick, tri 
## Aspect 1: grumpi, gorgeous, hmm, pinch, moan 
##  Aspect 2: christoph, music, throat, allow, swallow 
##  Aspect 3: precious, silent, sock, gettin, plenti 
##  Aspect 4: somat, pup, violent, grandma, aint 
##  Aspect 5: mummi, prove, quiet, busi, contempl 
##  Aspect 6: cradl, chime, pussycat, mobil, appl 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 21 (approx. per word bound = -4.547, relative change = 2.434e-04) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Completing Iteration 22 (approx. per word bound = -4.547, relative change = 9.134e-05) 
## ...................................................................................................................
## Completed E-Step (0 seconds). 
## ......................................................................................................
## Completed M-Step (6 seconds). 
## Model Converged
sageLabels(fit1) # only works with content covariates in the model
plot(fit1)



Dynamic documents!

For our meeting today, I’ll be sharing tales of my adventures in creating dynamic documents  — building R code right into my papers and presentations, so all of the figures, tables, statistics, etc. are generated automatically by the code — and tips and tricks for folks interested in trying this out.

I’ve tried two different methods for weaving my code in with my text: r-markdown, and sweave (r-latex). Both involve writing the document in plain text (with either markdown or latex formatting), with chunks of code throughout. The main difference is that markdown is MUCH easier to learn, and latex is MUCH more powerful. I’ll focus on latex during my r-club presentation since you all can probably figure out r-markdown pretty well on your own and don’t need me talking at you about it.

To whet your appetite, here are some latex resources:
http://www.tug.org/pracjourn/2008-1/zahn/zahn.pdf
http://mirror.jmu.edu/pub/CTAN/macros/latex/contrib/apa6/apa6.pdf
http://merkel.zoneo.net/Latex/natbib.php
http://yihui.name/knitr/demo/sweave/
https://support.rstudio.com/hc/en-us/articles/200552056-Using-Sweave-and-knitr

Here are my latest dynamic documents:

https://github.com/rosemm/context_word_seg

https://www.dropbox.com/s/5jm99cy17xfuetx/workshop1_slides.Rnw?dl=1