-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.R
237 lines (158 loc) · 7.7 KB
/
analysis.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
library(pacman)
p_load("tidyr",
"ggplot2",
"dplyr",
"lubridate",
"openxlsx")
### Sample topic proportion visualisation ##########
# Generate random sample from topic proportion matrix (theta)
random_row_indices <- sample(nrow(slda1$theta), 5)
topicProportionExamples <- slda1$theta[random_row_indices, ]
# Transform matrix to data frame for visualization
topicProportionExamples <- as.data.frame(topicProportionExamples)
# Add a column for document ids
topicProportionExamples$Document <- rownames(topicProportionExamples)
# Reshape the data using pivot_longer
topicProportionExamples <- pivot_longer(topicProportionExamples, cols = -Document, names_to = "Topic", values_to = "Proportion")
# Create the ggplot visualization
example_plot <- ggplot(data = topicProportionExamples, aes(x = Topic,
y = Proportion,
fill = Document)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
coord_flip() +
facet_wrap(~ Document, ncol = 5)
example_plot
### Topic proportions by year ####
topic_proportions <- as.data.frame(slda1$theta)
#topic_proportions$ProjectReference <- rownames(topic_proportions)
vars <- docvars %>% select(ProjectReference, StartDate, AwardPounds) %>% mutate(Year=year(StartDate),.keep="unused")
# get mean topic proportions per year
topic_proportion_per_year <- aggregate(topic_proportions,
by = list(Year = vars$Year),
mean
)
# create palette for visualisation
custom_palette <- c(
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
"#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf",
"#aec7e8", "#ffbb78", "#98df8a", "#ff9896", "#c5b0d5",
"#c49c94", "#f7b6d2", "#c7c7c7", "#dbdb8d", "#9edae5",
"#393b79", "#e6550d", "#637939", "#d6616b", "#843c39",
"#d8b365", "#7b4173", "#a55194", "#8ca252", "#b5cf6b",
"#6b6ecf", "#8c564b"
)
# transform df to longer for visualisation
topic_proportion_per_year <- pivot_longer(topic_proportion_per_year,
cols = -Year,
names_to = "Topic",
values_to = "Proportion")
# Plot topic proportions by year
topic_year_plot <- ggplot(topic_proportion_per_year, aes(x = Year,
y = Proportion,
fill = Topic)) +
geom_bar(stat = "identity") + ylab("Proportion") +
scale_fill_manual(values = custom_palette, name = "Topic") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
ggtitle("Topic Proportions by Year")
topic_year_plot
### Topic proportions by year weighted by AwardPounds ####
weighted_topic_proportions <- sweep(topic_proportions, MARGIN = 1, STATS = vars$AwardPounds, FUN = "*")
weighted_topic_proportions_per_year <- aggregate(weighted_topic_proportions,
by = list(Year = vars$Year),
mean
)
# Normalize values by dividing by the row sums
weighted_topic_proportions_per_year[, -1] <-
weighted_topic_proportions_per_year[, -1] /
rowSums(weighted_topic_proportions_per_year[, -1])
weighted_topic_proportions_per_year <-
pivot_longer(weighted_topic_proportions_per_year,
cols = -Year,
names_to = "Topic",
values_to = "Proportion")
topic_proportion_per_year$WeightedProportion <- weighted_topic_proportions_per_year$Proportion
# Plot weighted topic proportions with custom colors
weighted_topic_year_plot <-
ggplot(topic_proportion_per_year, aes(x = Year,
y = WeightedProportion,
fill = Topic)) +
geom_bar(stat = "identity") + ylab("Proportion") +
scale_fill_manual(values = custom_palette, name = "Topic") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
ggtitle("Topic Proportions by Year Weighted by the Value of the Award")
weighted_topic_year_plot
rm("example_plot",
"slda2",
"slda2_topics",
"topicProportionExamples",
"vars",
"json2",
"random_row_indices",
"weighted_topic_proportions_per_year")
### Descriptive stats of docvars #####
docvars$NumMonths <- as.period(interval(docvars$StartDate,
docvars$EndDate + 1)) %/% months(1)
boxplot(docvars$NumMonths)
summary(docvars$NumMonths)
docvars$StartYear <- year(docvars$StartDate)
CPI <- read.csv("ahrc_applications/CPI_index.csv")
docvars <- inner_join(docvars, CPI, by=c("StartYear" = "Year"))
docvars <- docvars%>% mutate(AwardPoundsAdjusted=AwardPounds*CPI.INDEX/131.3) %>%
select(-CPI.INDEX)
range(docvars$AwardPounds)
# Calculate quantiles to divide the data into equal parts
quantiles <- quantile(docvars$AwardPounds, probs = seq(0, 1, length.out = 8 + 1))
# Use cut() to create quantile-based bands
bands <- cut(docvars$AwardPounds, breaks = quantiles, include.lowest = TRUE)
# Round the values to the nearest 50000
rounded_values <- round(docvars$AwardPoundsAdjusted / 50000) * 50000
# Create a frequency table
frequency_table <- table(rounded_values)
# Convert the frequency table to a data frame
frequency_df <- as.data.frame(frequency_table)
colnames(frequency_df) <- c("AwardPoundsAdjusted", "Frequency")
# Define your custom bands
custom_bands <- c(0, 25000, 50000, 100000, 200000, 350000, 550000, 750000, 1000000, Inf)
band_labels <- c("0-25", "26-50", "51-100", "101-200", "201-350", "351-550", "551-750", "751-1,000", "1,001+")
docvars$AwardPoundsBand <- cut(round(docvars$AwardPoundsAdjusted, digits = -3), breaks = custom_bands, labels = band_labels, include.lowest = TRUE)
print(table(docvars$AwardPoundsBand))
# get mean topic proportions per award band
topic_proportion_per_band <- aggregate(topic_proportions,
by = list(AwardPoundsBand = docvars$AwardPoundsBand),
mean
)
# transform df to longer for visualisation
topic_proportion_per_band <- pivot_longer(topic_proportion_per_band,
cols = -AwardPoundsBand,
names_to = "Topic",
values_to = "Proportion")
# Plot topic proportions by award band
topic_band_plot <- ggplot(topic_proportion_per_band, aes(x = AwardPoundsBand,
y = Proportion,
fill = Topic)) +
geom_bar(stat = "identity") + ylab("Proportion") + xlab("Value of Award (thousand pounds)") +
scale_fill_manual(values = custom_palette, name = "Topic") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
ggtitle("Topic Proportions by Value of the Award (Band)")
topic_band_plot
# Create master dataframe for visualisation
master_df <- read.xlsx("gtr_scraper/master_data.xlsx") %>%
mutate(PI = paste(PIFirstName, PISurname, sep = " "), .keep = "unused") %>%
mutate(StartDate = as.Date(StartDate, origin = "1899-12-30"),
EndDate = as.Date(EndDate, origin = "1899-12-30"),
Region = as.factor(Region))%>%
mutate(AwardPounds=format(AwardPounds, big.mark = ","))
save(master_df,
slda1,
topic_proportion_per_band,
topic_proportion_per_year,
topic_proportions,
custom_palette,
json1,
file = "outputs.RData")
filtered_abstarcts <- topic_proportions_long %>%
filter(Topic=="gender_sexuality") %>%
arrange(desc(Proportion)) %>%
inner_join(master_df, by = "ProjectReference")%>%
filter(Proportion >= 0.1)