这本书写的还是不错的,都很基础, 常见的内容也都涉及到了,就是最后一章这个代码呀。。。真的不敢恭维。 这里做一个详细的勘误。 如果有不对的地方也欢迎指正。
首先,最后一章是译者自己写的吧。。。利用微博的api,不过微博这玩意那么多限制。。。结果确实跟作者高的不同, 话不多说进入勘误。
代码: 1. 原作者说了如果有问题需要用utf-8编码重新打开, 改成utf8也不麻烦,为什么不能好好改一下呢? 中间大量的中文即使改成utf8 也有乱码;
2.代码行26 sys.setlocale, 应该是Sys.setlocale。。。如果发布之前代码跑过了,不知道为啥会出现这种情况?? 书上倒是没印错。
3.Rwordseg 包安装采用了install.pacakges()的方式, 去网上查就知道这经常会报错, 需要配合rjava安装,然后之前需要配置好java环境,即使如此也会报错= = , 毕竟R不是原来的R了。 只能下载然后从zip安装= = 。 。。。
- 行 14/15 wobject <- fromJSON(data) length(wobject$statuses) 得不到100条微博, 而且最后结尾也不完整。
反正代码是公开的, 我po一下原来的第15章好了~
叙述部分: 上来就说创建一个应用,明明下面写的是创建网页应用,一开始还得点移动应用而不是网页接入。。。
为什么不能写清楚一些, 说明一下点击移动应用 能废多少时间啊摔
#
# 第15章
# SNSデータの分析
#
# Twitter REST API
# GET search/tweets
# https://syncer.jp/twitter-api-matome/get/search/tweets
install.packages("twitteR")
install.packages("ROAuth")
install.packages("base64enc")
library(twitteR)
library(ROAuth)
library(base64enc)
APIKey <- "****"
APISecret <- "****"
accessToken <- "****"
accessSecret <- "****"
setup_twitter_oauth(APIKey, APISecret, accessToken, accessSecret)
searchword <- "ガラケー スマホ"
searchquery <- iconv(paste0(searchword," AND -filter:links AND -RT"), to="UTF-8")
tw.df <- twListToDF(searchTwitter(searchquery,
since=as.character(Sys.Date()-8),
until=as.character(Sys.Date()), n=10000))
names(tw.df)
# ツイート数の集計
library(dplyr)
library(ggplot2)
# 日別の集計
tw.daily <- tw.df %>%
mutate(twdate=as.Date(created)) %>%
group_by(twdate) %>% summarize(cnt = n())
tw.daily
qplot(twdate, cnt, data=tw.daily, geom="bar", stat="identity")
# 印刷用グラフ
cairo_pdf("tw.daily.pdf", width=8, height=8, family="MixMix 1P")
qplot(twdate, cnt, data=tw.daily, geom="bar", stat="identity", fill=I("#666666")) +
theme_bw(base_size=18)
dev.off()
# 時間別の集計
tw.hourly <- tw.df %>%
mutate(twhour=as.POSIXct(format(created, "%Y-%m-%d %H:00:00"))) %>%
group_by(twhour) %>% summarize(cnt = n())
tw.hourly
qplot(twhour, cnt, data=tw.hourly, geom="bar", stat="identity")
# 印刷用グラフ
cairo_pdf("tw.hourly.pdf", width=8, height=8, family="MixMix 1P")
qplot(twhour, cnt, data=tw.hourly, geom="bar", stat="identity", fill=I("#666666")) +
theme_bw(base_size=18)
dev.off()
# RMeCabのインストール
install.packages ("RMeCab", repos = "https://rmecab.jp/R")
library(RMeCab)
# 前処理
tw.txt <- unique(tw.df$text)
tw.txt <- gsub("[[:print:]]", "", tw.txt, perl=TRUE)
tw.txt <- iconv(tw.txt, from="UTF-8", to="CP932", "")
tw.txt <- tw.txt[-grep("^RT", tw.txt)]
tw.dmat <- docMatrixDF(tw.txt, pos = c("名詞"))
dim(tw.dmat)
tw.wcnt <- as.data.frame(apply(tw.dmat, 1, sum))
tw.wcnt <- tw.wcnt[
!(row.names(tw.wcnt) %in% unlist(strsplit(searchword, " "))),
1, drop=FALSE]
tw.wcnt2 <- data.frame(word=as.character(row.names(tw.wcnt)),
freq=tw.wcnt[,1])
tw.wcnt2 <- subset(tw.wcnt2, rank(-freq)<25)
ggplot(tw.wcnt2, aes(x=reorder(word,freq), y=freq)) +
geom_bar(stat="identity", fill="grey", color="black") +
theme_bw(base_size=20) + coord_flip() + xlab("word")
cairo_pdf("wordcount.pdf", family="MigMix 1P", width=12, height=8)
ggplot(tw.wcnt2, aes(x=reorder(word,freq), y=freq)) +
geom_bar(stat="identity", fill="grey", color="black") +
theme_bw(base_size=20) + coord_flip() + xlab("word")
dev.off()
install.packages("wordcloud")
install.packages("RColorBrewer")
library(wordcloud)
library(RColorBrewer)
tw.wcnt <- subset(tw.wcnt, tw.wcnt[, 1] >= 30)
pal <- brewer.pal(8,"Dark2")
cairo_pdf("wordcloud.pdf", family="Meiryo", width=8, height=8)
wordcloud(row.names(tw.wcnt), tw.wcnt[, 1], scale = c(4, .2),
random.order = T, rot.per = .15, colors = pal)
dev.off()
# ネットワーク分析
unlist(RMeCabC("安いガラケーに機種変更したい"))
tw.file <- tempfile()
write(gsub("\n", "", tw.txt), file=tw.file)
tw.bigram <- NgramDF(tw.file, type = 1, N = 2,
c("名詞", "形容詞", "動詞"))
sortlist <- order(tw.bigram[,3],decreasing = TRUE)
tw.bigram <- tw.bigram[sortlist,]
tw.bigram <- subset(tw.bigram, Freq>20)
head(tw.bigram)
install.packages("igraph")
library(igraph)
tw.graph <- graph.data.frame(tw.bigram)
# コミュニティーの抽出
eb <- edge.betweenness.community(tw.graph)
# グラフ描画
cairo_pdf("network.pdf", family="Meiryo", width=8, height=8)
plot(tw.graph, vertex.label=V(tw.graph)$name,
vertex.label.family="Meiryo",
vertex.size=3*log(degree(tw.graph)),
vertex.color=cut_at(eb, 10), edge.arrow.size=0.1,
vertex.label.cex=1, edge.arrow.width=1)
dev.off()
plot(tw.graph, vertex.label=V(tw.graph)$name,
vertex.size=3*log(degree(tw.graph)),
vertex.color=cut_at(eb, 10), edge.arrow.size=0.1,
vertex.label.cex=1, edge.arrow.width=1)
install.packages。。。。 书上写了,代码没写。麻烦好好检查呗