你以為川普的推特都是他自己寫的?資料可不這麼認為!
寫在前面
近日,一直以“推特治國”聞名的川普正式宣誓就任了美國第 45 任總統。
川普這次在美國大選中勝出,他的推特也發揮了巨大的作用。相比大多數總統競選人來說,他們都沒時間自己發推。但推特玩的風生水起的川普卻表示,他的推特都是自己發的……
那麼事實真的是這樣嗎?
有個美國網友發現川普發推特有兩個客戶端。一個安卓,另一個是 iPhone 。
而且這位細心的網友還發現,一些言辭激烈的推都來自安卓;而畫風比較正常的推都來自 iPhone。
這一發現,也引起了資料分析師 David Robinson 的注意。David 注意到當川普發祝賀內容時,是通過 iPhone ;而當他抨擊競選對手時而是通過安卓。而且兩個不同客戶端通常發推的時間也不太相同。
本著科學嚴謹的態度,程式設計師小哥決定讓資料說話,於是做了程式,抓取分析了川普發過的推,終於發現了一些模式。並且通過統計,圖表,最終他基本確定,川普的推特並不是他一個人寫的。
資料證明,安卓端和iPhone發的推分別是兩個人所寫的。而且發推時間,使用標籤,加連結,轉發的方式也截然不同。同時,安卓端發的內容更加激烈和消極。
如果就像川普採訪中所說他使用的手機是三星 Galaxy ,我們可以確信用安卓發推的是川普本人,用 iPhone 發的大概是他的團隊助理。
發推時間對比
首先用 twitteR 包中的 userTimeline 函式匯入川普發推的時間資料:
♦ library ( dplyr )
♦ library ( purrr )
♦ library ( twitteR )
# You'd need to set global options with an authenticated appsetup_twitter_oauth(getOption("twitter_consumer_key"), getOption("twitter_consumer_secret"), getOption("twitter_access_token"), getOption("twitter_access_token_secret")) # We can request only 3200 tweets at a time; it will return fewer # depending on the APItrump_tweets <- userTimeline("realDonaldTrump", n = 3200)trump_tweets_df <- tbl_df(map_df(trump_tweets, as.data.frame)) # if you want to follow along without setting up Twitter authentication, # just use my dataset:load(url("http://varianceexplained.org/files/trump_tweets_df.rda"))
稍微清理下資料,提取原始檔。(在此只分析來自 iPhone 和 Android tweet 的資料,除去很少一部分發自網頁客戶端和 iPad 的推文)。
library(tidyr)
tweets <- trump_tweets_df %>%
select(id, statusSource, text, created) %>%
extract(statusSource, "source", "Twitter for (.*?)<") %>%
filter(source %in% c("iPhone", "Android"))
分析的資料包括來自 iPhone 的 628 條推文,來自 Android 的 762 條推文。
主要考慮推文是在一天內什麼時間釋出的,在此我們可以發現區別:
♦ library(lubridate)
♦ library(scales)
tweets %>%
count(source, hour = hour(with_tz(created, "EST"))) %>%
mutate(percent = n / sum(n)) %>%
ggplot(aes(hour, percent, color = source)) +
geom_line() +
scale_y_continuous(labels = percent_format()) +
labs(x = "Hour of day (EST)",
y = "% of tweets",
color = "")
川普一般習慣早上發推,而他的助理會集中在下午或晚上發推。
發文習慣對比
當川普的安卓手機轉推時,習慣用雙引號引用這整句話。
而 iPhone 轉推時,一般不使用雙引號。
安卓手機: 500 多條推文沒有雙引號,200 多條有雙引號
iPhone:幾乎沒有雙引號
與此同時,在分享連結和圖片時,安卓和 iPhone 也大不相同。
tweet_picture_counts <- tweets %>%
filter(!str_detect(text, '^"')) %>%
count(source,
picture = ifelse(str_detect(text, "t.co"),
"Picture/link", "No picture/link"))
ggplot(tweet_picture_counts, aes(source, n, fill = picture)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "", y = "Number of tweets", fill = "")
資料證明 iPhone 端 發的推文很多會附上圖片,連結。內容也以宣傳為主。
比如下面這條:
而川普安卓端發的推文沒有圖片、連結,更多是直接的文字,比如:
用詞對比
在對比安卓和 iPhone 用詞區別時,David 用到了他和 Julia Silge 一起編寫的 tidytext 包。
用 unnest_tokensfunction
把句子分解為單獨的詞:
library(tidytext)
reg <- "([^A-Za-z\d#@']|'(?![A-Za-z\d#@]))"tweet_words <- tweets %>%
filter(!str_detect(text, '^"')) %>%
mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\d]+|&", "")) %>%
unnest_tokens(word, text, token = "regex", pattern = reg) %>%
filter(!word %in% stop_words$word,
str_detect(word, "[a-z]"))
tweet_words
## # A tibble: 8,753 x 4
## id source created word
## <chr> <chr> <time> <chr>
## 1 676494179216805888 iPhone 2015-12-14 20:09:15 record
## 2 676494179216805888 iPhone 2015-12-14 20:09:15 health
## 3 676494179216805888 iPhone 2015-12-14 20:09:15 #makeamericagreatagain
## 4 676494179216805888 iPhone 2015-12-14 20:09:15 #trump2016
## 5 676509769562251264 iPhone 2015-12-14 21:11:12 accolade
## 6 676509769562251264 iPhone 2015-12-14 21:11:12 @trumpgolf
## 7 676509769562251264 iPhone 2015-12-14 21:11:12 highly
## 8 676509769562251264 iPhone 2015-12-14 21:11:12 respected
## 9 676509769562251264 iPhone 2015-12-14 21:11:12 golf
## 10 676509769562251264 iPhone 2015-12-14 21:11:12 odyssey
## # ... with 8,743 more rows
總體來說川普推文中有哪些常用詞呢?
在此基礎上我們再來分別看安卓和 iPhone 常用詞的區別。
android_iphone_ratios <- tweet_words %>%
count(word, source) %>%
filter(sum(n) >= 5) %>%
spread(source, n, fill = 0) %>%
ungroup() %>%
mutate_each(funs((. + 1) / sum(. + 1)), -word) %>%
mutate(logratio = log2(Android / iPhone)) %>%
arrange(desc(logratio))
結論
· 帶標籤的推文基本來自 iPhone 。
· iPhone 推文中常用詞有宣傳性的詞,比如:“參加”,“明天”,“晚上 7 點”。
· 安卓的推文常用有強烈情緒性的詞彙,“差勁”,“瘋了”,“軟弱”,“傻瓜”等等。
情感分析
安卓和 iPhone 推文在情感上也有很大的差異,讓我們來量化一下。用到 tidytext 當中的NRC Word-Emotion Association 詞典,主要把用詞聯絡以下十種情緒分析:積極,消極,憤怒,期待,厭惡,恐懼,快樂,悲傷,驚訝,信任。
nrc <- sentiments %>%
filter(lexicon == "nrc") %>%
dplyr::select(word, sentiment)
nrc
## # A tibble: 13,901 x 2
## word sentiment
## <chr> <chr>
## 1 abacus trust
## 2 abandon fear
## 3 abandon negative
## 4 abandon sadness
## 5 abandoned anger
## 6 abandoned fear
## 7 abandoned negative
## 8 abandoned sadness
## 9 abandonment anger
## 10 abandonment fear
## # ... with 13,891 more rows
為了分別計算安卓和 iPhone 推文的情感,可以把不同用詞分類。
sources <- tweet_words %>%
group_by(source) %>%
mutate(total_words = n()) %>%
ungroup() %>%
distinct(id, source, total_words)
by_source_sentiment <- tweet_words %>%
inner_join(nrc, by = "word") %>%
count(sentiment, id) %>%
ungroup() %>%
complete(sentiment, id, fill = list(n = 0)) %>%
inner_join(sources) %>%
group_by(source, sentiment, total_words) %>%
summarize(words = sum(n)) %>%
ungroup()
head(by_source_sentiment)
## # A tibble: 6 x 4
## source sentiment total_words words
## <chr> <chr> <int> <dbl>
## 1 Android anger 4901 321
## 2 Android anticipation 4901 256
## 3 Android disgust 4901 207
## 4 Android fear 4901 268
## 5 Android joy 4901 199
## 6 Android negative 4901 560
(比如,我們可以看到安卓推文中 4901 個詞中 321 個詞與情感“憤怒”有關。)
同時可以用 Poisson test 分析,比起 iPhone ,安卓推文更喜歡使用帶強烈情緒的詞。
library(broom)
sentiment_differences <- by_source_sentiment %>%
group_by(sentiment) %>%
do(tidy(poisson.test(.$words, .$total_words)))
sentiment_differences
## Source: local data frame [10 x 9]
## Groups: sentiment [10]
##
## sentiment estimate statistic p.value parameter conf.low
## (chr) (dbl) (dbl) (dbl) (dbl) (dbl)
## 1 anger 1.492863 321 2.193242e-05 274.3619 1.2353162
## 2 anticipation 1.169804 256 1.191668e-01 239.6467 0.9604950
## 3 disgust 1.677259 207 1.777434e-05 170.2164 1.3116238
## 4 fear 1.560280 268 1.886129e-05 225.6487 1.2640494
## 5 joy 1.002605 199 1.000000e+00 198.7724 0.8089357
## 6 negative 1.692841 560 7.094486e-13 459.1363 1.4586926
## 7 positive 1.058760 555 3.820571e-01 541.4449 0.9303732
## 8 sadness 1.620044 303 1.150493e-06 251.9650 1.3260252
## 9 surprise 1.167925 159 2.174483e-01 148.9393 0.9083517
## 10 trust 1.128482 369 1.471929e-01 350.5114 0.9597478
## Variables not shown: conf.high (dbl), method (fctr), alternative (fctr)
我們可以用 95% 的置信區間來明確二者的區別:
從而我們可知,川普安卓的推文比起 iPhone ,使用“厭惡”“悲傷”“恐懼”“憤怒”等消極情緒詞的比例高 40-80%
在資料探勘下
川普推特背後的團隊就這麼被扒了個精光
所以,看川普的推特,只要看安卓端的就好了。
但據報道,上任後的川普必須使用一部由美國特工處認證的安全加密手機,以替換他之前使用的安卓系統手機。據稱前總統奧巴馬就無法通過安全手機發推文,那使用安全手機後,川普還能繼續愉快的“推特治國”嗎?