K-Means Clustering and Logistic Regression Analysis on Fraud Data
VerifiedAdded on 2022/09/12
|8
|3858
|15
Practical Assignment
AI Summary
This assignment involves performing K-means clustering and logistic regression using R software on an insurance fraud dataset. The solution begins by loading the dataset and preprocessing the 'atty' variable, categorizing it into 0 and 1. Input variables are then standardized. The data is partitioned into training and testing sets. The K-means algorithm is executed with k=2, and the results, including cluster means and sizes, are displayed. Logistic regression is then applied. The assignment includes the use of the 'set.seed' function to ensure consistent cluster results and includes the generation of a confusion matrix to evaluate the performance of the logistic regression model. The assignment addresses the analysis of insurance fraud data using clustering and classification techniques in R, covering data preparation, model execution, and result interpretation, including the transformation of the 'age' variable.

Assignment
Answer1
##K-means Clustering Analysis## K-means clustering is an unsupervised learning technique which clas-
sifies the data objects into clusters.The clusters are formed in such a way that objects within a cluster are
as homogeneous as possible and heterogeneous between the clusters.
At first.the FRAUD dataset is loaded.
mydata=read.csv("D:/FraudRaw.csv")
THe variable “atty” is categorized into 0 and 1 where 0=“none” and 1="others.
mydata$atty=ifelse(mydata$atty=="none",0,1)
Now the input variables are standardised.
mydata$Age=scale(mydata$Age,center=T,scale=T)
mydata$Claim=scale(mydata$Claim,center=T,scale=T)
mydata$Gender=scale(mydata$Gender,center=T,scale=T)
mydata$tickets=scale(mydata$tickets,center =T,scale=T )
mydata$prior.claims=scale(mydata$prior.claims,center=T,scale=T)
mydata$atty=scale(mydata$atty,center=T,scale=T)
Data partition:
train_data=subset(mydata[1:1000,])
test_data=subset(mydata[4001:5000,])
Now k-means algorithm is executed for k=2 showing the two results of the outcome variable (1 and 0).To
get a fixed cluster size every time the code is executed, the function set.seed is used.
fit_train=kmeans(train_data,2,iter.max = 10,nstart=25,set.seed(123))
fit_train$size
## [1] 16 984
print(fit_train)
## K-means clustering with 2 clusters of sizes 16, 984
##
## Cluster means:
## Age Gender Claim tickets prior.claims atty
## 1 -0.26291745 -0.245178303.44925345 0.302756371 3.49460649 7.6034180
1
Answer1
##K-means Clustering Analysis## K-means clustering is an unsupervised learning technique which clas-
sifies the data objects into clusters.The clusters are formed in such a way that objects within a cluster are
as homogeneous as possible and heterogeneous between the clusters.
At first.the FRAUD dataset is loaded.
mydata=read.csv("D:/FraudRaw.csv")
THe variable “atty” is categorized into 0 and 1 where 0=“none” and 1="others.
mydata$atty=ifelse(mydata$atty=="none",0,1)
Now the input variables are standardised.
mydata$Age=scale(mydata$Age,center=T,scale=T)
mydata$Claim=scale(mydata$Claim,center=T,scale=T)
mydata$Gender=scale(mydata$Gender,center=T,scale=T)
mydata$tickets=scale(mydata$tickets,center =T,scale=T )
mydata$prior.claims=scale(mydata$prior.claims,center=T,scale=T)
mydata$atty=scale(mydata$atty,center=T,scale=T)
Data partition:
train_data=subset(mydata[1:1000,])
test_data=subset(mydata[4001:5000,])
Now k-means algorithm is executed for k=2 showing the two results of the outcome variable (1 and 0).To
get a fixed cluster size every time the code is executed, the function set.seed is used.
fit_train=kmeans(train_data,2,iter.max = 10,nstart=25,set.seed(123))
fit_train$size
## [1] 16 984
print(fit_train)
## K-means clustering with 2 clusters of sizes 16, 984
##
## Cluster means:
## Age Gender Claim tickets prior.claims atty
## 1 -0.26291745 -0.245178303.44925345 0.302756371 3.49460649 7.6034180
1
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

## 2 0.01196396 0.01699362 -0.09557363 -0.008829906-0.07540348 -0.1314935
## outcome
## 1 0.3750000
## 2 0.0101626
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2
## 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
## 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2
## 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
## 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2
## 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
## 2 2 2 2 2 2 2 1 2 2 2 2 2 2 1 2
## 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
## 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2
## 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2
## outcome
## 1 0.3750000
## 2 0.0101626
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2
## 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
## 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2
## 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
## 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2
## 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
## 2 2 2 2 2 2 2 1 2 2 2 2 2 2 1 2
## 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
## 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2
## 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2

## 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
## 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2
## 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
## 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
## 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
## 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2
## 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
## 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2
## 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
## 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
## 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
## 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2
## 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
## 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
## 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
## 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2
## 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
## 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2
## 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
## 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
## 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
## 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2
## 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

## 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 993 994 995 996 997 998 999 1000
## 2 2 2 2 2 2 2 2
##
## Within cluster sum of squares by cluster:
## [1] 122.5507 4442.0650
## (between_SS / total_SS = 22.8 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
It can be seen that the first cluster size is 16(for outcome 1) and second one is 984 (for outcome 0).In the
dataset,there are also 16 cases with outcome 1 in first 1000 observations.Hence,it can be deeduced that
the clustering is properly showing the outcomes.
##cluster means
aggregate(train_data,by=list(fit_train$cluster),FUN = mean)
## Group.1 Age Gender Claim tickets prior.claims
## 1 1 -0.26291745 -0.24517830 3.44925345 0.302756371 3.49460649
## 2 2 0.01196396 0.01699362 -0.09557363 -0.008829906-0.07540348
## atty outcome
## 1 7.6034180 0.3750000
## 2 -0.1314935 0.0101626
the cluster means can be observed from the above table.
fit_test=kmeans(test_data,2,iter.max =10,nstart=25,set.seed(123))
fit_test$size
4
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
## 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 993 994 995 996 997 998 999 1000
## 2 2 2 2 2 2 2 2
##
## Within cluster sum of squares by cluster:
## [1] 122.5507 4442.0650
## (between_SS / total_SS = 22.8 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
It can be seen that the first cluster size is 16(for outcome 1) and second one is 984 (for outcome 0).In the
dataset,there are also 16 cases with outcome 1 in first 1000 observations.Hence,it can be deeduced that
the clustering is properly showing the outcomes.
##cluster means
aggregate(train_data,by=list(fit_train$cluster),FUN = mean)
## Group.1 Age Gender Claim tickets prior.claims
## 1 1 -0.26291745 -0.24517830 3.44925345 0.302756371 3.49460649
## 2 2 0.01196396 0.01699362 -0.09557363 -0.008829906-0.07540348
## atty outcome
## 1 7.6034180 0.3750000
## 2 -0.1314935 0.0101626
the cluster means can be observed from the above table.
fit_test=kmeans(test_data,2,iter.max =10,nstart=25,set.seed(123))
fit_test$size
4
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

## [1] 977 23
##cluster means
aggregate(test_data,by=list(fit_test$cluster),FUN=mean)
## Group.1 Age Gender Claim tickets prior.claims atty
## 1 1 -0.03573399 0.00991683 -0.04735856 0.02865686-0.0598329 -0.1314935
## 2 2 -0.34613405 0.048273992.40392199 0.20350911 3.7701209 7.6034180
## outcome
## 1 0.0174002
## 2 0.2173913
Here the cluster sizes are 977 (for 0) and 23 respectively(for 1).Consequently, the cluster means are obtained.
Limitations:k-means is not appropriate for a dataset with categorical variables.Since the concerned dataset
has many categoricalvariables,any other clustering method would be more suitable to obtain a better
result.Further,since the method is an unsupervised learning,partition of dataset into training and testing
is not required.
Answer 2
Here k-means algorithm is applied on the Loan dataset.At first, the dataset is selected.
data1=read.csv("D:/LoanRaw.csv")
Now the variables credit rating and risk are transformed into numerical variable.
##categorize credit rating
data1$credit=as.numeric(data1$credit)
data1$Risk=as.numeric(data1$Risk)
For credit rating, amber is taken as 1, 2 for green and 3 for red.For risk variable, 1 signifies high, 2 low and
3 medium.Now a new dataset is formed taking age, income,credit rating, risk and on-time variables.
newdata=data1[c(1:2,6:8)]
Standardizing the variables:
newdata$Age=scale(newdata$Age,center=T,scale=T)
newdata$Income=scale(newdata$Income,center=T,scale=T)
newdata$credit=scale(newdata$credit,center=T,scale=T)
newdata$Risk=scale(newdata$Risk,center=T,scale=T)
Now, training and test datasets are selected taking first 600 observations for training and last 50 for testing.
train_data1=subset(newdata[1:600,])
test_data1=subset(newdata[601:650,])
k-means for training set
##k means
fit_train1=kmeans(train_data1,2,iter.max=10,nstart=25,set.seed(123))
fit_train1$size
5
##cluster means
aggregate(test_data,by=list(fit_test$cluster),FUN=mean)
## Group.1 Age Gender Claim tickets prior.claims atty
## 1 1 -0.03573399 0.00991683 -0.04735856 0.02865686-0.0598329 -0.1314935
## 2 2 -0.34613405 0.048273992.40392199 0.20350911 3.7701209 7.6034180
## outcome
## 1 0.0174002
## 2 0.2173913
Here the cluster sizes are 977 (for 0) and 23 respectively(for 1).Consequently, the cluster means are obtained.
Limitations:k-means is not appropriate for a dataset with categorical variables.Since the concerned dataset
has many categoricalvariables,any other clustering method would be more suitable to obtain a better
result.Further,since the method is an unsupervised learning,partition of dataset into training and testing
is not required.
Answer 2
Here k-means algorithm is applied on the Loan dataset.At first, the dataset is selected.
data1=read.csv("D:/LoanRaw.csv")
Now the variables credit rating and risk are transformed into numerical variable.
##categorize credit rating
data1$credit=as.numeric(data1$credit)
data1$Risk=as.numeric(data1$Risk)
For credit rating, amber is taken as 1, 2 for green and 3 for red.For risk variable, 1 signifies high, 2 low and
3 medium.Now a new dataset is formed taking age, income,credit rating, risk and on-time variables.
newdata=data1[c(1:2,6:8)]
Standardizing the variables:
newdata$Age=scale(newdata$Age,center=T,scale=T)
newdata$Income=scale(newdata$Income,center=T,scale=T)
newdata$credit=scale(newdata$credit,center=T,scale=T)
newdata$Risk=scale(newdata$Risk,center=T,scale=T)
Now, training and test datasets are selected taking first 600 observations for training and last 50 for testing.
train_data1=subset(newdata[1:600,])
test_data1=subset(newdata[601:650,])
k-means for training set
##k means
fit_train1=kmeans(train_data1,2,iter.max=10,nstart=25,set.seed(123))
fit_train1$size
5

## [1] 312 288
#cluster means#
aggregate(train_data1,by=list(fit_train1$cluster),FUN=mean)
## Group.1 Age Income credit Risk On.time
## 1 1 0.6966604 0.1829318 0.015583320.8919894 0.9583333
## 2 2 -0.7387395 -0.1538130 0.01942789 -0.9761467 0.8368056
The cluster sizes are 312 and 288 respectively.
k-means for test set
fit_test1=kmeans(test_data1,2,iter.max=10,nstart=25,set.seed(123))
fit_test1$size
## [1] 22 28
aggregate(test_data1,by=list(fit_test1$cluster),FUN=mean)
## Group.1 Age Income credit Risk On.time
## 1 1 -0.6541479 -0.3183541 0.1572499 -0.994393 0.7727273
## 2 2 0.3496493 -0.2061708 -0.49702590.882365 1.0000000
The cluster sizes are 22 and 28 respectively.
Limitations:Since the dataset contains many categoricalvariables,k-means is not suitable for clustering.
Moreover, data partitioning is not required since it is an unsupervised learning technique.
Answer 3
data2=read.csv("D:/FraudRaw.csv")
A new dataset is formed taking first 2000 observations of Fraud dataset.
data3=subset(data2[1:2000,])
Now, the variable Age is transformed as per the rule-20 or younger=0,50 or older=1 and any age between
=age/30.
data3$Age=ifelse(data3$Age>50,1,ifelse(data3$Age<=20,0,(data3$Age/30)))
The variable claim is transformed as claim/5000.
data3$Claim=data3$Claim/5000
Further, the other variables are also transformed as per the requirement.
data3$atty=ifelse(data3$atty=="none",0,1)
data3$tickets=ifelse(data3$tickets=="0",0,ifelse(data3$tickets=="1",0.5,1))
data3$prior.claims=ifelse(data3$prior.claims=="0",0,ifelse(data3$prior.claims=="1",0.5,1))
Now the training and test sets are formed by taking first 1000 rows for training and rest 1000 for test data.
6
#cluster means#
aggregate(train_data1,by=list(fit_train1$cluster),FUN=mean)
## Group.1 Age Income credit Risk On.time
## 1 1 0.6966604 0.1829318 0.015583320.8919894 0.9583333
## 2 2 -0.7387395 -0.1538130 0.01942789 -0.9761467 0.8368056
The cluster sizes are 312 and 288 respectively.
k-means for test set
fit_test1=kmeans(test_data1,2,iter.max=10,nstart=25,set.seed(123))
fit_test1$size
## [1] 22 28
aggregate(test_data1,by=list(fit_test1$cluster),FUN=mean)
## Group.1 Age Income credit Risk On.time
## 1 1 -0.6541479 -0.3183541 0.1572499 -0.994393 0.7727273
## 2 2 0.3496493 -0.2061708 -0.49702590.882365 1.0000000
The cluster sizes are 22 and 28 respectively.
Limitations:Since the dataset contains many categoricalvariables,k-means is not suitable for clustering.
Moreover, data partitioning is not required since it is an unsupervised learning technique.
Answer 3
data2=read.csv("D:/FraudRaw.csv")
A new dataset is formed taking first 2000 observations of Fraud dataset.
data3=subset(data2[1:2000,])
Now, the variable Age is transformed as per the rule-20 or younger=0,50 or older=1 and any age between
=age/30.
data3$Age=ifelse(data3$Age>50,1,ifelse(data3$Age<=20,0,(data3$Age/30)))
The variable claim is transformed as claim/5000.
data3$Claim=data3$Claim/5000
Further, the other variables are also transformed as per the requirement.
data3$atty=ifelse(data3$atty=="none",0,1)
data3$tickets=ifelse(data3$tickets=="0",0,ifelse(data3$tickets=="1",0.5,1))
data3$prior.claims=ifelse(data3$prior.claims=="0",0,ifelse(data3$prior.claims=="1",0.5,1))
Now the training and test sets are formed by taking first 1000 rows for training and rest 1000 for test data.
6
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

##data partition
set.seed(123)
trainingset=subset(data3[1:1000,])
test_set=subset(data3[1001:2000,])
Logistic regression:
##logistic regression
fitmodel=glm(outcome~.,family=binomial(link="logit"),data=trainingset)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(fitmodel)
##
## Call:
## glm(formula = outcome ~ ., family = binomial(link = "logit"),
## data = trainingset)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.1304 -0.1691 -0.1415 -0.1070 3.1773
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -5.91257 1.45301 -4.069 4.72e-05 ***
## Age -0.18721 0.61724 -0.303 0.762
## Gender -0.09271 0.55879 -0.166 0.868
## Claim 4.42771 2.78025 1.593 0.111
## tickets -31.49909 4017.16635-0.008 0.994
## prior.claims -32.20165 4238.74044-0.008 0.994
## atty 34.55308 4238.74055 0.008 0.993
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 164.07 on 999 degrees of freedom
## Residual deviance: 123.36 on 993 degrees of freedom
## AIC: 137.36
##
## Number of Fisher Scoring iterations: 20
p=predict(fitmodel,test_set,type="response")
The confusion matrix is obtained as,
library(InformationValue)
## Warning: package 'InformationValue' was built under R version 3.6.3
7
set.seed(123)
trainingset=subset(data3[1:1000,])
test_set=subset(data3[1001:2000,])
Logistic regression:
##logistic regression
fitmodel=glm(outcome~.,family=binomial(link="logit"),data=trainingset)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(fitmodel)
##
## Call:
## glm(formula = outcome ~ ., family = binomial(link = "logit"),
## data = trainingset)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.1304 -0.1691 -0.1415 -0.1070 3.1773
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -5.91257 1.45301 -4.069 4.72e-05 ***
## Age -0.18721 0.61724 -0.303 0.762
## Gender -0.09271 0.55879 -0.166 0.868
## Claim 4.42771 2.78025 1.593 0.111
## tickets -31.49909 4017.16635-0.008 0.994
## prior.claims -32.20165 4238.74044-0.008 0.994
## atty 34.55308 4238.74055 0.008 0.993
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 164.07 on 999 degrees of freedom
## Residual deviance: 123.36 on 993 degrees of freedom
## AIC: 137.36
##
## Number of Fisher Scoring iterations: 20
p=predict(fitmodel,test_set,type="response")
The confusion matrix is obtained as,
library(InformationValue)
## Warning: package 'InformationValue' was built under R version 3.6.3
7
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

confusionMatrix(test_set$outcome,p)
## 0 1
## 0 985 15
8
## 0 1
## 0 985 15
8
1 out of 8
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.




