data("mtcars")
colnames(mtcars)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
mtcars3<-mtcars[,1:3]
mtcarsk3<-kmeans(mtcars3,centers=3)
mtcarsk3$size
## [1] 8 16 8
mtcarsk3$centers
## mpg cyl disp
## 1 14.6000 8.000 399.1250
## 2 24.5000 4.625 122.2937
## 3 16.7625 7.500 279.1750
mtcarsk3$cluster
## Mazda RX4 Mazda RX4 Wag Datsun 710
## 2 2 2
## Hornet 4 Drive Hornet Sportabout Valiant
## 3 1 3
## Duster 360 Merc 240D Merc 230
## 1 2 2
## Merc 280 Merc 280C Merc 450SE
## 2 2 3
## Merc 450SL Merc 450SLC Cadillac Fleetwood
## 3 3 1
## Lincoln Continental Chrysler Imperial Fiat 128
## 1 1 2
## Honda Civic Toyota Corolla Toyota Corona
## 2 2 2
## Dodge Challenger AMC Javelin Camaro Z28
## 3 3 1
## Pontiac Firebird Fiat X1-9 Porsche 914-2
## 1 2 2
## Lotus Europa Ford Pantera L Ferrari Dino
## 2 1 2
## Maserati Bora Volvo 142E
## 3 2
plot.new()
selectedData <- mtcars[, c("mpg","qsec")]
clusters <- kmeans(selectedData, 3)
plot(x=selectedData[,1], y=selectedData[,2],
col = mtcarsk3$cluster,
pch = 20, cex = 3,
main = paste("qsec", "vs.", "mpg"),
xlab = "mpg",
ylab = "qsec")
points(mtcarsk3$centers, pch = 4, cex = 2, lwd = 4)
abline(lm(selectedData[,2] ~ selectedData[,1]))
##Describe each cluster
cluster 1 red dots have low qsec, mpg
cluster 2 black dots have high qesc,mpg
cluster 3 greeen dots have medium qsec,mpg
data("PimaIndiansDiabetes")
colnames(PimaIndiansDiabetes)
## [1] "pregnant" "glucose" "pressure" "triceps" "insulin" "mass"
## [7] "pedigree" "age" "diabetes"
p1, How do glucose and Insulin measurements are relate? p2, How do pressure and Insulin measurements are relate? p3, How do mass and Insulin measurements are relate?
p1<-plot(PimaIndiansDiabetes$glucose,PimaIndiansDiabetes$insulin,col="red",pch=19)
p2<-plot(PimaIndiansDiabetes$pressure,PimaIndiansDiabetes$insulin,col="blue",pch=19)
p3<-plot(PimaIndiansDiabetes$mass,PimaIndiansDiabetes$insulin,col="green",pch=19)
diabetes<- PimaIndiansDiabetes[,1:8]
dkmeans<-kmeans(diabetes,center=13)
dkmeans$size
## [1] 60 91 82 19 62 24 36 35 88 80 54 120 17
dkmeans$centers
## pregnant glucose pressure triceps insulin mass pedigree
## 1 4.250000 73.96667 67.9666667 12.4166667 1.5833333 29.49500 0.3775833
## 2 2.428571 91.79121 69.3186813 25.1208791 59.2087912 30.21319 0.4681319
## 3 2.597561 107.06098 65.9024390 28.2560976 108.0609756 32.05610 0.4800610
## 4 4.421053 148.94737 70.0000000 28.7368421 354.0000000 33.51579 0.4816316
## 5 2.693548 119.48387 69.5806452 31.9354839 184.6612903 34.62258 0.5988387
## 6 5.041667 143.62500 77.1250000 31.9583333 87.2083333 32.61250 0.4369583
## 7 3.555556 117.00000 0.6666667 2.0000000 0.6944444 25.76389 0.3931667
## 8 4.114286 151.25714 76.2857143 32.0285714 259.3714286 37.03143 0.6018857
## 9 3.897727 107.71591 72.3181818 29.0000000 0.4545455 31.13977 0.4278750
## 10 4.950000 165.18750 79.3250000 14.8625000 0.5375000 34.97125 0.4925375
## 11 4.777778 153.25926 75.0555556 28.9074074 149.0555556 33.59815 0.5360741
## 12 4.741667 119.06667 75.6916667 0.2416667 0.0000000 30.45583 0.4002750
## 13 3.176471 166.35294 73.4117647 35.7058824 555.2352941 36.81765 0.6916471
## age
## 1 31.56667
## 2 26.71429
## 3 27.35366
## 4 33.15789
## 5 31.08065
## 6 36.58333
## 7 30.44444
## 8 34.74286
## 9 31.92045
## 10 41.57500
## 11 36.16667
## 12 37.78333
## 13 34.82353
dkmeans$cluster
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 10 1 10 3 5 12 2 7 13 12 12 10 12 13 11 7 8 12
## 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 3 3 8 12 10 9 11 3 10 3 6 12 9 8 2 1 9 5
## 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## 12 9 9 5 6 12 9 8 10 10 12 1 9 7 2 2 1 8
## 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 4 1 8 3 10 3 7 12 1 11 12 9 9 12 2 6 3 11
## 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## 12 8 1 1 1 9 7 9 9 7 2 9 12 3 9 2 6 9
## 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## 1 5 2 12 6 8 9 2 2 5 10 10 12 2 1 11 12 11
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## 1 2 11 13 2 1 11 10 12 1 9 2 6 9 3 12 12 3
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 3 3 11 12 11 12 8 1 2 11 2 3 12 4 12 9 2 12
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## 8 9 1 3 10 1 5 12 11 13 10 10 3 3 2 6 10 3
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 8 9 12 5 10 12 12 2 12 11 7 2 2 11 1 11 10 12
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## 1 3 1 1 12 10 13 6 3 11 12 6 10 7 1 5 12 2
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## 3 4 9 12 9 2 5 9 8 10 3 10 1 10 10 11 5 8
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## 3 3 1 12 13 10 7 5 2 2 12 10 13 2 12 4 2 12
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## 2 10 11 10 10 12 9 3 12 5 5 10 12 13 4 9 12 12
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 2 9 8 9 9 9 4 11 11 7 9 10 12 2 7 9 12 7
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 9 2 12 2 12 2 9 3 12 8 10 3 11 10 12 11 13 5
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## 2 2 2 2 5 5 10 11 4 5 5 12 7 11 2 12 10 3
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## 11 11 5 5 1 5 6 3 9 3 2 10 3 10 8 9 9 10
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 9 11 5 10 3 2 9 2 7 12 2 8 7 12 11 10 3 2
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## 1 12 12 6 6 7 2 1 1 12 1 2 1 10 5 7 2 8
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## 4 10 9 10 8 3 12 9 2 11 13 3 2 3 5 4 2 2
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## 10 2 3 9 5 2 3 2 9 9 8 2 5 10 4 3 10 8
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## 3 9 1 10 1 12 6 1 10 5 12 12 10 13 9 5 4 6
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 5 13 9 10 1 3 5 2 2 9 5 8 7 11 11 5 7 2
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## 2 12 1 7 10 10 9 12 10 2 3 12 12 10 2 3 2 2
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## 3 12 5 7 3 10 12 2 8 6 2 1 2 9 12 3 2 3
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## 7 11 10 9 9 12 12 10 5 3 6 9 4 9 2 3 7 8
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## 13 8 9 10 2 9 9 3 7 10 12 2 11 5 2 1 1 2
## 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
## 9 1 6 5 2 12 1 5 1 1 3 6 11 12 1 4 2 3
## 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
## 7 12 12 1 2 3 5 12 3 12 2 7 2 7 12 1 5 11
## 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
## 5 5 9 2 2 8 8 5 6 10 9 3 12 2 3 5 9 12
## 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## 9 1 12 8 2 2 1 3 9 3 11 5 1 12 2 3 4 2
## 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
## 3 12 12 10 10 9 12 12 13 1 12 12 11 7 9 3 12 3
## 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
## 8 11 1 7 10 3 9 7 9 11 7 9 8 2 8 5 5 11
## 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
## 4 9 11 12 12 1 9 7 5 9 10 3 12 2 12 12 12 9
## 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
## 12 3 12 5 1 12 12 2 3 2 3 12 10 7 5 4 11 11
## 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
## 11 9 3 3 6 12 3 13 3 5 12 2 10 10 8 11 9 3
## 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
## 10 9 5 6 11 9 2 8 12 10 10 1 12 8 2 10 3 12
## 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
## 12 5 12 9 5 5 12 10 3 3 1 13 11 7 11 12 5 9
## 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
## 10 7 3 1 7 4 10 5 4 9 9 8 12 4 11 9 5 9
## 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
## 1 5 11 3 12 9 5 10 10 1 6 12 11 5 12 9 3 1
## 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
## 5 12 11 3 3 12 11 3 10 2 11 10 12 6 9 13 10 6
## 757 758 759 760 761 762 763 764 765 766 767 768
## 9 12 12 10 1 10 1 5 9 3 12 9
diabetes3<- PimaIndiansDiabetes[,1:8]
dkmeans3<-kmeans(diabetes3,center=3)
dkmeans3$size
## [1] 38 495 235
dkmeans3$centers
## pregnant glucose pressure triceps insulin mass pedigree age
## 1 4.026316 158.4474 72.00000 32.26316 441.2895 35.10789 0.5692105 34.76316
## 2 3.981818 114.0081 67.77172 14.99798 14.4000 30.80545 0.4319313 33.75960
## 3 3.527660 129.3277 71.44681 30.30638 159.1021 33.98936 0.5402766 31.90213
dkmeans3$cluster
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 2 2 2 3 3 2 2 2 1 2 2 2 2 1 3 2 3 2
## 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 2 3 3 2 2 2 3 3 2 3 3 2 2 3 2 2 2 3
## 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## 2 2 2 3 2 2 2 3 2 2 2 2 2 2 2 2 2 1
## 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 1 2 1 3 2 3 2 2 2 3 2 2 2 2 2 3 3 3
## 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## 2 3 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2
## 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## 2 3 2 2 2 3 2 2 2 3 2 2 2 2 2 3 2 3
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## 2 2 3 1 2 2 3 2 2 2 2 2 3 2 3 2 2 3
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 3 3 3 2 3 2 3 2 2 3 2 3 2 1 2 2 2 2
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## 3 2 2 3 2 2 3 2 3 1 2 2 3 3 2 3 2 3
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 3 2 2 3 2 2 2 2 2 3 2 2 2 3 2 3 2 2
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## 2 3 2 2 2 2 1 2 3 3 2 3 2 2 2 3 2 2
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## 3 1 2 2 2 2 3 2 3 2 2 2 2 2 2 3 3 3
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## 3 3 2 2 1 2 2 3 2 2 2 2 1 2 2 1 2 2
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## 2 2 3 2 2 2 2 2 2 3 3 2 2 1 1 2 2 2
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 2 2 3 2 2 2 1 3 3 2 2 2 2 2 2 2 2 2
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 2 2 2 2 2 2 2 3 2 3 2 3 3 2 2 3 1 3
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## 2 2 2 2 3 3 2 3 1 3 3 2 2 3 2 2 2 3
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## 3 3 3 3 2 3 3 2 2 3 2 2 3 2 3 2 2 2
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 2 3 3 2 3 2 2 2 2 2 2 3 2 2 3 2 3 2
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## 2 2 2 3 2 2 2 2 2 2 2 2 2 2 3 2 2 3
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## 1 2 2 2 3 2 2 2 2 3 1 3 2 3 3 1 2 2
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## 2 2 2 2 3 2 3 2 2 2 3 2 3 2 1 2 2 3
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## 3 2 2 2 2 2 3 2 2 3 2 2 2 1 2 3 1 2
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 3 1 2 2 2 3 3 2 2 2 3 3 2 3 3 3 2 2
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 3 2 2
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## 3 2 3 2 3 2 2 2 3 2 2 2 2 2 2 3 2 3
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## 2 3 2 2 2 2 2 2 3 3 2 2 1 2 2 3 2 3
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## 1 3 2 2 2 2 2 3 2 2 2 2 3 3 2 2 2 2
## 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
## 2 2 3 3 2 2 2 3 2 2 2 3 3 2 2 1 2 3
## 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
## 2 2 2 2 2 3 3 2 3 2 2 2 2 2 2 2 3 3
## 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
## 3 3 2 2 2 3 3 3 2 2 2 3 2 2 3 3 2 2
## 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## 2 2 2 3 2 2 2 2 2 3 3 3 2 2 2 3 1 2
## 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
## 3 2 2 2 2 2 2 2 1 2 2 2 3 2 2 3 2 3
## 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
## 3 3 2 2 2 3 2 2 2 3 2 2 3 2 3 3 3 3
## 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
## 1 2 3 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2
## 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
## 2 3 2 3 2 2 2 2 3 2 3 2 2 2 3 1 3 3
## 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
## 3 2 3 3 2 2 3 1 3 3 2 2 2 2 3 3 2 3
## 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
## 2 2 3 3 3 2 2 3 2 2 2 2 2 3 2 2 3 2
## 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
## 2 3 2 2 3 3 2 2 3 3 2 1 3 2 3 2 3 2
## 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
## 2 2 3 2 2 1 2 3 1 2 2 3 2 1 3 2 3 2
## 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
## 2 3 3 3 2 2 3 2 2 2 2 2 3 3 2 2 3 2
## 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
## 3 2 3 3 3 2 3 3 2 2 3 2 2 2 2 1 2 3
## 757 758 759 760 761 762 763 764 765 766 767 768
## 2 2 2 2 2 2 2 3 2 3 2 2
diabetesc<-PimaIndiansDiabetes
head(PimaIndiansDiabetes)
## pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 1 6 148 72 35 0 33.6 0.627 50 pos
## 2 1 85 66 29 0 26.6 0.351 31 neg
## 3 8 183 64 0 0 23.3 0.672 32 pos
## 4 1 89 66 23 94 28.1 0.167 21 neg
## 5 0 137 40 35 168 43.1 2.288 33 pos
## 6 5 116 74 0 0 25.6 0.201 30 neg
diabetesc$cid<-dkmeans$cluster
head(diabetesc)
## pregnant glucose pressure triceps insulin mass pedigree age diabetes cid
## 1 6 148 72 35 0 33.6 0.627 50 pos 10
## 2 1 85 66 29 0 26.6 0.351 31 neg 1
## 3 8 183 64 0 0 23.3 0.672 32 pos 10
## 4 1 89 66 23 94 28.1 0.167 21 neg 3
## 5 0 137 40 35 168 43.1 2.288 33 pos 5
## 6 5 116 74 0 0 25.6 0.201 30 neg 12
table(diabetesc$diabetes,diabetesc$cid)
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13
## neg 55 85 70 11 38 14 20 16 66 25 17 78 5
## pos 5 6 12 8 24 10 16 19 22 55 37 42 12
diabetesc3<-PimaIndiansDiabetes
diabetesc3$cid<-dkmeans3$cluster
head(diabetesc3)
## pregnant glucose pressure triceps insulin mass pedigree age diabetes cid
## 1 6 148 72 35 0 33.6 0.627 50 pos 2
## 2 1 85 66 29 0 26.6 0.351 31 neg 2
## 3 8 183 64 0 0 23.3 0.672 32 pos 2
## 4 1 89 66 23 94 28.1 0.167 21 neg 3
## 5 0 137 40 35 168 43.1 2.288 33 pos 3
## 6 5 116 74 0 0 25.6 0.201 30 neg 2
table(diabetesc3$diabetes,diabetesc3$cid)
##
## 1 2 3
## neg 16 347 137
## pos 22 148 98
Eescribe the row of data represents and Describe each of your columns used - give a one sentence description of the column each record captured the infomation of number of times pregant, plasma glucose concentration a 2 hours in oral glucose toleranct test, diastolic blood pressure, triceps kin fold thickness, 2-hour serum insulin, Body mass index, Diabetes pedigree function, Age, Class variable of diabetes
data info Until 02/28/2011 this web page indicated that there were no missing values in the dataset. As pointed out by a repository user, this cannot be true: there are zeros in places where they are biologically impossible, such as the blood pressure attribute. It seems very likely that zero values encode missing data. However, since the dataset donors made no such statement we encourage you to use your best judgement and state your assumptions.
3.Give a one- or two-word description to each cluster - in other words, give each cluster a label or name when k=13 the information is not group very nicely, but when k=3 the pos/neg status has higher concerntration, however, there are too many factors we need to take into account, so it is not as simple as the iris data