data(wsjibm)
## fit a simple topic model
summary( newstpx <- topics(wsjibmCounts, K=10, tol=100), nwrd=10 )
## fit topics over years, using prior shape to allow them to change in time
year <- factor(1900 + as.POSIXlt(wsjibmReturns$DATE)$year)
Y <- nlevels(year)
annualtopics <- vector(length=Y, mode="list")
topwords <- c()
shape=NULL
for(i in 1:Y){
annualtopics[[i]] <- topics(wsjibmCounts[year==levels(year)[i],], K=5, shape=shape, ord=FALSE)
topwords <- cbind(topwords, as.character(summary(annualtopics[[i]], verb=FALSE)$phrase))
delta <- 10000 # weight of the previous year in number of words observed per topic
shape <- annualtopics[[i]]$theta*delta }
## top 5 words by topic in past 4 years
dimnames(topwords) <- list(topic=rep(1:5,each=5), year=levels(year))
print(topwords[,Y - 3:0])
Run the code above in your browser using DataLab