-
Notifications
You must be signed in to change notification settings - Fork 0
/
combining_all.R
317 lines (205 loc) · 10.2 KB
/
combining_all.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
library(stringr)
library(tidyverse)
library(readxl)
############ MAKING THE FINAL DATASET ###########
final_poi_all_df = data.frame()
big_df_file_list <- list.files(path = "D:/UNI STUFF/Stat/4th year/Research/Flickr R/Clusteing Results_R/All_big_dfs", pattern = "*.csv")
for (csv in big_df_file_list) {
## reading one district csv
big_df_district = read.csv(sprintf("D:/UNI STUFF/Stat/4th year/Research/Flickr R/Clusteing Results_R/All_big_dfs/%s",csv))
## reading the corresponding POI xls
districts_name = str_to_title(tolower(unique(big_df_district$DISTRICT_N)))
df_poi = read_excel(sprintf("D:/UNI STUFF/Stat/4th year/Research/Flickr R/Clusteing Results_R/All_POI/POI_%s.xlsx", districts_name))
## chanigng the column names in order to merge properly
columns_poi = c("Clusters_0.0005_5", colnames(df_poi)[-1])
colnames(df_poi) = columns_poi
## merging with POI df
df_merged = merge(big_df_district, df_poi, by = "Clusters_0.0005_5")
cleaned_merged = subset(df_merged, df_merged$`Can Take` == 1)
cleaned_district_df = subset(cleaned_merged, select = c("id", "owner", "ownername", "date", "time", "DISTRICT_N", "POI",
"latitude_poi", "longitude_poi"))
final_poi_all_df = rbind(final_poi_all_df, cleaned_district_df)
#break
}
length(unique(final_poi_all_df$ownername))
############### FILTER FOR INBOUND TOURISTS ###############
user_cat_df = read.csv("D:/UNI STUFF/Stat/4th year/Research/Flickr R/just_takin_last_first.csv")
final_poi_all_df = merge(final_poi_all_df, user_cat_df, by = "ownername")
length(unique(final_poi_all_df$POI))
########### MAKING THE WHOLE COUNTRY FLOW ###########
final_poi_all_df$date = as.Date(final_poi_all_df$date)
## removing records with same owner, same date at same POI
final_poi_all_df <- final_poi_all_df[!duplicated(final_poi_all_df [, c("owner", "date", "POI")]), ]
## sorting by user, date and time
final_poi_all_df <- final_poi_all_df[order(final_poi_all_df$ownername, final_poi_all_df$date, final_poi_all_df$time), ]
length(unique(final_poi_all_df$POI))
## 208
## filtering for inbound tourists
final_poi_all_df = subset(final_poi_all_df, user_type == "Inbound")
length(unique(final_poi_all_df$ownername))
## 975 to 963 inbound tourists after removing the repeated POIs
###### district to district flow ######
final_poi_all_df_adjusted = data.frame()
##### Adding Airport for once who do not have airport as the first POI #############
for (user in unique(final_poi_all_df$ownername)) {
user_df = subset(final_poi_all_df, ownername == user)
if(user_df[1,]$POI != "Bandaranayaka International Airport"){
add_df = user_df[1,]
add_df['DISTRICT_N'] = "GAMPAHA"
add_df['POI'] = "Bandaranayaka International Airport"
add_df['date'] = add_df['date'] - 1
add_df['latitude_poi'] = 7.1753
add_df['longitude_poi'] = 79.8835
user_df = rbind(add_df, user_df)
}
final_poi_all_df_adjusted = rbind(final_poi_all_df_adjusted, user_df)
}
user_dfs = data.frame()
for (i in users) {
## extracting user df
user_df = subset(final_poi_all_df_adjusted , final_poi_all_df_adjusted$ownername == i)
## 1st argument - more than one district visit
if(length(unique(user_df$DISTRICT_N)) > 1){
district_vec = user_df$DISTRICT_N
user_df["FROM"] = c(NA, district_vec[-length(district_vec)])
user_df["TO"] = district_vec
user_df = user_df[c("ownername", "FROM", "TO")]
user_dfs = rbind(user_dfs, user_df)
}else{
abc = c(abc,i)
FROM = rep("GAMPAHA", nrow(user_df))
TO = rep(NA, nrow(user_df))
user_df = data.frame(ownername = user_df$ownername, FROM, TO)
user_dfs = rbind(user_dfs, user_df)
}
}
final_district_all_df_na_drop = drop_na(user_dfs)
final_district_all_df_na_drop <- final_district_all_df_na_drop %>% filter(FROM!= TO)
length(unique(final_district_all_df_na_drop$ownername))
#clean_kandy_na_drop["combined"] = paste(clean_kandy_na_drop$FROM,clean_kandy_na_drop$TO,sep=",")
#length(unique(clean_kandy_na_drop$combined))
df_count <- final_district_all_df_na_drop %>% group_by(FROM, TO) %>% summarise(count = n_distinct(ownername))
df_sum <- final_district_all_df_na_drop %>% group_by(FROM) %>% summarise(sum = n_distinct(ownername))
df_count = merge(df_count, df_sum, by = "FROM", all.x = TRUE)
df_count["prob"] = df_count$count/df_count$sum
df_count = df_count %>% filter(sum > 5)
df_prob = df_count %>% select(c("FROM", "TO", "prob"))
#df_prob = df_count
## the reason why some FROMs have one is because the rest is classfied s local tourists
write.csv(df_count, "District_to_district_flow.csv")
## TAKING ATLEAST 5 USERS DISTRICTS
##Rounding off to 4 decimal places
df_count$prob = round(df_count$prob, digits = 4)
write.csv(df_count, "1district_todistrict.csv")
### district to didstrict matrix
matrix <- xtabs(prob ~ FROM + TO, df_count)
# Convert the matrix to a data frame
df <- as.data.frame(matrix)
# Convert the "from" and "to" columns to character or factor
df$FROM <- as.character(df$FROM)
df$TO <- as.character(df$TO)
# Convert the data frame to long format
df_long <- pivot_longer(df, cols = -c(FROM, TO), names_to = "count", values_to = "prob")
df_long <- df_long[order(df_long$TO), ]
df_long = df_long[order(df_long$FROM), ]
# Plot the heatmap using ggplot2
library(ggplot2)
ggplot(df_long, aes(x = TO, y = FROM, fill = prob)) +
geom_tile() +
scale_fill_gradient(high = "#003477", low = "#9AC0F2") +
geom_text(aes(label = round(prob, 4)),color = "black", size = 3) +
labs(x = "TO", y = "FROM") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
axis.text.y = element_text(angle = 0, hjust = 0))
###### Finding POI to POI whole country ######
## for each user if the minimum date is not "Bandaranayaka International Airport" add it for the minimum date
final_poi_all_df_adjusted = data.frame()
for (user in unique(final_poi_all_df$ownername)) {
user_df = subset(final_poi_all_df, ownername == user)
if(user_df[1,]$POI != "Bandaranayaka International Airport"){
add_df = user_df[1,]
add_df['DISTRICT_N'] = "GAMPAHA"
add_df['POI'] = "Bandaranayaka International Airport"
add_df['date'] = add_df['date'] - 1
add_df['latitude_poi'] = 7.1753
add_df['longitude_poi'] = 79.8835
user_df = rbind(add_df, user_df)
}
final_poi_all_df_adjusted = rbind(final_poi_all_df_adjusted, user_df)
}
## finding number of people visited from one attraction to another
users = unique(final_poi_all_df_adjusted$ownername)
user_dfs = data.frame()
for (i in users) {
## extracting user df
user_df = subset(final_poi_all_df_adjusted, final_poi_all_df_adjusted$ownername == i)
## 1st argument - more than one place visit
if(length(unique(user_df$POI)) > 1){
poi_vec = user_df$POI
user_df["FROM"] = c("Bandaranayaka International Airport", poi_vec[-length(poi_vec)])
user_df["TO"] = poi_vec
user_df = user_df[c("ownername", "FROM", "TO", "DISTRICT_N", "latitude_poi", "longitude_poi")]
user_dfs = rbind(user_dfs, user_df)
}else{
FROM = rep("Bandaranayaka International Airport", nrow(user_df))
TO = rep(NA, nrow(user_df))
user_df = data.frame(ownername = user_df$ownername, FROM, TO)
user_dfs = rbind(user_dfs, user_df)
}
}
final_poi_all_df_adjusted["FROM"] = user_dfs["FROM"]
final_poi_all_df_adjusted["TO"] = user_dfs["TO"]
final_poi_all_df_na_drop = drop_na(final_poi_all_df_adjusted)
final_poi_all_df_na_drop = final_poi_all_df_na_drop %>% filter(FROM != TO)
df_count <- final_poi_all_df_na_drop %>% group_by(FROM, TO) %>% summarise(count = n_distinct(ownername))
df_sum <- df_count %>% group_by(FROM) %>% summarise(sum = sum(count))
df_count = merge(df_count, df_sum, by = "FROM")
df_count["prob"] = df_count$count/df_count$sum
df_count = df_count %>% filter(sum>10)
df_count$prob = round(df_count$prob, digits = 4)
user_dfs_matale = user_dfs %>% filter(DISTRICT_N == "MATALE")
################ Adding Airport for once who do n`ot have airport as the first POI #############
## for each user if the minimum date is not "Bandaranayaka International Airport" add it for the minimum date
final_poi_all_df_adjusted = data.frame()
for (user in unique(final_poi_all_df$ownername)) {
user_df = subset(final_poi_all_df, ownername == user)
if(user_df[1,]$POI != "Bandaranayaka International Airport"){
add_df = user_df[1,]
add_df['DISTRICT_N'] = "GAMPAHA"
add_df['POI'] = "Bandaranayaka International Airport"
add_df['date'] = add_df['date'] - 1
add_df['latitude_poi'] = 7.1753
add_df['longitude_poi'] = 79.8835
user_df = rbind(add_df, user_df)
}
final_poi_all_df_adjusted = rbind(final_poi_all_df_adjusted, user_df)
}
final_poi_all_df_adjusted = subset(final_poi_all_df_adjusted, DISTRICT_N == "KANDY")
## finding number of people visited from one attraction to another
users = unique(final_poi_all_df_adjusted$ownername)
user_dfs = data.frame()
for (i in users) {
## extracting user df
user_df = subset(final_poi_all_df_adjusted, final_poi_all_df_adjusted$ownername == i)
## 1st argument - more than one place visit
if(length(unique(user_df$POI)) > 1){
poi_vec = user_df$POI
user_df["FROM"] = c(NA, poi_vec[-length(poi_vec)])
user_df["TO"] = poi_vec
user_df = user_df[c("ownername", "FROM", "TO")]
user_dfs = rbind(user_dfs, user_df)
}else{
FROM = rep(NA, nrow(user_df))
TO = rep(NA, nrow(user_df))
user_df = data.frame(ownername = user_df$ownername, FROM, TO)
user_dfs = rbind(user_dfs, user_df)
}
}
final_poi_all_df_adjusted["FROM"] = user_dfs["FROM"]
final_poi_all_df_adjusted["TO"] = user_dfs["TO"]
final_poi_all_df_na_drop = drop_na(final_poi_all_df_adjusted)
final_poi_all_df_na_drop = final_poi_all_df_na_drop %>% filter(FROM != TO)
df_count <- final_poi_all_df_na_drop %>% group_by(FROM, TO) %>% summarise(count = n_distinct(ownername))
df_sum <- df_count %>% group_by(FROM) %>% summarise(sum = sum(count))
df_count = merge(df_count, df_sum, by = "FROM")
df_count["prob"] = df_count$count/df_count$sum