Skip to content

Commit

Permalink
Predicts Bitcoin Prices with 16 Machine Learning Algorithms
Browse files Browse the repository at this point in the history
To implement the project you have to do your own authentication with your Twitter account. From that with 'tweetsPull' class you can pull tweets.
All the datasets are prepared. Untill April.
  • Loading branch information
ShaomiR committed Apr 12, 2018
0 parents commit 60fc1e0
Show file tree
Hide file tree
Showing 76 changed files with 29,716 additions and 0 deletions.
23 changes: 23 additions & 0 deletions DecisionTreeRegression.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
setwd("C:/Users/HP/Desktop/thss")
dataset = read.csv("sentiment.csv")

library(rpart)

regressorDT = rpart(formula = price ~ .,
data = dataset,
control = rpart.control(minsplit = 1))

y_predDT = predict(regressorDT , data.frame(anger= 15 , anticipation= 50 , disgust = 10 , fear = 19 , joy = 23 , sadness =16 , surprise = 13 , trust = 58, negative = 29 , positive = 103))

library(ggplot2)

x_grid = seq(min(dataset$positive), max(dataset$positive),0.01)

ggplot()+

geom_point(aes(x=dataset$positive , y = dataset$price),
color = 'blue') +
geom_line(aes(x= dataset$positive , y= predict(regressorDT , newdata = dataset)),
color = 'green') +
xlab('sentiments') +
ylab('price')
21 changes: 21 additions & 0 deletions SVR.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
setwd("C:/Users/HP/Desktop/thss")
dataset = read.csv("sentiment.csv")

library(e1071)

regressor = svm(formula = price ~ .,
data = dataset,
type = 'eps-regression')

y_predSVR = predict(poly_reg , data.frame(anger= 15 , anticipation= 46 , disgust = 2 , fear = 15 , joy = 29 , sadness = 8 , surprise = 14 , trust = 58, negative = 28 , positive = 100))

library(ggplot2)

ggplot()+

geom_point(aes(x=dataset$positive , y = dataset$price),
color = 'blue') +
geom_line(aes(x= dataset$positive , y= predict(regressor , newdata = dataset)),
color = 'green') +
xlab('sentiments') +
ylab('price')
97 changes: 97 additions & 0 deletions TweetsPull.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
setwd("C:/Users/HP/Desktop/thss")

library(twitteR)
library(ROAuth)
library(plyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(httr)
library(wordcloud)
library(RCurl)
library(syuzhet)
library(tm)
library(sentimentr)
library(SentimentAnalysis)

oauth_endpoint(authorize = "https://api.twitter.com/oauth" , access = "https://api.twitter.com/oauth/access_token")

download.file(url = "http://curl.haxx.se/ca/cacert.pem" , destfile = "cacert.pem")
reqURL <- 'https://api.twitter.com/oauth/request_token'
accessURL <- 'https://api.twitter.com/oauth/access_token'
authURL <- 'https://api.twitter.com/oauth/authorize'

consumerKey = "D57e41bCCKX8AwILpQbj5GTmk"
consumerSecret = "RQ0ws6puGWKq7OQMJ8gqucKyl1FXYez7xhpC5Uj1WLoaEIwRJn"
accesstoken = "962001620727771136-1HaBpIqN6R4i9th7iXa30iZO3SfVACC"
accesssecret = "zZ6MkCGraJueoeuMeE9Yj7MxIbD6KREt3FRCvGVijXOLi"

Cred <- OAuthFactory$new(consumerKey = consumerKey ,
consumerSecret = consumerSecret ,
requestURL = reqURL ,
accessURL = accessURL,
authURL = authURL)

Cred$handshake(cainfo = system.file('CurlSSL' , 'cacert.pem' , package = 'RCurl'))

save(Cred, file = 'twitter authentication.Rdata')

load('twitter authentication.Rdata')

setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = accesssecret)

some_tweets = searchTwitter("cryptocurrency" ,n = 500 , since = "2018-03-17" , until="2018-03-18" , lang = "en" )

some_tweets

length.some_tweets <- length(some_tweets)
length.some_tweets

some_tweets.df <- ldply(some_tweets , function(t) t$toDataFrame())
write.csv(some_tweets.df , "rawtweetsmar17-18.csv")

some_txt = sapply(some_tweets, function(x) x$getText())

some_txt1 = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "",some_txt)

some_txt2 = gsub("http[^[:blank:]]+", "",some_txt1)

some_txt3 = gsub("@\\w+","",some_txt2)

some_txt4 = gsub("[[:punct:]]", " ",some_txt3)

some_txt5 = gsub("[^[:alnum:]]"," ",some_txt4)

write.csv(some_txt5, "tweetsmar17-18.csv")

some_txt6 <- Corpus(VectorSource(some_txt5))
some_txt6 <- tm_map(some_txt6, removePunctuation)
some_txt6 <- tm_map(some_txt6, content_transformer(tolower))
some_txt6 <- tm_map(some_txt6, removeWords, stopwords("english"))
some_txt6 <- tm_map(some_txt6, stripWhitespace)

pal <- brewer.pal(8, "Dark2")

wordcloud(some_txt6, min.freq = 5, max.words = Inf ,width=1000, height=1000, random.order = FALSE , color=pal)

get_nrc_sentiment("I bought an iPhone a few days ag0. It is such a nice phone, although a little large. The touch screen is cool. The voice quality is clear too. I simply love it!")

mysentiment <- get_nrc_sentiment(some_txt5)
SentimentScores <- data.frame(colSums(mysentiment[,]))
names(SentimentScores) <- "Score"
SentimentScores <- cbind("sentiment" = rownames(SentimentScores), SentimentScores)
rownames(SentimentScores) <- NULL
ggplot(data = SentimentScores, aes(x = sentiment, y= Score)) + geom_bar(aes(fill = sentiment),stat = "identity")+ theme(legend.position = "none")+ xlab("Sentiment")+ylab("Score")+ggtitle("Total Sentiment Score Based On Tweets")
write.csv(SentimentScores,"sentimar17-18.csv")

sentiment <-analyzeSentiment(some_txt5)
write.csv(convertToBinaryResponse(sentiment)$SentimentQDAP , "binarylabel.csv")

documents <- c(some_txt5)

sentimentnum <- analyzeSentiment(documents)

sentimentnum$SentimentQDAP



127 changes: 127 additions & 0 deletions _Rhistory
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
install.packages("syuzhet")
library(twitteR)
library(ROAuth)
library(plyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(httr)
library(wordcloud)
library(RCurl)
library(syuzhet)
oauth_endpoint(authorize = "https://api.twitter.com/oauth" , access = "https://api.twitter.com/oauth/access_token")
download.file(url = "http://curl.haxx.se/ca/cacert.pem" , destfile = "cacert.pem")
reqURL <- 'https://api.twitter.com/oauth/request_token'
accessURL <- 'https://api.twitter.com/oauth/access_token'
authURL <- 'https://api.twitter.com/oauth/authorize'
consumerKey = "D57e41bCCKX8AwILpQbj5GTmk"
consumerSecret = "RQ0ws6puGWKq7OQMJ8gqucKyl1FXYez7xhpC5Uj1WLoaEIwRJn"
accesstoken = "962001620727771136-482ck4DcQ44eYulYCGtnuhqUrdzjDvk"
accesssecret = "CpAtdB8Fryot85hp1rsXJVvt7ZLctnEupDDuTEsiPnCtx"
Cred <- OAuthFactory$new(consumerKey=consumerKey , consumerSecret = consumerSecret , requestURL=reqURL , accessURL=accessURL, authURL=authURL)
Cred$handshake(cainfo = system.file('CurlSSL' , 'cacert.pem' , package = 'RCurl'))
6486828
Cred$handshake(cainfo = system.file('CurlSSL' , 'cacert.pem' , package = 'RCurl'))
save(Cred, file = 'twitter authentication.Rdata')
load('twitter authentication.Rdata')
setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = access_secret)
setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = accesssecret)
some_tweets = searchTwitter("trump" , n=20, since = 2017-12-01 , lang = "en")
some_tweets = searchTwitter("trump" , n=20, since = "2017-12-01" , lang = "en")
install.packages("sentimentr")
Cred <- OAuthFactory$new(consumerKey = consumerKey ,
consumerSecret = consumerSecret ,
requestURL = reqURL ,
accessURL = accessURL ,
authURL = authURL)
Cred <- OAuthFactory$new(consumerKey = consumerKey ,
consumerSecret = consumerSecret ,
requestURL = reqURL ,
accessURL = accessURL,
authURL = authURL)
Cred$handshake(cainfo = system.file('CurlSSL' , 'cacert.pem' , package = 'RCurl'))
save(Cred, file = 'twitter authentication.Rdata')
load('twitter authentication.Rdata')
setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = accesssecret)
consumerKey = "D57e41bCCKX8AwILpQbj5GTmk"
consumerSecret = "RQ0ws6puGWKq7OQMJ8gqucKyl1FXYez7xhpC5Uj1WLoaEIwRJn"
accesstoken = "962001620727771136-1HaBpIqN6R4i9th7iXa30iZO3SfVACC"
accesssecret = "zZ6MkCGraJueoeuMeE9Yj7MxIbD6KREt3FRCvGVijXOLi"
setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = accesssecret)
Cred <- OAuthFactory$new(consumerKey = consumerKey ,
consumerSecret = consumerSecret ,
requestURL = reqURL ,
accessURL = accessURL,
authURL = authURL)
Cred$handshake(cainfo = system.file('CurlSSL' , 'cacert.pem' , package = 'RCurl'))
load('twitter authentication.Rdata')
setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = accesssecret)
some_tweets = searchTwitter("trump" , n=20, since = "2017-12-01" , lang = "en")
some_tweets
some_tweets = searchTwitter("trump" , n=2000, since = "2017-12-01" , lang = "en")
setwd("C:/Users/HP/Desktop/thss")
library(twitteR)
library(ROAuth)
library(plyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(httr)
library(wordcloud)
library(RCurl)
library(syuzhet)
library(tm)
library(sentimentr)
library(SentimentAnalysis)
oauth_endpoint(authorize = "https://api.twitter.com/oauth" , access = "https://api.twitter.com/oauth/access_token")
download.file(url = "http://curl.haxx.se/ca/cacert.pem" , destfile = "cacert.pem")
reqURL <- 'https://api.twitter.com/oauth/request_token'
accessURL <- 'https://api.twitter.com/oauth/access_token'
authURL <- 'https://api.twitter.com/oauth/authorize'
consumerKey = "D57e41bCCKX8AwILpQbj5GTmk"
consumerSecret = "RQ0ws6puGWKq7OQMJ8gqucKyl1FXYez7xhpC5Uj1WLoaEIwRJn"
accesstoken = "962001620727771136-1HaBpIqN6R4i9th7iXa30iZO3SfVACC"
accesssecret = "zZ6MkCGraJueoeuMeE9Yj7MxIbD6KREt3FRCvGVijXOLi"
Cred <- OAuthFactory$new(consumerKey = consumerKey ,
consumerSecret = consumerSecret ,
requestURL = reqURL ,
accessURL = accessURL,
authURL = authURL)
Cred$handshake(cainfo = system.file('CurlSSL' , 'cacert.pem' , package = 'RCurl'))
save(Cred, file = 'twitter authentication.Rdata')
load('twitter authentication.Rdata')
setup_twitter_oauth(consumer_key = consumerKey , consumer_secret = consumerSecret , access_token = accesstoken , access_secret = accesssecret)
some_tweets = searchTwitter("cryptocurrency" ,n = 500 , since = "2018-03-17" , until="2018-03-18" , lang = "en" )
some_tweets
length.some_tweets <- length(some_tweets)
length.some_tweets
some_tweets.df <- ldply(some_tweets , function(t) t$toDataFrame())
write.csv(some_tweets.df , "rawtweetsmar17-18.csv")
some_txt = sapply(some_tweets, function(x) x$getText())
some_txt1 = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "",some_txt)
some_txt2 = gsub("http[^[:blank:]]+", "",some_txt1)
some_txt3 = gsub("@\\w+","",some_txt2)
some_txt4 = gsub("[[:punct:]]", " ",some_txt3)
some_txt5 = gsub("[^[:alnum:]]"," ",some_txt4)
write.csv(some_txt5, "tweetsmar17-18.csv")
some_txt6 <- Corpus(VectorSource(some_txt5))
some_txt6 <- tm_map(some_txt6, removePunctuation)
some_txt6 <- tm_map(some_txt6, content_transformer(tolower))
some_txt6 <- tm_map(some_txt6, removeWords, stopwords("english"))
some_txt6 <- tm_map(some_txt6, stripWhitespace)
pal <- brewer.pal(8, "Dark2")
wordcloud(some_txt6, min.freq = 5, max.words = Inf ,width=1000, height=1000, random.order = FALSE , color=pal)
get_nrc_sentiment("I bought an iPhone a few days ag0. It is such a nice phone, although a little large. The touch screen is cool. The voice quality is clear too. I simply love it!")
mysentiment <- get_nrc_sentiment(some_txt5)
SentimentScores <- data.frame(colSums(mysentiment[,]))
names(SentimentScores) <- "Score"
SentimentScores <- cbind("sentiment" = rownames(SentimentScores), SentimentScores)
rownames(SentimentScores) <- NULL
ggplot(data = SentimentScores, aes(x = sentiment, y= Score)) + geom_bar(aes(fill = sentiment),stat = "identity")+ theme(legend.position = "none")+ xlab("Sentiment")+ylab("Score")+ggtitle("Total Sentiment Score Based On Tweets")
write.csv(SentimentScores,"sentimar17-18.csv")
sentiment <-analyzeSentiment(some_txt5)
write.csv(convertToBinaryResponse(sentiment)$SentimentQDAP , "binarylabel.csv")
documents <- c(some_txt5)
sentimentnum <- analyzeSentiment(documents)
sentimentnum$SentimentQDAP
source('C:/Users/HP/Desktop/thesis.R')
1 change: 1 addition & 0 deletions _gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.httr-oauth
Empty file added _httr-oauth
Empty file.
Loading

0 comments on commit 60fc1e0

Please sign in to comment.