library(rattle)
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(MASS)
data("Boston")
str(Boston)
## 'data.frame': 506 obs. of 14 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
Boston2=Boston[,c(6,11:14)]
str(Boston2)
## 'data.frame': 506 obs. of 5 variables:
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
names(Boston2)
## [1] "rm" "ptratio" "black" "lstat" "medv"
clusters=8
Boston2.cl=kmeans(Boston2,clusters)
Boston2.cl
## K-means clustering with 8 clusters of sizes 75, 60, 193, 29, 72, 36, 13, 28
##
## Cluster means:
## rm ptratio black lstat medv
## 1 6.980160 16.90400 392.48240 5.911600 32.44800
## 2 5.785417 19.59000 394.39217 22.365000 13.56833
## 3 6.171093 18.71762 393.87415 10.630415 21.50570
## 4 7.597724 15.95172 382.25690 4.271379 47.55517
## 5 6.106278 18.62083 368.77611 13.448889 20.47222
## 6 6.068944 19.91389 50.15417 20.477500 12.63889
## 7 6.008538 18.28462 228.75846 18.757692 17.36923
## 8 5.778143 18.74643 319.38929 17.581429 16.76429
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 3 3 1 1 1 1 3 3 2 3 2 3 3 3 5 3 3 3
## 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 8 3 5 3 2 2 2 8 5 8 3 5 5 5 7 5 7 3
## 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## 5 3 3 1 1 1 1 3 3 3 3 2 2 3 3 3 3 3
## 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 3 1 3 1 3 3 3 5 3 3 1 3 3 3 3 3 3 5
## 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## 3 5 3 3 5 3 3 3 1 3 3 3 3 1 3 3 3 1
## 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## 3 3 3 3 3 5 3 1 1 1 3 3 6 3 3 3 2 3
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## 3 3 3 3 3 3 3 5 3 3 8 3 3 5 5 5 5 3
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 5 2 3 2 3 3 3 3 7 2 5 3 2 2 2 2 2 2
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## 2 7 7 2 5 5 5 8 8 7 8 6 6 4 5 5 8 4
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 4 4 3 7 4 7 8 8 8 5 3 3 3 1 3 3 1 1
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## 1 1 1 1 3 1 4 1 1 1 4 1 1 1 5 4 1 5
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## 1 1 1 3 1 4 4 3 3 3 5 2 3 2 3 1 5 3
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## 3 1 3 3 3 2 1 1 4 4 1 5 4 1 5 5 4 4
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## 5 5 3 1 5 3 3 3 5 5 5 2 3 5 5 3 3 5
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 1 1 3 3 4 4 1 1 1 4 4 1 1 3 1 4 4 3
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 3 3 3 1 1 1 1 1 1 1 4 1 4 4 1 3 8 3
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## 3 5 1 1 1 3 3 1 1 3 5 5 3 3 1 1 1 1
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## 1 1 3 3 5 3 3 3 3 3 2 3 3 3 3 3 3 3
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 3 3 3 3 3 5 5 3 5 3 3 3 3 3 3 3 3 1
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## 3 3 1 3 5 3 3 1 3 5 3 1 3 5 5 3 3 3
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## 5 5 5 5 5 5 8 6 4 4 4 4 4 2 2 3 5 2
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## 2 2 2 2 2 2 8 2 2 2 5 2 2 5 2 2 2 2
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## 2 2 2 8 2 2 5 2 8 2 5 8 8 7 6 6 6 7
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 6 6 6 6 6 6 8 8 8 6 6 6 6 6 6 6 6 6
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## 6 6 6 6 6 6 6 2 2 2 3 2 7 6 8 2 2 8
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## 6 5 2 5 6 6 6 6 7 3 7 3 3 3 3 8 6 8
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## 5 3 3 3 3 5 5 8 2 5 5 3 3 3 3 3 5 3
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## 3 3 2 8 8 2 3 3 3 3 2 3 3 3 3 3 3 3
## 505 506
## 3 3
##
## Within cluster sum of squares by cluster:
## [1] 3358.768 3468.721 6340.040 3880.316 12970.421 59413.964 15688.620
## [8] 11260.822
## (between_SS / total_SS = 97.3 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss"
## [5] "tot.withinss" "betweenss" "size" "iter"
## [9] "ifault"
Boston2.cl$centers
## rm ptratio black lstat medv
## 1 6.980160 16.90400 392.48240 5.911600 32.44800
## 2 5.785417 19.59000 394.39217 22.365000 13.56833
## 3 6.171093 18.71762 393.87415 10.630415 21.50570
## 4 7.597724 15.95172 382.25690 4.271379 47.55517
## 5 6.106278 18.62083 368.77611 13.448889 20.47222
## 6 6.068944 19.91389 50.15417 20.477500 12.63889
## 7 6.008538 18.28462 228.75846 18.757692 17.36923
## 8 5.778143 18.74643 319.38929 17.581429 16.76429
plot(Boston2[4:5],col=Boston2.cl$cluster)
points(Boston2.cl$centers,pch=19,cex=1.5,col=1:clusters)
