rtopic-modeling

Structural Topic Modeling (stm) Error in makeTopMatrix(prevalence, data) : Error creating model matrix


I'm trying to run the initial steps of this stm tutorial

https://github.com/dondealban/learning-stm

with this dataset, it is part of the original one

http://www.mediafire.com/file/1jk2aoz4ac84jn6/data.csv/file

install.packages("stm")
library(stm)
load("VignetteObjects.RData") 
data <- read.csv("C:/data.csv") 
head(data)
processed <- textProcessor(data$documents, metadata=data)
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta


poliblogPrevFit <- stm(out$documents, out$vocab, K=4, prevalence=~rating+s(day), 
                       max.em.its=200, data=out$meta, init.type="Spectral", 
                       seed=8458159)

But I keep getting the same error

Error in makeTopMatrix(prevalence, data) : Error creating model matrix.
                 This could be caused by many things including
                 explicit calls to a namespace within the formula.
                 Try a simpler formula.

Please can anyone run it in 64 bits MS Windows R-3.5.2.. I could not even find similar errors anywhere..


Solution

  • It seems your problem was that with the sampling you did, you ended up with a factor object with just one level:

    > levels(meta$rating)
    [1] "Conservative"
    

    Using a variable like this does not make any sense though, as there is no variation between cases. If you use the original data, your code works absolutely fine:

    data <- read.csv("https://raw.githubusercontent.com/dondealban/learning-stm/master/data/poliblogs2008.csv")
    
    processed <- textProcessor(data$documents, metadata = data)
    out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
    docs <- out$documents
    vocab <- out$vocab
    meta <- out$meta
    
    levels(meta$rating)
    [1] "Conservative" "Liberal" 
    
    poliblogPrevFit <- stm(docs, vocab, K = 4, prevalence = ~rating+s(day), 
                           max.em.its = 200, data = out$meta, init.type = "Spectral", 
                           seed = 8458159)