forked from KaroRonty/FinancialDeepLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment.R
58 lines (53 loc) · 2.15 KB
/
sentiment.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
library(tm)
library(tidyverse)
library(lubridate)
library(data.table)
library(SentimentAnalysis)
coindesk_news <- fread("https://raw.githubusercontent.com/Zmaznevegor/defi_uncertainty/main/data/news/coindesk.csv") %>%
mutate(date = as_date(ymd_hms(date))) %>%
as_tibble()
coindesk_news_dtm <- coindesk_news %>%
pull(text) %>%
VectorSource() %>%
Corpus() %>%
DocumentTermMatrix(control = list(removePunctuation = TRUE,
stopwords = TRUE,
removeNumbers = TRUE,
setmming = TRUE,
tolower = TRUE,
wordLengths = c(5, 20)))
analyzed_sentiment <- coindesk_news %>%
mutate(SentimentLM = analyzeSentiment(
coindesk_news_dtm,
removeStopwords = TRUE,
stemming = TRUE,
rules = list("SentimentLM" = list(ruleSentiment,
loadDictionaryLM()))) %>%
pull(SentimentLM))
crypto_data <- map(list.files(pattern = "*.csv",
full.names = TRUE),
~read.csv(.x)) %>%
reduce(bind_rows) %>%
mutate(Date = as_date(ymd_hms(Date)))
analyzed_sentiment %>%
filter(SentimentLM != 0) %>%
mutate(lower_text = tolower(text),
mentioned_coin = case_when(
str_detect(lower_text, "binance coin|bnb") ~ "Binance Coin",
str_detect(lower_text, "bitcoin|btc") ~ "Bitcoin",
str_detect(lower_text, "ethereum|eth") ~ "Ethereum",
str_detect(lower_text, "iota|miota") ~ "IOTA",
str_detect(lower_text, "ripple|xrp") ~ "XRP",
TRUE ~ "Other")) %>%
filter(!str_detect(lower_text, "sponsored")) %>%
ggplot(aes(date, SentimentLM, color = mentioned_coin)) +
geom_point() +
geom_smooth(color = "black") +
geom_hline(yintercept = 0, color = "gray20", linetype = "dashed") +
facet_wrap(~mentioned_coin) +
scale_x_date(breaks = seq.Date(ymd("2016-01-01"),
ymd("2021-01-01"),
"1 year"),
labels = 2016:2021) +
theme_minimal() +
theme(legend.position = "none")