-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocialDataAnalysis.R
156 lines (72 loc) · 4.27 KB
/
SocialDataAnalysis.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
library(tidyverse)
library(jsonlite)
library(sentimentr)
#Change name of file as needed
file = "D:/Users/Kurt/Documents/Research/Dissertation/Data/tweets/01152020/economy01152020.json"
#Import file as a dataframe, limit to first 2000 tweets
current_day = fromJSON(file) %>% as.data.frame()
current_day = current_day[1:2000,]
#Select only the handles and the text of each tweet
handles_text = current_day %>% select(5, 6)
#Identify separate sentences in each tweet
tweet_text_sentences = get_sentences(handles_text$tweets.text)
#Score the sentiment of each sentence and then average them together to form a single sentiment score for each tweet
current_day_sentiment_scores = sentiment_by(tweet_text_sentences) %>%
mutate(tweets.screen_name = handles_text$tweets.screen_name) %>%
select(-element_id)
#Reorder columns for readability
current_day_sentiment_scores = current_day_sentiment_scores[,c(4,3,2,1)]
#Calculate sentiment for the day's corpus
current_day_corpus_sentiment = mean(current_day_sentiment_scores$ave_sentiment)
current_day_corpus_sentiment
#Remove bots from individual analysis
current_day_bots_removed = subset(current_day, !(current_day$tweets.screen_name %in% bots$user.screen_name))
#Select only the handles and the text of each tweet
handles_text = current_day_bots_removed %>% select(5, 6)
#Identify separate sentences in each tweet
tweet_text_sentences = get_sentences(handles_text$tweets.text)
#Score the sentiment of each sentence and then average them together to form a single sentiment score for each tweet
current_day_sentiment_scores = sentiment_by(tweet_text_sentences) %>%
mutate(tweets.screen_name = handles_text$tweets.screen_name) %>%
select(-element_id)
#Reorder columns for readability
current_day_sentiment_scores = current_day_sentiment_scores[,c(4,3,2,1)]
#Calculate sentiment for the day's non-bot individuals
sentiment_by_individual = current_day_sentiment_scores %>% group_by(tweets.screen_name) %>%
summarise(individual_sentiment = mean(ave_sentiment))
current_day_individual_sentiment = mean(sentiment_by_individual$individual_sentiment)
current_day_individual_sentiment
#Remove verified users from individual analysis
current_day_bots_removed_no_verifieds = subset(current_day_bots_removed, tweets.verified == FALSE)
#Select only the handles and the text of each tweet
handles_text = current_day_bots_removed_no_verifieds %>% select(5, 6)
#Identify separate sentences in each tweet
tweet_text_sentences = get_sentences(handles_text$tweets.text)
#Score the sentiment of each sentence and then average them together to form a single sentiment score for each tweet
current_day_sentiment_scores = sentiment_by(tweet_text_sentences) %>%
mutate(tweets.screen_name = handles_text$tweets.screen_name) %>%
select(-element_id)
#Reorder columns for readability
current_day_sentiment_scores = current_day_sentiment_scores[,c(4,3,2,1)]
#Calculate sentiment for the day's non-bot, not verified individuals
sentiment_by_individual = current_day_sentiment_scores %>% group_by(tweets.screen_name) %>%
summarise(individual_sentiment = mean(ave_sentiment))
current_day_individual_sentiment_no_verifieds = mean(sentiment_by_individual$individual_sentiment)
current_day_individual_sentiment_no_verifieds
#Select only bots from individual analysis
current_day_bots = subset(current_day, (current_day$tweets.screen_name %in% bots$user.screen_name))
#Select only the handles and the text of each tweet
handles_text = current_day_bots %>% select(5, 6)
#Identify separate sentences in each tweet
tweet_text_sentences = get_sentences(handles_text$tweets.text)
#Score the sentiment of each sentence and then average them together to form a single sentiment score for each tweet
current_day_sentiment_scores = sentiment_by(tweet_text_sentences) %>%
mutate(tweets.screen_name = handles_text$tweets.screen_name) %>%
select(-element_id)
#Reorder columns for readability
current_day_sentiment_scores = current_day_sentiment_scores[,c(4,3,2,1)]
#Calculate sentiment for the day's non-bot individuals
sentiment_by_individual = current_day_sentiment_scores %>% group_by(tweets.screen_name) %>%
summarise(individual_sentiment = mean(ave_sentiment))
current_day_bot_sentiment = mean(sentiment_by_individual$individual_sentiment)
current_day_bot_sentiment