2017-02-15 22 views
0
library(tm) 
library(topicmodels) 
lda_topicmodel <- model_LDA(dtm, k=20, control=list(seed=1234)) 

RのLDA関数を使用して潜在的なディリクレ割り当てを実行しました。S4オブジェクト形式のLDAがあります。RのLDA出力を単語トピック行列に変換するには?

Rのワードトピックマトリックスとドキュメントトピックマトリックスに変換するにはどうすればよいですか?

残念ながら、タイプ 'S4'のオブジェクトはサブセット化できません。ですから、データのサブセットをコピーして使用する必要がありました。

Topic 1  Topic 2 Topic 3 Topic 4 Topic 5  Topic 6 Topic 7   Topic 8 Topic 9  Topic 10  
[1,] "flooding" "beach" "sets" "flooding" "storm"  "fwy"  "storms"  "flooding" "socal"  "rain"  
[2,] "erosion" "long" "alltime" "just"  "flooding" "due"  "thunderstorms" "via"  "major"  "california" 
[3,] "cause"  "abc7" "rain" "almost" "years"  "closures" "flash"   "public" "throughout" "nearly"  
[4,] "emergency" "day"  "slides" "hardcore" "mudslides" "avoid" "continue"  "asks"  "abc7"  "southern" 
[5,] "highway" "history" "last" "spun"  "snow"  "latest" "possible"  "call"  "streets" "storms" 



Topic 11 Topic 12 Topic 13 Topic 14  Topic 15  Topic 16 Topic 17 Topic 18 Topic 19  Topic 20  
[1,] "abc7" "abc7"  "like" "widespread" "widespread" "across" "rainfall" "flooding" "flooding" "vehicles" 
[2,] "beach" "flooding" "closed" "batters"  "biggest"  "can" "record" "region" "storm"  "several"  
[3,] "long" "stranded" "live" "california" "evacuations" "stay" "breaks" "reported" "california" "getting"  
[4,] "fwy" "county" "raining" "evacuations" "mudslides" "home" "long"  "corona" "causes"  "floodwaters" 
[5,] "710" "san"  "blog" "mudslides" "years"  "wires" "beach" "across" "related" "stranded" 

画像は、各トピック内の単語のサブセットを含む:LDA word-topic Iが示すように、単語トピックマトリックスのようなCSVファイルにS4オブジェクトの内容を記述したい: Word-Topic Matrix

+0

ハッピーこれに答えるために。しかし、あなたが提供してください:最小限のデータセット、あなたが得ている結果は何ですか?あなたはアウトプットとして何を期待していますか? – lizzie

+0

ここをクリックしてください:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-exampleこれらの情報で元の投稿を編集する必要があります – lizzie

+0

@lizzie。私はこれを従来のS4フォーマットのLDAからマトリックスに変換し、それをcsvファイルに書きたいと思っています。何か案は? – Sisir

答えて

1

私はあなたのデータを再現できなかったので、私はRのデータを使っています。

# load the libraries 
library(topicmodels) 
library(tm) 

# load the data we'll be using 
data("AssociatedPress") 

# estimate a LDA model using the VEM algorithm (default) 
# I'll be using the number of k (number of topics) being 2 
# just as a example 
ap_lda <- LDA(AssociatedPress, 
       k = 2, 
       control = list(seed = 1234)) 

# get all the terms in a dataframe 
as.data.frame(terms(ap_lda, dim(ap_lda)[1])) 

出力は次のようになります。

Topic 1 Topic 2 
1 percent   i 
2 million president 
3  new government 
4 year  people 
5 billion  soviet 
6 last  new 
関連する問題