Advertisement
techno-

Networks code

May 20th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. # Cargar librerías necesarias
  2. library(dplyr)
  3. library(syuzhet)
  4. library(igraph)
  5. library(tidyr)
  6. library(ggplot2)
  7.  
  8. # Cargar los datos
  9. data <- read.csv(file.choose(), header = TRUE)
  10.  
  11. # Convertir created_at a datetime
  12. data$created_at <- as.POSIXct(data$created_at, format = "%a %b %d %H:%:%S %z %Y")
  13.  
  14. # Análisis de sentimiento en los tweets
  15. sentiment_scores <- get_sentiment(data$text, method = "afinn")
  16.  
  17. # Añadir los resultados de sentimiento a los datos originales
  18. data_with_sentiments <- cbind(data, sentiment_scores)
  19.  
  20. # Clasificar sentimiento en categorías: positivo, negativo, neutral
  21. data_with_sentiments$sentiment_category <- ifelse(data_with_sentiments$sentiment > 0, "Positive",
  22. ifelse(data_with_sentiments$sentiment < 0, "Negative", "Neutral"))
  23.  
  24. # Extraer autores y sus sentimientos predominantes
  25. author_sentiments <- data_with_sentiments %>%
  26. group_by(author_id, sentiment_category) %>%
  27. summarise(count = n()) %>%
  28. pivot_wider(names_from = sentiment_category, values_from = count, values_fill = list(count = 0))
  29.  
  30. # Asegurarse de que la matriz es cuadrada
  31. author_matrix <- as.matrix(author_sentiments[,-1])
  32. rownames(author_matrix) <- author_sentiments$author_id
  33.  
  34. # Calcular la similitud entre autores (por ejemplo, usando el coeficiente de Jaccard)
  35. similarity_matrix <- 1 - as.matrix(dist(author_matrix, method = "binary"))
  36.  
  37. # Crear la red
  38. g <- graph_from_adjacency_matrix(similarity_matrix, mode = "undirected", weighted = TRUE, diag = FALSE)
  39.  
  40. # Simplificar la red eliminando enlaces débiles
  41. g <- simplify(g, remove.multiple = TRUE, remove.loops = TRUE)
  42. E(g)$weight <- 1/E(g)$weight # Invertir para que un valor más alto signifique mayor similitud
  43.  
  44. # Visualizar la red
  45. plot(g, vertex.label = NA, vertex.size = 5, edge.width = E(g)$weight)
  46.  
  47. # Analizar la red
  48. degree_distribution <- degree(g)
  49. centrality <- closeness(g)
  50. communities <- cluster_louvain(g)
  51.  
  52. print(degree_distribution)
  53. print(centrality)
  54. print(communities)
  55.  
  56. # Visualizar comunidades
  57. plot(communities, g, vertex.label = NA, vertex.size = 5)
  58.  
  59. # Añadir análisis de comunidades a los datos originales
  60. data_with_sentiments$author_community <- communities$membership[match(data_with_sentiments$author_id, V(g)$name)]
  61.  
  62. # Analizar tiempo de respuesta por comunidad
  63. reply_time_summary <- data_with_sentiments %>%
  64. filter(!is.na(in_response_to_tweet_id)) %>%
  65. group_by(author_community, sentiment_category) %>%
  66. summarise(avg_reply_time = mean(reply_time, na.rm = TRUE))
  67.  
  68. # Visualizar el tiempo de respuesta por comunidad y categoría de sentimiento
  69. ggplot(reply_time_summary, aes(x = sentiment_category, y = avg_reply_time, fill = as.factor(author_community))) +
  70. geom_bar(stat = "identity", position = "dodge") +
  71. labs(title = "Average Reply Time by Sentiment Category and Author Community",
  72. x = "Sentiment Category",
  73. y = "Average Reply Time (minutes)") +
  74. theme_minimal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement