编辑代码

wt = matrix(0, K, V)
colnames(wt) = vocab
# create topic assignment token list
ta = lapply(docs, function(x) rep( 0, length(x)))
names(ta) = paste0("doc", 1:M)
# create document-topic matrix
dt = matrix(0, M, K)
for(d in 1:M)
{
# randomly assign topic to each word in the list
for( w in 1:length(docs[[d]]))
{
ta[[d]][w] = sample( 1:K, 1 )
# extract the topic index, word id and update the corresponding cell
ti = ta[[d]][w]
wi = docs[[d]][w]
wt[ti, wi] = wt[ti, wi] + 1
}
# count words in document d assigned to each topic t
for(t in 1:K)
dt[d, t] = sum(ta[[d]] == t)
}