Skip to content

Commit 97a5e47

Browse files
スクリプトを修正
1 parent 57385fd commit 97a5e47

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

Chapter07.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setwd("C:/Users/ishida/TextMining")# など
99
setwd("/Users/ishida/Download/TextMining")# など
1010
### Linux
1111
setwd("/home/ishida/Dropbox/R/Morikita/Version2/")# など
12-
setwd("/myData/Books/morikita/")
12+
setwd("~/myData/Books/morikita/")
1313

1414
### 7.1 沖縄観光への意見データ
1515
okinawa <- read.csv("data/H18koe.csv", stringsAsFactors = TRUE)
@@ -113,12 +113,15 @@ rownames(FM4)
113113
View(FM4)
114114

115115
### 7.3 意見データの対応分析
116-
#install.packages(c("FactoMineR", "factoextra"))
116+
# install.packages(c("FactoMineR", "factoextra"))
117117
library(FactoMineR)
118118
FM4ca <- CA(FM4, graph = FALSE)
119119
## ggplot2 ベースのバイプロットを描く
120120
library(factoextra)
121-
fviz_ca_biplot(FM4ca)
121+
fviz_ca_biplot(FM4ca, labelsize = 6, pointsize = 4)
122+
# +theme(text = element_text(size = 12),
123+
# axis.title = element_text(size = 12),
124+
# axis.text = element_text(size = 12))
122125

123126
# 上記の実行結果の画像で文字化けが生じている場合、以下のようにPDF画像として作成して確認してみてください
124127
# 3行続けて実行することで画像ファイルが作成されます

Chapter09.R

+18-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ setwd("/Users/ishida/Download/TextMining")# など
1111
setwd("/home/ishida/Dropbox/R/Morikita/Version2/")# など
1212

1313

14-
### l9.1 解析の準備
14+
### 9.1 解析の準備
1515
library(RMeCab)
1616
## Windowsの場合は以下の "data/prime/utf" を "data/prime/sjis" にするなど
1717
## 自身の作業環境にあわせて適宜変更
@@ -26,14 +26,13 @@ library(dplyr)
2626
library(magrittr)
2727
## 列名を短縮化する
2828
colnames(prime) %<>% str_replace("_general-policy-speech.txt", "")
29-
colnames(prime) %<>% str_replace("(\\d{4})\\d{4}_(\\d{3})", "\\1_\\2")
29+
colnames(prime) %<>% str_replace("(\\d{4})\\d{4}_(\\d{1,3})", "\\1_\\2")
3030

3131

3232
### 9.3 所信表明演説のクラスター分析
3333
hc <- prime %>% t %>% dist %>% hclust("ward.D2")
3434

35-
#
36-
install.packages("ggdendro")
35+
# install.packages("ggdendro")
3736

3837
library(ggdendro)
3938
ggdendrogram(hc, rotate= TRUE)
@@ -73,7 +72,7 @@ TD_svd$v
7372
t(TD_svd$u[, 1:3]) %*% TD
7473

7574
### 9.6 潜在的意味インデキシングによる分類
76-
install.packages("rgl")
75+
# install.packages("rgl")
7776

7877
prime.svd <- svd(prime)
7978
prime2 <- t(prime.svd$u[, 1:3]) %*% prime
@@ -110,7 +109,7 @@ vignette("rgl")
110109

111110

112111
### 9.7 トピックモデル
113-
install.packages(c("topicmodels","lda"))
112+
# install.packages(c("topicmodels","lda"))
114113

115114
library(RMeCab)
116115

@@ -133,15 +132,16 @@ prime3 <- prime2 %>% select(-c(TERM:POS2))
133132
rownames(prime3) <- prime2$TERM
134133
## 列名は短縮化
135134
colnames(prime3) %<>% str_replace("_general-policy-speech.txt", "")
136-
colnames(prime3) %<>% str_replace("(\\d{4})\\d{4}_(\\d{3})", "\\1_\\2")
135+
colnames(prime3) %<>% str_replace("(\\d{4})\\d{4}_(\\d{1,3})", "\\1_\\2")
137136

137+
## ターム文書行列を作成
138138
library(tm)
139139
prime3a <- prime3 %>% t() %>% as.DocumentTermMatrix(weighting = weightTf)
140140

141141
### 9.7.1 トピックモデルによるモデル推定
142142
library(topicmodels)
143143
## トピックの数を指定
144-
K <- 5
144+
K <- 5
145145
res1 <- prime3a %>% LDA(K)
146146

147147
terms(res1)
@@ -192,6 +192,15 @@ ministersDF <- as.data.frame(ministers) %>%
192192
mutate(num = paste0("No", c(64, 74, 77, 80)))
193193
ministersDF
194194

195+
## 行列をデータフレームに変換し列名を設定
196+
ministersDF <- as.data.frame(ministers) %>%
197+
set_names(paste0("topic", 1:5)) %>%
198+
## num という列を追加
199+
mutate(num = c("64:小泉", "74:鳩山", "77:野田", "80:安倍"))
200+
201+
ministersDF
202+
203+
195204
# install.packages("tidyr")
196205

197206
library(tidyr)
@@ -207,6 +216,6 @@ ministersDF %>% ggplot(aes(x = topic, y = props, fill = num)) + geom_bar(s
207216
# 3行続けて実行することで画像ファイルが作成されます
208217
# RStudio 右のFilesタブで画像ファイルをクリックすることで、適切なビューワー が立ちあがります
209218
cairo_pdf(file = "ministersDF.pdf", family = "JP1")# Mac の場合は family = "HiraKakuProN-W3" と変えてください
210-
x
219+
211220
dev.off()
212221

Chapter10.R

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ setwd("C:/Users/ishida/TextMining")# など
88
### Mac
99
setwd("/Users/ishida/Download/TextMining")# など
1010
### Linux
11-
setwd("/home/ishida/Dropbox/R/Morikita/Version2/")# など
11+
setwd("/home/ishida/Download/TextMining")# など
1212

1313

1414
library(RMeCab)
@@ -18,6 +18,8 @@ res <- docNgram("data/writers", type = 0)
1818

1919
ncol(res) ; nrow(res)
2020

21+
colnames(res) <- c("鴎外:雁", "鴎外:かのように", "鴎外:鶏", "鴎外:ヰタ",
22+
"漱石:永日", "漱石:硝子", "漱石:思い出す", "漱石:夢")
2123

2224
res %>% tail()
2325

@@ -66,18 +68,22 @@ options(digits = 3)
6668

6769
summary(res2_pc)
6870

69-
install.packages("ggfortify")
71+
# install.packages("ggfortify")
7072
library(ggfortify)
7173

7274
library(stringr)
75+
rownames(res2_pc$scores)
7376

77+
# 変数名を日本語にした場合は以下(1)はスキップして、(2)を実行
78+
# (1)変数名がアルファベット表記の場合
7479
rownames(res2_pc$scores) <- res2_pc$scores %>%
7580
rownames() %>%
7681
str_extract("[a-z]+") %>%
7782
paste0(1:8)
7883

84+
# (2) バイプロットの作成
7985
autoplot(res2_pc, label =TRUE, label.size = 8, loadings = TRUE,
80-
loadings.label = TRUE, loadings.label.size = 12,
86+
loadings.label = TRUE, loadings.label.size = 6,
8187
loadings.label.family = "JP1")
8288

8389
# 上記の実行結果の画像で文字化けが生じている場合、以下のようにPDF画像として作成して確認してみてください

res2pc.pdf

-9.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)