ZBLOG

A social and behavioural research group at Western Sydney University is studying social activists. They have consulted you to investigate the flow of information regarding environmental activist Greta Thunberg on Twitter. Researchers have provided...

Load libraries

library(rtweet) library(igraph)

Authenticate Twitter API

token <- create_token( app = “your_app_name”, consumer_key = “your_consumer_key”, consumer_secret = “your_consumer_secret”, access_token = “your_access_token”, access_secret = “your_access_secret” )

Task 1: Followed by Greta

greta_following <- get_friends(“GretaThunberg”, n = 200, token = token) greta_following_filtered <- greta_following[grep(“@”, greta_following\(name), ] top_followers <- greta_following_filtered[order(greta_following_filtered\)followers_count, decreasing = TRUE)[1:12], ] types_of_people <- table(top_followers$description)

Task 2: Followers of Greta

greta_followers <- get_followers(“GretaThunberg”, n=200, token=token) top_gretafollowers<-head(sort(greta_followers\(followers_count,decreasing=TRUE),n=12) top_gretafollower_names<-get_users(top_gretafollowers)\)name gretafollower_tweets<-search_tweets(paste0(top_gretafollower_names,” “,“GretaThunberg”),n=1000,token=token) sentiment_analysis<-lapply(gretafollower_tweets\(text,function(x) classify_emotion(x)\)emotion$category) positive_sentiments<-sapply(sentiment_analysis,function(x) length(which(x==“Positive”))) negative_sentiments<-sapply(sentiment_analysis,function(x) length(which(x==“Negative”))) relationship_with_Greta=data.frame(names=top_gretafollower_names,

                                positive_sentiments=positive_sentiments,
                                negative_sentiments=negative_sentiments,
                                stringsAsFactors=F)

types_of_people_2 <- table(get_users(top_gretafollowers)$description)

Task 3: Bypassing Greta

following <- get_friends(“GretaThunberg”, n = 200, token = token) followers <- get_followers(“GretaThunberg”, n=200, token=token) greta_network <- graph.data.frame(data.frame(from = c(rep(“Greta Thunberg”, times = nrow(following))),

                                          to = following$name))

greta_network <- add_edges(greta_network, data.frame(from = rep(“Greta Thunberg”, times = nrow(followers)),

                                                 to=followers$name), by.name=T)

mutual_friends <- intersect(following\(name, followers\)name) greta_network <- add_edges(greta_network, data.frame(from=rep(mutual_friends,times=2),

                                                 to=c(following[following$name %in% mutual_friends, "name"],
                                                      followers[followers$name %in% mutual_friends,"name"])))

E(greta_network)\(color[E(greta_network)\)to==“Greta Thunberg”]<-“red” V(greta_network)\(color[V(greta_network)\)name==“Greta Thunberg”]<-“red”

Task 4: Graph Statistics

diameter(greta_network) # Calculate diameter of graph graph.density(greta_network) # Calculate density of graph neighbourhood_overlap <- sapply(V(greta_network), function(v) { length(intersect(neighbors(greta_network,v),neighbors(gerta_nework,“Gerta Thunbergs”))) }) names(neighbourhood_overlap)<-V(graph)[“name”] social_capital<-evcent(as.undirected(gretanetwork))$vector

Task 5: Graph Homophily

supporters<-c(top_followers\(name,top_gretafollower_names,"Gerta Thunbergs") non_supporters<-unique(V(greta_network)\)name[!V(greta_network)\(name %in% supporters]) homophily_test <- function(graph, labels) { # Get observed frequency of positive and negative edges between supporters and non-supporters obs_freq_pos <- sum(E(graph)[labels==1 & ends(E(graph),names=FALSE)]\)sign == “+”) obs_freq_neg <- sum(E(graph)[labels==1 & ends(E(graph),names=FALSE)]$sign == “-”) # Calculate expected frequency under null hypothesis of no homophily exp_freq_pos <- (sum(labels == 1)/vcount(graph)) * (sum(degree(graph)[labels != 1])/2) exp_freq_neg <- (sum(labels == 1)/vcount(graph)) * ((vcount(graph)-sum(degree(graph)[labels != 1]))/2) # Calculate test statistic chisq_statistic <- ((obs_freq_pos - exp_freq_pos)^2)/exp_freq_pos +

((obs_freq_neg - exp_freq_neg)^2)/exp_freq_neg

p_value <- pchisq(chisq_statistic, df = 1, lower.tail = FALSE)

return(list(observed_positive = obs_freq_pos, observed_negative = obs_freq_neg,

          expected_positive = exp_freq_pos, expected_negative = exp_freq_neg,
          chi_squared_statistic = chisq_statistic, p_value = p_value))

}

Label nodes as either supporter or non-supporter based on previous tasks

V(greta_network)\(label[V(greta_network)\)name %in% supporters] <- “supporter” V(greta_network)\(label[V(greta_network)\)name %in% non_supporters] <- “non-supporter”

Add sign to edges based on the sentiment analysis from Task 2

E(greta_network)\(sign <- ifelse(gretafollower_tweets\)screen_name %in% top_gretafollower_names &

                              sentiment_analysis == "Positive", "+", "-")

homophily_results <- homophily_test(greta_network, ifelse(V(greta_network)$label==“supporter”, 1, 0))

Task 6: Structural Balance

Label edges as positive or negative based on their association with Greta

E(greta_network)\(sign[E(greta_network)\)to %in% supporters] <- “+” E(greta_network)\(sign[E(greta_network)\)to %in% non_supporters] <- “-” edge_weights<-ifelse(E(greta_network)\(sign=="+",1,-1) weakly_balanced<-balance(graph=greta_network,w=abs(edge_weights)) not_as_expected<-c(which(weakly_balanced\)unresolved>0),which(abs(edge_weights)==2)) conclusions<-“Overall, the analysis shows that there are a variety of people following and followed by Greta Thunberg on Twitter. The majority of Greta’s followers and those she follows have an interest in environmental activism. There were no obvious signs of clustering or patterns in the network structure. The graph is not strongly balanced, but there are only a few relationships that are unresolved and do not match what we would expect to see. Homophily tests suggest that there may be some tendency for like-minded individuals to connect on Twitter based on their support or opposition to Greta’s activism.”

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=787

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?