-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample.R
62 lines (57 loc) · 1.95 KB
/
Example.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
# Here's an example that shows how to model a text input as a bipartite network
library(quanteda)
library(quanteda.textstats)
load("Comments.RData")
my_corpus <- corpus(UserComments$text)
mycorpus <- data.frame(summary(my_corpus, n = nrow(UserComments)))
summary(my_corpus)
spanishstopwords <- c("q", stopwords("spanish"))
Restaurant <- dfm(corpus(
my_corpus),
remove_numbers = TRUE,
remove = spanishstopwords,
stem = TRUE, remove_punct = TRUE)
restaurant <- textstat_simil(Restaurant,
margin = "documents",
method = "jaccard")
restaurantdf <- data.frame(as.matrix(restaurant))
restaurantdf[is.na(restaurantdf)] = 0
restaurant <- data.frame(jaccard = restaurant[lower.tri(restaurant, diag = FALSE)])
#library(clustertend)
#library(hopkins)
#set.seed(123)
#hopkins(restaurantdf, m = nrow(restaurantdf)/10)
# Hopkins = 0.06922988
library(mclust)
fit <- Mclust(restaurantdf)
summary(fit)
clasificados <- data.frame(fit$classification)
names(clasificados)[1] <- "classification"
clasificados$Category <- "restaurant"
library(topicmodels)
restaurantC <- convert(Restaurant, to = "topicmodels", docvars = NULL)
pave <- LDA(restaurantC, k = 9)
library(tidytext)
restaurant_topics <- tidy(pave, matrix = "beta")
restaurant_topics$Category <- "restaurant"
library(broom)
EssentialWordsrestaurant <- tidy(pave)
library(dplyr)
topics <- EssentialWordsrestaurant %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)
library(igraph)
g_rest <- graph.data.frame(topics, directed = FALSE)
bipartite.mapping(g_rest)
V(g_rest)$type <- bipartite_mapping(g_rest)$type
V(g_rest)$color <- ifelse(V(g_rest)$type, "lightblue", "red")
V(g_rest)$shape <- ifelse(V(g_rest)$type, "circle", "square")
E(g_rest)$color <- "black"
V(g_rest)$size <- eccentricity(g_rest) * 2.5
V(g_rest)$label.cex <- degree(g_rest) * 2.5
plot(g_rest,
vertex.label.cex = 0.8,
vertex.label.color = "black",
layout = layout_with_dh)