4
votes

I'm working on building some topic models in R using the 'topicmodels' package. After pre-processing and creating a document term matrix, I am applying the following LDA Gibbs model. This may be a simple answer but I'm a newbie to R so here it goes. Is there a way that I can export the topics and term lists along with their probabilities to a text file or excel file? I can print them in R (as below), but don't know how to export :(

This is mainly so I can do some visualisation, which I'm sure can be done in Excel, but like I mentioned I'm a newbie and don't have too much available to learn visualisation techniques in R. Hope this makes sense

k = 33
burnin = 1000
iter = 1000
keep = 50
seed = 2003


model_lda <- LDA(myDtm, k = k, method = "Gibbs",control = list(seed = seed, burnin =     burnin, iter = iter, keep = keep))
print(model_lda)
save(model_lda, file = "LDA_Output.RData")

topics(model_lda, 5)
terms(model_lda, 15)


 Topic 1   Topic 2    Topic 3       Topic 4   Topic 5    Topic 6    Topic 7 
[1,] "seat"    "dialogu"  "websit"      "census"  "northern" "growth"   "hse"   
[2,] "resum"   "church"   "partnership" "disabl"  "univers"  "adjust"   "legisl"
[3,] "suspend" "congreg"  "nesc"        "cso"     "peac"     "forecast" "die"   
[4,] "adjourn" "school"   "site"        "statist" "unemploy" "bernard"  "legal" 
[5,] "fisheri" "survivor" "nesf"        "survey"  "polic"    "burton"   "child" 
1
Usually you export data from R with write.table. If you just have simple vectors cat can also be useful. Perhaps you could be a bit more explicit about the output format you need.MrFlick

1 Answers

0
votes

First, you can read in data with readr and then you could use the tidytext R package. For example:

readr::write_csv(tidy(model_lda, "beta"), "beta.csv")

readr::write_csv(tidy(model_lda, "gamma"), "gamma.csv")

The above code should save your beta matrix and gamma matrix in beta.csv and gamma.csv, respectively.

You can also find a chapter that was helpful for me here: http://tidytextmining.com/topicmodeling.html