-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_clusters.R
187 lines (118 loc) · 4.06 KB
/
random_clusters.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
#-------------- SKATER RANDOM ---------------------
# Reference: https://geodacenter.github.io/rgeoda/articles/rgeoda_tutorial.html#spatial-clustering
set.seed(12345)
memory.size()
memory.limit()
ls()
rm(list = ls())
gc()
options(digits=7, scipen=999)
library(sp)
library(spdep)
library(here)
library(tidyverse)
library(rgeoda)
library(raster)
setwd(here())
setwd('results')
dfg <- read.csv('gstar.csv')
dfg$ID <- NULL
# Subset (if slow) dfsub <- dplyr::sample_n(dfg, 10000) # want to increase!
# all data
dfsub <- dfg
dfsub <- dfsub %>% dplyr::select( c('x','y', !contains("95")) )
colnames(dfsub)
# cattle-only
tofocus <- colnames(dfsub %>% dplyr::select(!c('x','y',
'hosts_muylaert',
'hosts_sanchez',
"mammals_iucn_mode",
"bovliv",
'trans',
'pollution',
'motor_travel_time_weiss' )) )
#------------------------------
# Get projected coords, generate weights matrix ---
mercator <- raster::shapefile('mercator.shp')
queen_w <- queen_weights(st_as_sf(mercator))
head(dfsub[,tofocus])
# Creating random data
#hist(dbeta(runif(nrow(rd)), shape1 = 5, shape2 = 20) )
#dir.create('skater_random')
setwd('skater_random')
rd <- data.frame(replicate(10,rnorm(nrow(dfsub), 0, 1)))
colnames(rd) <- colnames(dfsub[,tofocus])
write.csv(rd, row.names = FALSE, file= 'random_data.csv')
rd <- read.csv('random_data.csv')
#head(rd)
# iterations
its <- c(40:1)
#for(i in its){
# print(i)
# clusg <- rgeoda::skater(i, queen_w, rd,
scale_method = "raw",
distance_method = "euclidean" )
# save(clusg, file = paste0(i,'_clusters_random.RData') )
#}
ratio <- c()
twss <-c()
loads <- c(1:40)
for(lo in loads){
print(lo)
load(paste0(lo,'_clusters_random.RData') )
twss <- c(twss, clusg$`Total within-cluster sum of squares`)
ratio <- c(ratio, clusg$`The ratio of between to total sum of squares` )
}
ratio
twss
# Elbow plots
#
png(filename= 'elbow_plot_random.png', width = 20, height = 14, unit='cm',
res=300)
plot(twss ~ loads, pch = 19, xlab= 'Number of clusters (random)',
ylab='Total within-cluster sum of squares' )
dev.off()
png(filename= 'elbow_plot_ratio_random.png', width = 20, height = 14, unit='cm',
res=300)
plot(ratio ~ loads, pch = 19, xlab= 'Number of clusters',
ylab= 'Ratio of between/total sum of squares')
dev.off()
# Export ss data
filenametwss <- paste0('clusters_twss_ratio_random', ".csv")
write.csv(data.frame(its, twss, ratio), file = filenametwss, row.names = FALSE)
# After inspecting
# assign optimal k clusters to spatial object (careful, col names reduced when exported)
otimo <- 19
result <- dfsub
coordinates(result)<-~x+y
raster::crs(result) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
setwd(here())
setwd('results/skater_random')
load(paste0(otimo,'_clusters_random.RData') )
table(clusg$Clusters)
result$cluster <- clusg$Clusters
colnames(result@data)
class(result)
#------------------------------
# Export shapefile of points and csv
getwd()
dir.create('skater_optimal_cluster_size_19_random')
setwd('skater_optimal_cluster_size_19_random')
colwant <- c(tofocus, 'cluster')
colnames(result[colwant]@data)
filenamesh <- paste0('clusters_rgeoda_c19_random', ".shp")
raster::shapefile(result[colwant], filenamesh, overwrite=TRUE)
hist(table(result[colwant]@data$cluster))
resultdf <- result@data
resultdf$x <- dfsub$x
resultdf$y <- dfsub$y
filenamec <- paste0('clusters_rgeoda_c19_random', ".csv")
write.csv(resultdf, file = filenamec, row.names = FALSE)
#--------------------------------------------------------------------------------
# # #
# # #
# # #
# ######
# ####
# ##
#-------------------------------------------------------------------------------