-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.m
More file actions
278 lines (237 loc) · 9.68 KB
/
Copy pathCode.m
File metadata and controls
278 lines (237 loc) · 9.68 KB
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
load('COVIDbyCounty.mat')
figure
subplot(2,1,1)
plot(CNTY_COVID(1,:))
subplot(2,1,2)
plot(CNTY_COVID(15,:))
all_diffs = zeros(225,130); %change to 225 (225 are the counties)
place = 1;
OneThirty_place = 1;
second_place = 1;
i = 1;
for b = CNTY_COVID(place,:)
OneThirty_place = 1;
for a = CNTY_COVID(place,:)
if OneThirty_place <= 130
second_place = place + 1;
values_sub1 = CNTY_COVID(place,OneThirty_place);
values_sub2 = CNTY_COVID(second_place,OneThirty_place);
diff = (values_sub1 - values_sub2);
all_diffs(place,:) = diff;
place = place + 1;
OneThirty_place = OneThirty_place + 1;
end
end
end
temp = CNTY_COVID.';
temp1 = corr(temp);
plot(temp1);
max1 = max(temp1);
subplot(1,2,1)
plot(temp1(1,:));
subplot(1,2,2)
plot(temp1(10,:));
temp3 = kmeans(CNTY_COVID,9);
sil1 = silhouette(CNTY_COVID, temp3);
figure
[sil1, H] = silhouette(CNTY_COVID, temp3);
%the counties most similar to each other fall under the same division. k
%means should cluster them according to the right divisions. Up to us to
%make sure they match
%kmeans(all_diffs,9)
-------------------------------------
load('COVIDbyCounty.mat')
temp = CNTY_COVID.';
temp1 = corr(temp);
plot(temp1);
max1 = max(temp1);
subplot(1,2,1)
plot(temp1(1,:));
subplot(1,2,2)
plot(temp1(10,:));
CNTY_COVID_TRAIN=[CNTY_COVID(1:215,:)]; %segment the data into train / test
CNTY_COVID_TEST=[CNTY_COVID(216:225,:)]; %segment the data into train / text
SUM_CNTY_COVID_TRAIN=sum(CNTY_COVID_TRAIN.'); %for each county, across its timestamps, add the incremental # of cases for the case total
figure
pareto(SUM_CNTY_COVID_TRAIN,1);
temp3 = kmeans(CNTY_COVID_TRAIN,9);
sil1 = silhouette(CNTY_COVID_TRAIN, temp3);
figure
[sil1, H] = silhouette(CNTY_COVID_TRAIN, temp3);
count=1;
for i=2:100
[junk1,junk2,sumd] = kmeans(CNTY_COVID_TRAIN,i);
tempk(:,count)=junk1;
silk(:,count)=silhouette(CNTY_COVID_TRAIN,tempk(:,count));
%figure
%[silk(:,count), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,count));
Sum_of_SquDist(count)=sum(sumd);
count=count+1;
end
figure
plot(Sum_of_SquDist);
------------------------------------------------
load('COVIDbyCounty.mat') %load the data
temp = CNTY_COVID.'; %transpose the COVID series so the columns = counties, rows = timestamps
temp1 = corr(temp); %run correlation that looks at each column = county and compares them
plot(temp1); %plot shows there is real correlation, which makes clustering worthwhile
max1 = max(temp1);
subplot(1,2,1) % look at the correlation trends for different counties
plot(temp1(1,:));
subplot(1,2,2)
plot(temp1(10,:));
%these data are worth clustering
CNTY_COVID_TRAIN=[CNTY_COVID(1:215,:)]; %segment the data into train / test
CNTY_COVID_TEST=[CNTY_COVID(216:225,:)]; %segment the data into train / text
SUM_CNTY_COVID_TRAIN=sum(CNTY_COVID_TRAIN.'); %for each county, across its timestamps, add the incremental # of cases for the case total
figure
pareto(SUM_CNTY_COVID_TRAIN,1);
temp3 = kmeans(CNTY_COVID_TRAIN,9); %try clustering with 9 clusters, based on having 9 divisions
sil1 = silhouette(CNTY_COVID_TRAIN, temp3); %compute quality of clustering w/ silhouette
figure
[sil1, H] = silhouette(CNTY_COVID_TRAIN, temp3); %plot the silhouette values, to see if there are negatives or low values (bad)
%clustering into 9 divisions yields poor silhouette values, we can and should do better
%let’s try clustering with different values of k from 2:100 and then plotting the Sum of Sq distances from the respective centroids, to see if there’s an ‘elbow’ that shows us the optimal # of clusters to have
count=1;
for i=2:100
[junk1,junk2,sumd] = kmeans(CNTY_COVID_TRAIN,i);
tempk(:,count)=junk1;
silk(:,count)=silhouette(CNTY_COVID_TRAIN,tempk(:,count));
%figure
%[silk(:,count), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,count));
Sum_of_SquDist(count)=sum(sumd);
count=count+1;
end
figure
plot(Sum_of_SquDist);
%looking at this plot, we see somewhat of an ‘elbow’ at k=6, so let’s set k=6
%now let’s get the centroids matrix C for k=6
[tempk(:,6),C,sumd]=kmeans(CNTY_COVID_TRAIN,6);
centroids = C;
%take the 10 test vectors from CNTY_COVID_TEST and figure out which of the six cluster centroids it’s closest to
%compute its Euclidean distance
D=pdist2(centroids,CNTY_COVID_TEST);
%return the index of the closest cluster distance for each of the 10 counties in I
[D,I]=pdist2(centroids,CNTY_COVID_TEST,'euclidean','smallest',1);
%I has one row with 10 entries, each tells the cluster of the test county
%figure out the meaning of these centroids – do they represent times of peak volumes, times of a certain magnitude, times when there was a spike in certain geographies?
%now let’s try cleaning the test data by grouping it across counties in a given division
% 9 rows x 130 columns = sum of all counties in each of those 9 divisions across time
SUM_DIVISION_COVID_TRAIN = zeros(9,130);
tempsum=zeros(1,130);
for j=1:9
for r=1:215
if CNTY_CENSUS{r,3} == j
tempsum=(CNTY_COVID_TRAIN(r,:));
SUM_DIVISION_COVID_TRAIN(j,:)=SUM_DIVISION_COVID_TRAIN(j,:) + tempsum;
%whatever it was before + tempsum
end;
end;
end;
plot(SUM_DIVISION_COVID_TRAIN(1,:));
hold on
plot(SUM_DIVISION_COVID_TRAIN(2,:));
plot(SUM_DIVISION_COVID_TRAIN(3,:));
plot(SUM_DIVISION_COVID_TRAIN(4,:));
plot(SUM_DIVISION_COVID_TRAIN(5,:));
plot(SUM_DIVISION_COVID_TRAIN(6,:));
plot(SUM_DIVISION_COVID_TRAIN(7,:));
plot(SUM_DIVISION_COVID_TRAIN(8,:));
plot(SUM_DIVISION_COVID_TRAIN(9,:));
------------------------------------------------------------
load('COVIDbyCounty.mat') %load the data
temp = CNTY_COVID.'; %transpose the COVID series so the columns = counties, rows = timestamps
temp1 = corr(temp); %run correlation that looks at each column = county and compares them
plot(temp1); %plot shows there is real correlation, which makes clustering worthwhile
max1 = max(temp1);
subplot(1,2,1) % look at the correlation trends for different counties
plot(temp1(1,:));
subplot(1,2,2)
plot(temp1(10,:));
%these data are worth clustering
CNTY_COVID_TRAIN=[CNTY_COVID(1:215,:)]; %segment the data into train / test
CNTY_COVID_TEST=[CNTY_COVID(216:225,:)]; %segment the data into train / text
SUM_CNTY_COVID_TRAIN=sum(CNTY_COVID_TRAIN.'); %for each county, across its timestamps, add the incremental # of cases for the case total
figure
pareto(SUM_CNTY_COVID_TRAIN,1);
temp3 = kmeans(CNTY_COVID_TRAIN,9); %try clustering with 9 clusters, based on having 9 divisions
sil1 = silhouette(CNTY_COVID_TRAIN, temp3); %compute quality of clustering w/ silhouette
figure
[sil1, H] = silhouette(CNTY_COVID_TRAIN, temp3); %plot the silhouette values, to see if there are negatives or low values (bad)
%clustering into 9 divisions yields poor silhouette values, we can and should do better
%let’s try clustering with different values of k from 2:100 and then plotting the Sum of Sq distances from the respective centroids, to see if there’s an ‘elbow’ that shows us the optimal # of clusters to have
count=1;
for i=2:100
[junk1,junk2,sumd] = kmeans(CNTY_COVID_TRAIN,i);
tempk(:,count)=junk1;
silk(:,count)=silhouette(CNTY_COVID_TRAIN,tempk(:,count));
%figure
Sum_of_SquDist(count)=sum(sumd);
count=count+1;
end
figure
plot(Sum_of_SquDist);
%looking at this plot, we see somewhat of an ‘elbow’ in the area from k=4 through k=9
%Let’s look at silhouette graphs for different k values to see what’s best
figure
[silk(:,9), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,9));
figure
[silk(:,8), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,8));
figure
[silk(:,7), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,7));
figure
[silk(:,6), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,6));
figure
[silk(:,5), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,5));
figure
[silk(:,4), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,4));
% k=4 gives us the best silhouette so let’s go with k=4
[tempk(:,4),C,sumd]=kmeans(CNTY_COVID_TRAIN,4);
[silk(:,4), H] = silhouette(CNTY_COVID_TRAIN, tempk(:,4));
%now let’s get the centroids matrix C for k=4
centroids = C;
%take the 10 test vectors from CNTY_COVID_TEST and figure out which of the 4 cluster centroids it’s closest to
%compute its Euclidean distance
D=pdist2(centroids,CNTY_COVID_TEST);
%return the index of the closest cluster distance for each of the 10 counties in I
[D,I]=pdist2(centroids,CNTY_COVID_TEST,'euclidean','smallest',1);
%I has one row with 10 entries, each tells the cluster of the test county
%figure out the meaning of these centroids – do they represent times of peak volumes, times of a certain magnitude, times when there was a spike in certain geographies?
%now let’s try cleaning the training data by adding it across all the counties in a given geographic division in the CENSUS data set, for each of the 9 divisions in the CENSUS
% we want to produce a matrix that has 9 rows x 130 columns = sum of all counties in each of those 9 divisions across time
SUM_DIVISION_COVID_TRAIN = zeros(9,130);
tempsum=zeros(1,130);
for j=1:9
for r=1:215
if CNTY_CENSUS{r,3} == j
tempsum=(CNTY_COVID_TRAIN(r,:));
SUM_DIVISION_COVID_TRAIN(j,:)=SUM_DIVISION_COVID_TRAIN(j,:) + tempsum;
%the new case counts for that division = whatever it was before + tempsum
end;
end;
end;
plot(SUM_DIVISION_COVID_TRAIN(1,:));
hold on
plot(SUM_DIVISION_COVID_TRAIN(2,:));
plot(SUM_DIVISION_COVID_TRAIN(3,:));
plot(SUM_DIVISION_COVID_TRAIN(4,:));
plot(SUM_DIVISION_COVID_TRAIN(5,:));
plot(SUM_DIVISION_COVID_TRAIN(6,:));
plot(SUM_DIVISION_COVID_TRAIN(7,:));
plot(SUM_DIVISION_COVID_TRAIN(8,:));
plot(SUM_DIVISION_COVID_TRAIN(9,:));
I
plot(CNTY_COVID_TEST(1,:));
hold on
plot(CNTY_COVID_TEST(3,:));
plot(CNTY_COVID_TEST(4,:));
plot(CNTY_COVID_TEST(5,:));
plot(CNTY_COVID_TEST(9,:));
%%
figure
plot(CNTY_COVID_TEST(1,:))
hold on
plot(CNTY_COVID_TEST(2,:))
plot(CNTY_COVID_TEST(8,:))
plot(CNTY_COVID_TEST(9,:))
plot(CNTY_COVID_TEST(10,:))