0. Chuẩn bị
library(rio)
setwd("/Users/thuphan/Desktop/dataR")
dulieu <- import("muanha.dta")
head(dulieu)
## TIME GIADV NGANG DAI DTICH DTN KCACH MTIEN STHUONG XEHOI SLAU PHONG
## 1 1 0.10172414 6.8 7.0 116 12 10.5 1 0 0 2 3
## 2 2 0.05309735 10.0 20.0 226 12 10.5 1 0 0 1 3
## 3 3 0.11538462 6.0 10.0 52 7 11.0 0 1 0 3 3
## 4 4 0.09200000 5.0 20.0 100 20 10.6 1 1 0 2 3
## 5 5 0.05714286 8.5 13.5 112 6 6.8 1 1 1 3 4
## 6 6 0.08989899 5.3 18.7 99 10 5.7 1 0 0 2 3
dim(dulieu)
## [1] 998 12
dulieu <-dulieu[,2:7]
dulieu <-na.omit(dulieu)
head(dulieu)
## GIADV NGANG DAI DTICH DTN KCACH
## 1 0.10172414 6.8 7.0 116 12 10.5
## 2 0.05309735 10.0 20.0 226 12 10.5
## 3 0.11538462 6.0 10.0 52 7 11.0
## 4 0.09200000 5.0 20.0 100 20 10.6
## 5 0.05714286 8.5 13.5 112 6 6.8
## 6 0.08989899 5.3 18.7 99 10 5.7
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
datachia <- createDataPartition(dulieu$GIADV, p = 0.8, list = F)
traindata <- dulieu[datachia,]
testdata <- dulieu[-datachia,]
dim(datachia)
## [1] 799 1
dim(traindata)
## [1] 799 6
dim(testdata)
## [1] 198 6
1. k-nearest Neighbors
cv_opts = trainControl(method="cv", number=10)
knn_opts = data.frame(.k=c(seq(3, 11, 2), 25, 51, 101))
congthuc <- GIADV ~ NGANG + DAI + DTICH + DTN + KCACH
results_knn <- train(congthuc, data=traindata, method="knn",
preProcess="range", trControl=cv_opts,
tuneGrid = knn_opts)
results_knn
## k-Nearest Neighbors
##
## 799 samples
## 5 predictor
##
## Pre-processing: re-scaling to [0, 1] (5)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 719, 719, 719, 719, 719, 718, ...
## Resampling results across tuning parameters:
##
## k RMSE Rsquared MAE
## 3 0.1589721 0.5081172 0.08157000
## 5 0.1537864 0.5263503 0.07882821
## 7 0.1537645 0.5241913 0.07820221
## 9 0.1507983 0.5442968 0.07838485
## 11 0.1528351 0.5307682 0.07968215
## 25 0.1528672 0.5308133 0.08067923
## 51 0.1526056 0.5320437 0.08059676
## 101 0.1557356 0.5113642 0.08323277
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 9.
summary(results_knn)
## Length Class Mode
## learn 2 -none- list
## k 1 -none- numeric
## theDots 0 -none- list
## xNames 5 -none- character
## problemType 1 -none- character
## tuneValue 1 data.frame list
## obsLevels 1 -none- logical
## param 0 -none- list
preds_knn = predict(results_knn, testdata)
preds_knn
## [1] 0.12958832 0.06769108 0.12767574 0.08772570 0.16208568 0.18893968
## [7] 0.17187316 0.16561188 0.03267466 0.14688444 0.11303718 0.11806547
## [13] 0.23473453 0.12919552 0.12713352 0.21822885 0.11632602 0.10886221
## [19] 0.12385477 0.12708641 0.07368518 0.22453623 0.15323726 0.11264154
## [25] 0.11341596 0.13141836 0.50968657 0.08010438 0.52224080 0.06757994
## [31] 0.12444123 0.11620326 0.14877873 0.08644568 0.07280399 0.14080203
## [37] 0.07589061 0.09773246 0.15561905 0.10253033 0.15593233 0.11987500
## [43] 0.04119883 0.12167500 0.08246425 0.23894078 0.14842172 0.62468555
## [49] 0.25704408 0.29274670 0.32638889 0.30968207 0.18104545 0.29737961
## [55] 0.38178610 0.33929793 0.40186266 0.28349200 0.26426542 0.32854861
## [61] 0.32989347 0.44117916 0.28673416 0.27981906 0.23906804 0.62468555
## [67] 0.24059975 0.64410280 0.26358369 0.29396780 0.38630143 0.35741058
## [73] 0.56899096 0.47444721 0.28388423 0.26021238 0.47601501 0.53959732
## [79] 0.27078531 0.47685763 0.23292922 0.29657145 0.25491028 0.32304856
## [85] 0.24393139 0.24778287 0.16806629 0.41389206 0.37810456 0.26855318
## [91] 0.34999190 0.24890912 0.39968191 0.27677479 0.43379330 0.59804479
## [97] 0.25138028 0.43883666 0.23464912 0.27766437 0.20508884 0.44890975
## [103] 0.44652884 0.41095380 0.29896748 0.34360289 0.34749867 0.31225174
## [109] 0.26251725 0.31966943 0.31252993 0.28471708 0.31094316 0.28567944
## [115] 0.52688004 0.34455799 0.25437347 0.40380322 0.17072319 0.62530770
## [121] 0.28543772 0.31995701 0.24703301 0.12835940 0.34749867 0.35741058
## [127] 0.25333245 0.31118718 0.32923833 0.23999453 0.29073881 0.31636066
## [133] 0.28645473 0.31520950 0.48091028 0.52942808 0.37237481 0.47168459
## [139] 0.50426257 0.32454364 0.37726821 0.61181113 0.06997303 0.06609609
## [145] 0.08279354 0.07183758 0.07082441 0.07939541 0.07035875 0.07434302
## [151] 0.05984862 0.04122726 0.06319954 0.07058838 0.05853731 0.06460161
## [157] 0.05730360 0.06238944 0.08335906 0.08151437 0.07150701 0.06739923
## [163] 0.06147903 0.05571908 0.04255951 0.05618157 0.03970372 0.05826243
## [169] 0.06383686 0.05051186 0.05868338 0.07929727 0.06041348 0.05417338
## [175] 0.06774129 0.06974653 0.05982386 0.07509317 0.05574538 0.08705966
## [181] 0.06494680 0.04157444 0.04402792 0.04212779 0.05210124 0.04371919
## [187] 0.06872181 0.06634391 0.08620343 0.07482837 0.24588972 0.16169497
## [193] 0.37513639 0.37237211 0.28041371 0.12921205 0.21475375 0.24754385
#confusionMatrix(factor(preds_knn), factor(testdata))
2. Neural Nets
results_nnet = train(congthuc, data=traindata, method="avNNet", trControl=cv_opts, preProcess="range", tuneLength=5, trace=F, maxit=100)
## Warning: executing %dopar% sequentially: no parallel backend registered
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, :
## There were missing values in resampled performance measures.
results_nnet
## Model Averaged Neural Network
##
## 799 samples
## 5 predictor
##
## Pre-processing: re-scaling to [0, 1] (5)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 719, 719, 718, 719, 719, 720, ...
## Resampling results across tuning parameters:
##
## size decay RMSE Rsquared MAE
## 1 0e+00 0.2222799 0.5275332 0.13804469
## 1 1e-04 0.1845029 0.5976184 0.10008598
## 1 1e-03 0.1429827 0.6051232 0.07429745
## 1 1e-02 0.1429314 0.6049938 0.07263226
## 1 1e-01 0.1526096 0.5629850 0.08062206
## 3 0e+00 0.2196725 0.6314974 0.13626370
## 3 1e-04 0.1845591 0.6124980 0.10768933
## 3 1e-03 0.1356602 0.6422640 0.07144864
## 3 1e-02 0.1423134 0.6085330 0.07230451
## 3 1e-01 0.1516339 0.5628933 0.07921117
## 5 0e+00 0.2043798 0.6626557 0.12752371
## 5 1e-04 0.1758397 0.6514214 0.10156015
## 5 1e-03 0.1330549 0.6555578 0.07164623
## 5 1e-02 0.1423398 0.6084465 0.07237196
## 5 1e-01 0.1516390 0.5617000 0.07913848
## 7 0e+00 0.2331217 0.6600833 0.14968424
## 7 1e-04 0.1767883 0.6854519 0.10417259
## 7 1e-03 0.1321343 0.6573605 0.07157390
## 7 1e-02 0.1421510 0.6096022 0.07232195
## 7 1e-01 0.1516538 0.5611042 0.07912027
## 9 0e+00 0.2284285 0.6702550 0.15009662
## 9 1e-04 0.1671204 0.6832353 0.09698684
## 9 1e-03 0.1314501 0.6610200 0.07092315
## 9 1e-02 0.1422401 0.6090635 0.07235583
## 9 1e-01 0.1516809 0.5606918 0.07912689
##
## Tuning parameter 'bag' was held constant at a value of FALSE
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were size = 9, decay = 0.001 and bag
## = FALSE.
preds_nnet = predict(results_nnet, testdata)
preds_nnet
## 8 10 12 13 18 21
## 1.383714e-01 1.645105e-01 1.070190e-01 2.434381e-02 6.920612e-02 1.580386e-01
## 23 28 39 42 47 48
## 1.958435e-01 1.741098e-01 1.607157e-05 1.193859e-01 1.575502e-01 8.560462e-02
## 49 51 63 67 77 86
## 1.707602e-01 1.612377e-01 1.260287e-01 1.778633e-01 1.324399e-01 1.293108e-01
## 88 91 101 107 117 119
## 1.393317e-01 7.720203e-02 1.059209e-01 1.214913e-01 1.346454e-01 6.881498e-04
## 120 128 132 136 150 157
## 1.243093e-01 1.530506e-01 1.667185e-01 1.101325e-01 6.116149e-02 7.622835e-02
## 159 162 180 181 184 189
## 1.522457e-02 2.698420e-01 1.329307e-01 5.311389e-02 3.769731e-02 9.337470e-02
## 191 195 216 218 220 225
## 9.218590e-02 8.750444e-02 1.463321e-01 6.014478e-02 1.391778e-01 9.955067e-02
## 227 233 240 256 261 271
## 3.693802e-04 9.077943e-02 6.188296e-02 2.005909e-01 3.903608e-02 7.279679e-01
## 272 274 291 294 300 314
## 2.476647e-01 2.066155e-01 3.589879e-01 4.265441e-01 1.884957e-01 3.450802e-01
## 323 327 330 337 342 347
## 3.608479e-01 3.506614e-01 4.749280e-01 2.815166e-01 2.057678e-01 2.926159e-01
## 356 363 366 368 370 373
## 3.486674e-01 5.206726e-01 4.192585e-01 3.327765e-01 2.688563e-01 7.712820e-01
## 375 376 379 395 401 410
## 2.783242e-01 6.108398e-01 2.956130e-01 2.841274e-01 3.609560e-01 3.345894e-01
## 411 412 428 430 435 436
## 6.713232e-01 4.990572e-01 1.775666e-01 2.537186e-01 5.367170e-01 6.187960e-01
## 448 449 460 470 471 473
## 4.544445e-02 5.321407e-01 2.009078e-01 2.775157e-01 3.048356e-01 4.390969e-01
## 474 475 480 486 487 492
## 4.559564e-01 2.427788e-01 2.126539e-01 4.064940e-01 3.326237e-01 2.734855e-01
## 500 506 510 512 522 533
## 2.457629e-01 3.304809e-01 3.169870e-01 1.759576e-01 4.673555e-01 5.777185e-01
## 536 543 549 553 554 556
## 1.914802e-01 4.168924e-01 1.518910e-01 2.232857e-01 2.201866e-01 5.012491e-01
## 559 561 567 570 573 574
## 4.297961e-01 3.833799e-01 2.865750e-01 2.497871e-01 3.143295e-01 3.464578e-01
## 579 581 583 586 587 588
## 2.584067e-01 2.419981e-01 2.515393e-01 2.570247e-01 3.464784e-01 2.630965e-01
## 592 598 603 606 610 615
## 5.604822e-01 3.598862e-01 1.581682e-01 2.880804e-01 2.324211e-01 6.383847e-01
## 620 624 628 630 638 642
## 2.951184e-01 3.190302e-01 1.842880e-01 3.646173e-03 2.855589e-01 3.316603e-01
## 643 644 652 655 660 668
## 2.604119e-01 4.047067e-01 3.091225e-01 2.910512e-01 4.109127e-01 2.625126e-01
## 674 678 681 683 684 696
## 3.431201e-01 2.716879e-01 4.839098e-01 4.551048e-01 4.651966e-01 5.057563e-01
## 697 700 704 706 710 713
## 3.810669e-01 3.528073e-01 4.550843e-01 7.353971e-01 7.841950e-02 1.127189e-01
## 719 728 729 731 732 744
## 3.283196e-02 5.440646e-02 5.392946e-02 4.540206e-01 7.257369e-02 7.366085e-02
## 745 749 753 768 779 786
## 3.642750e-02 2.966853e-02 4.491794e-02 1.193574e-01 4.787901e-02 2.074938e-02
## 790 791 795 797 801 809
## 8.749518e-03 2.601790e-02 1.557492e-02 2.978148e-03 3.826567e-02 4.443598e-02
## 814 815 816 827 828 831
## 6.369356e-02 3.824237e-02 2.048886e-02 2.065014e-02 1.397550e-02 3.628290e-02
## 839 840 855 858 862 866
## 3.190505e-02 2.651270e-04 4.819518e-02 9.259780e-02 4.513981e-02 2.118871e-02
## 876 877 885 886 892 894
## 8.380753e-02 6.455833e-02 2.786795e-03 1.074634e-02 3.224464e-04 7.000809e-02
## 902 904 906 907 908 912
## 8.288972e-02 5.015104e-02 1.395328e-03 1.311326e-02 3.597687e-03 3.872805e-02
## 935 941 944 948 950 953
## 9.388024e-02 9.144863e-02 6.438527e-02 8.544085e-02 1.589769e-01 2.479170e-03
## 955 957 961 962 985 996
## 4.353921e-01 4.114282e-01 3.043938e-01 2.479574e-01 1.989398e-01 2.272745e-01
3. Trees & Forests
set.seed(1234)
rf_opts = data.frame(.mtry=c(2:6))
results_rf = train(congthuc, data=traindata, method="rf",
preProcess='range',trControl=cv_opts, tuneGrid=rf_opts,
n.tree=100)
results_rf
## Random Forest
##
## 799 samples
## 5 predictor
##
## Pre-processing: re-scaling to [0, 1] (5)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 719, 721, 719, 719, 719, 719, ...
## Resampling results across tuning parameters:
##
## mtry RMSE Rsquared MAE
## 2 0.1491944 0.5936631 0.07279747
## 3 0.1517978 0.5968194 0.07298214
## 4 0.1527534 0.6144991 0.07302973
## 5 0.1553125 0.6237837 0.07387512
## 6 0.1538947 0.6269796 0.07300114
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was mtry = 2.
preds_rf = predict(results_rf, testdata)
preds_rf
## 8 10 12 13 18 21 23
## 0.13893838 0.06399121 0.11754774 0.07227251 0.16788797 0.12606732 0.14936873
## 28 39 42 47 48 49 51
## 0.16187496 0.03513476 0.13092159 0.12074513 0.18020450 0.19700030 0.11182806
## 63 67 77 86 88 91 101
## 0.15412962 0.18445549 0.11895803 0.13938627 0.13393824 0.11805579 0.07280365
## 107 117 119 120 128 132 136
## 0.14960400 0.17723598 0.09518656 0.10806500 0.20065843 0.11167701 0.12184783
## 150 157 159 162 180 181 184
## 0.11654011 0.06402103 0.10747037 0.10545986 0.12368441 0.08916497 0.07574472
## 189 191 195 216 218 220 225
## 0.12977567 0.07410510 0.08893455 0.13384326 0.10853883 0.14986912 0.12633378
## 227 233 240 256 261 271 272
## 0.02891479 0.09406856 0.11114543 0.24317613 0.15478040 0.73251202 0.27162810
## 274 291 294 300 314 323 327
## 0.21630562 0.32754043 0.36523773 0.17629070 0.36759008 0.40027364 0.28250145
## 330 337 342 347 356 363 366
## 0.53583684 0.25635324 0.25251825 0.29810399 0.27477944 0.46191162 0.35964345
## 368 370 373 375 376 379 395
## 0.30386782 0.21424953 0.82083691 0.18131387 0.60706231 0.32859528 0.28263725
## 401 410 411 412 428 430 435
## 0.37927236 0.35938549 0.73551801 0.51570588 0.24417600 0.21322067 0.46729790
## 436 448 449 460 470 471 473
## 0.66726834 0.19470980 0.50765856 0.22779049 0.26758850 0.24226007 0.39813959
## 474 475 480 486 487 492 500
## 0.33650146 0.23901922 0.17409248 0.32244996 0.37398643 0.28263345 0.26343580
## 506 510 512 522 533 536 543
## 0.32982489 0.49020168 0.25785875 0.43036829 0.55603851 0.20693064 0.40895452
## 549 553 554 556 559 561 567
## 0.19302050 0.28663092 0.24077591 0.52017303 0.43196685 0.42044222 0.30947292
## 570 573 574 579 581 583 586
## 0.34400113 0.35049647 0.33691663 0.26211767 0.24471398 0.28725450 0.28119576
## 587 588 592 598 603 606 610
## 0.30696667 0.29082326 0.58874323 0.34344216 0.21816069 0.36972678 0.22178340
## 615 620 624 628 630 638 642
## 0.64797425 0.26728742 0.34063508 0.22972622 0.17199898 0.34191508 0.36719616
## 643 644 652 655 660 668 674
## 0.26303282 0.34532983 0.39909569 0.28078177 0.28575481 0.30116919 0.18583314
## 678 681 683 684 696 697 700
## 0.21971828 0.42027225 0.39100341 0.37343758 0.42611425 0.42353234 0.27381725
## 704 706 710 713 719 728 729
## 0.44204069 0.61907310 0.11953535 0.07958925 0.09877477 0.07599156 0.06765891
## 731 732 744 745 749 753 768
## 0.08645048 0.05709703 0.09425016 0.06574919 0.04207488 0.06709522 0.08699960
## 779 786 790 791 795 797 801
## 0.05250627 0.05435434 0.05343980 0.05937071 0.06923533 0.05765600 0.06778695
## 809 814 815 816 827 828 831
## 0.05547576 0.07776617 0.05928462 0.06172049 0.04791967 0.04806324 0.06031647
## 839 840 855 858 862 866 876
## 0.06073210 0.03805019 0.06588997 0.07881465 0.05341407 0.06280283 0.08961597
## 877 885 886 892 894 902 904
## 0.06913271 0.03792726 0.09038702 0.05919183 0.06544637 0.05652986 0.04714218
## 906 907 908 912 935 941 944
## 0.04530551 0.04136632 0.04477863 0.06774892 0.08131620 0.08007955 0.05878924
## 948 950 953 955 957 961 962
## 0.07562438 0.20100595 0.16174236 0.44048963 0.33671119 0.26506663 0.18430866
## 985 996
## 0.22491664 0.26290275
4. Support Vector Machines
set.seed(1234)
results_svm = train(congthuc, data=traindata, method="svmLinear",
preProcess="range", trControl=cv_opts, tuneLength=5)
results_svm
## Support Vector Machines with Linear Kernel
##
## 799 samples
## 5 predictor
##
## Pre-processing: re-scaling to [0, 1] (5)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 719, 721, 719, 719, 719, 719, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 0.1583742 0.5055941 0.08581693
##
## Tuning parameter 'C' was held constant at a value of 1
preds_svm = predict(results_svm, testdata)
preds_svm
## 8 10 12 13 18 21
## 0.172608900 0.078507611 0.158149109 0.089243596 0.229492304 0.195442450
## 23 28 39 42 47 48
## 0.307052051 0.222593259 0.072447632 0.192676255 0.160788788 0.152215858
## 49 51 63 67 77 86
## 0.209857732 0.155380958 0.187087380 0.249868082 0.135794730 0.149241071
## 88 91 101 107 117 119
## 0.210615008 0.166839123 0.102990390 0.161901551 0.194630416 0.097624746
## 120 128 132 136 150 157
## 0.141432380 0.184725218 0.200584984 0.154334347 0.181623881 0.089749516
## 159 162 180 181 184 189
## 0.079485719 0.197284110 0.153063584 0.091262118 0.091844986 0.157303112
## 191 195 216 218 220 225
## 0.111404575 0.118938449 0.211713159 0.093530145 0.158863907 0.149117114
## 227 233 240 256 261 271
## 0.008875950 0.142400699 0.135416450 0.222254279 0.216900692 0.435709269
## 272 274 291 294 300 314
## 0.231124597 0.282254025 0.279412806 0.339416711 0.159634622 0.260534411
## 323 327 330 337 342 347
## 0.275563013 0.334909854 0.409887861 0.255863955 0.198705304 0.283239867
## 356 363 366 368 370 373
## 0.326102414 0.286441986 0.239569613 0.266596322 0.223740122 0.440440489
## 375 376 379 395 401 410
## 0.225416190 0.367656874 0.291123210 0.288847291 0.368348388 0.369732503
## 411 412 428 430 435 436
## 0.419004833 0.356366518 0.188835633 0.168213847 0.447972522 0.365076859
## 448 449 460 470 471 473
## 0.257469944 0.306300285 0.211550003 0.251718196 0.258675776 0.270803448
## 474 475 480 486 487 492
## 0.260281982 0.230983032 0.231071430 0.389680919 0.329228153 0.255874543
## 500 506 510 512 522 533
## 0.246252551 0.218627486 0.263655458 0.200495603 0.322277785 0.363753877
## 536 543 549 553 554 556
## 0.220086864 0.344883777 0.247181497 0.240409975 0.227814106 0.313428811
## 559 561 567 570 573 574
## 0.320594003 0.312451489 0.287753964 0.259781210 0.264555879 0.321921938
## 579 581 583 586 587 588
## 0.257714149 0.243845926 0.264990224 0.261308696 0.272986219 0.268112322
## 592 598 603 606 610 615
## 0.353464443 0.325272441 0.213972352 0.370248319 0.244840600 0.325059715
## 620 624 628 630 638 642
## 0.284840596 0.321003591 0.211327780 0.182582777 0.260312969 0.369589242
## 643 644 652 655 660 668
## 0.242797372 0.270038532 0.260680419 0.236131908 0.254114796 0.277907863
## 674 678 681 683 684 696
## 0.239056486 0.250363603 0.408850191 0.349421224 0.368743290 0.356444367
## 697 700 704 706 710 713
## 0.506329474 0.304118177 0.328542415 0.366960300 0.089234848 0.106266287
## 719 728 729 731 732 744
## 0.151331706 0.027497604 0.049287505 0.054016076 0.053543978 0.064813575
## 745 749 753 768 779 786
## 0.042106505 -0.087083727 -0.004087416 0.121329333 -0.018068736 -0.010181989
## 790 791 795 797 801 809
## -0.021422406 0.058438207 0.063578060 0.117209848 0.058087892 0.041932019
## 814 815 816 827 828 831
## 0.060742517 -0.034176180 -0.005497100 0.022707698 -0.036158041 -0.035444505
## 839 840 855 858 862 866
## 0.040748089 0.043743527 0.011971075 0.099360765 0.025816137 0.036931219
## 876 877 885 886 892 894
## 0.091683032 0.055621883 0.046188842 0.061483452 0.098829549 0.094357615
## 902 904 906 907 908 912
## 0.091682723 0.021083326 0.035471116 0.033022978 0.064879247 -0.025161863
## 935 941 944 948 950 953
## 0.127186955 0.116056149 0.052557080 0.106940200 0.196444480 0.133944932
## 955 957 961 962 985 996
## 0.362988387 0.275128486 0.266047007 0.205516848 0.241814825 0.226381000