-
Notifications
You must be signed in to change notification settings - Fork 3
/
DH2018-Workflow.R
149 lines (111 loc) · 4.88 KB
/
DH2018-Workflow.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
library(plotly)
library(viridis)
library(RColorBrewer)
colors <- brewer.pal(n = 4, name = "Spectral")
l <- list(
font = list(
family = "sans-serif",
size = 24,
color = "#000"),
bgcolor = "#fff",
size = 44,
bordercolor = "#efefef",
borderwidth = 2)
# Use ClusterAnalysis-New.R for reading in clusters and hand-tagged genres.
#PCA Code for TOPICS (This assumes topic modelling has been completed in Topic Modelling Generic.r)
#UNCLASSIFIED TEXTS FIRST
#Create a dataframe with all topics for clusters that have known genres
unClassifiedTopicsDF = doc.topics %>%
as.data.frame() %>%
mutate(cluster = input$Cluster, primary_genre = input$Genre)
#Convert to a matrix
modeling_matrix = unClassifiedTopicsDF %>% select(-primary_genre, -cluster)
modeling_matrix = log(modeling_matrix + .5)
#PCA
model = prcomp(modeling_matrix, scale = TRUE)
prediction = predict(model)
prediction = prediction %>%
as.data.frame %>%
mutate(cluster=unClassifiedTopicsDF$cluster %>%
as.character, genre=unClassifiedTopicsDF$primary_genre %>%
as.character) %>%
select(cluster,genre,PC1,PC2,PC3)
#vtData$cluster <- vtData$cluster %>% as.character()
prediction <- prediction %>% inner_join(allData, by = "cluster" )
prediction <- prediction[,c("cluster","text","genre.x","PC1","PC2","PC3")]
prediction$genre <- prediction$genre.x
prediction$genre.x <- NULL
#replace poetry/prose
# prediction <- prediction %>% mutate_if(is.character, str_replace_all, pattern = 'poetry', replacement = 'literary')
# prediction <- prediction %>% mutate_if(is.character, str_replace_all, pattern = 'prose', replacement = 'informational')
# prediction <- prediction %>% mutate_if(is.character, str_replace_all, pattern = 'advertisement', replacement = 'ads')
#Visualize with plotly
unClassPCAViz <- plot_ly(prediction, x = ~PC1, y = ~PC2, z = ~PC3, color = ~genre, symbol = ~genre, symbols = c("cross","circle","square","diamond","circle-open"), opacity = 1, colors = colors, marker = list(size = 6)) %>%
add_markers() %>%
layout(legend = l, title ="Principal Component Analysis of Viral Texts Clusters", scene = list(xaxis = list(title = 'PC1'),
yaxis = list(title = 'PC2'),
zaxis = list(title = 'PC3')))
unClassPCAViz
# Export to large SVG
library(htmlwidgets)
# Save viewer settings (e.g. RStudio viewer pane)
op <- options()
# Set viewer to web browser
options(viewer = NULL)
# Use web browser to save image
unClassPCAViz %>% htmlwidgets::onRender(
"function(el, x) {
var gd = document.getElementById(el.id);
Plotly.downloadImage(gd, {format: 'svg', width: 2400, height: 1600, filename: 'pca_noads'});
}"
)
# Restore viewer to old setting (e.g. RStudio)
options(viewer = op$viewer)
#CLASSIFIED TEXTS FIRST
#Topic model genreClass first!
#Create a dataframe with all topics for clusters that have known genres
classifiedTopicsDF = doc.topics %>%
as.data.frame() %>%
mutate(cluster = input$Cluster, primary_genre = input$Genre)
#Convert to a matrix
modeling_matrix = classifiedTopicsDF %>% select(-primary_genre, -cluster)
modeling_matrix = log(modeling_matrix + .5)
#PCA
model = prcomp(modeling_matrix, scale = TRUE)
prediction = predict(model)
prediction = prediction %>%
as.data.frame %>%
mutate(cluster=classifiedTopicsDF$cluster %>%
as.character, genre=classifiedTopicsDF$primary_genre %>%
as.character) %>%
select(cluster,genre,PC1,PC2,PC3)
#vtData$cluster <- vtData$cluster %>% as.character()
prediction <- prediction %>% inner_join(allData, by = "cluster" )
prediction <- prediction[,c("cluster","text","genre.x","PC1","PC2","PC3")]
prediction$genre <- prediction$genre.x
prediction$genre.x <- NULL
#Remove ads
#prediction <- prediction %>% filter(genre != "ads")
#Visualize with plotly
classPCAViz <- plot_ly(prediction, x = ~PC1, y = ~PC2, z = ~PC3, color = ~genre, symbol = ~genre, symbols = c("cross","circle","square","diamond"), opacity = 1, text = ~text, colors = colors, marker = list(size = 6)) %>%
add_markers() %>%
layout(legend = l, title ="Principal Component Analysis of Viral Texts Clusters", scene = list(xaxis = list(title = 'PC1'),
yaxis = list(title = 'PC2'),
zaxis = list(title = 'PC3')))
classPCAViz
options(viewer=NULL)
# Export to large SVG
library(htmlwidgets)
# Save viewer settings (e.g. RStudio viewer pane)
op <- options()
# Set viewer to web browser
options(viewer = NULL)
# Use web browser to save image
classPCAViz %>% htmlwidgets::onRender(
"function(el, x) {
var gd = document.getElementById(el.id);
Plotly.downloadImage(gd, {format: 'svg', width: 2400, height: 1600, filename: 'pca_noads'});
}"
)
# Restore viewer to old setting (e.g. RStudio)
options(viewer = op$viewer)