Kết nối data
library(readxl)
library(qqplotr)
## Warning: package 'qqplotr' was built under R version 4.0.5
## Loading required package: ggplot2
##
## Attaching package: 'qqplotr'
## The following objects are masked from 'package:ggplot2':
##
## stat_qq_line, StatQqLine
setwd("d:/TAILIEU/R Studio/Performance")
dulieu <-read_excel("data.performance.xlsx")
dulieu <-data.frame(dulieu)
head(dulieu)
## lanhdao sangtao luong thuong trungthanh thunhap tuisangtao
## 1 -1.5501910 0.4596061 0.3379045 0.5049469 -0.3749169 1 0.4105673
## 2 -0.1893130 0.5193391 -1.7197400 1.4582160 -0.3707190 1 0.4181218
## 3 -0.1680707 -0.2495031 -1.5250830 -0.8022256 -0.7763493 1 -0.6422983
## 4 -0.6012416 -1.1185100 0.2997132 -1.2922280 -1.3775820 1 -1.3379770
## 5 0.2530354 -0.4436758 0.8574147 0.1420081 0.6400768 0 -0.2775573
## 6 0.2476853 -1.1073270 -0.9956320 -1.2483090 -1.3763690 1 -1.3379770
hoiquy <- lm(data = dulieu, trungthanh~ lanhdao + sangtao+ luong + thuong)
logit <-glm(data=dulieu,thunhap ~ lanhdao + sangtao+ luong + thuong, family = "binomial")
summary(hoiquy)
##
## Call:
## lm(formula = trungthanh ~ lanhdao + sangtao + luong + thuong,
## data = dulieu)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.58928 -0.39804 0.00815 0.35717 1.49553
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.023e-08 2.475e-02 0.00 1
## lanhdao 4.731e-01 2.478e-02 19.09 <2e-16 ***
## sangtao 3.886e-01 2.478e-02 15.69 <2e-16 ***
## luong 3.720e-01 2.478e-02 15.01 <2e-16 ***
## thuong 4.276e-01 2.478e-02 17.26 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5535 on 495 degrees of freedom
## Multiple R-squared: 0.6961, Adjusted R-squared: 0.6936
## F-statistic: 283.4 on 4 and 495 DF, p-value: < 2.2e-16
summary(logit)
##
## Call:
## glm(formula = thunhap ~ lanhdao + sangtao + luong + thuong, family = "binomial",
## data = dulieu)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.23843 -0.41049 -0.03071 0.42529 2.58327
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.1135 0.1430 -0.794 0.427
## lanhdao -1.6534 0.1896 -8.719 < 2e-16 ***
## sangtao -1.5199 0.1810 -8.396 < 2e-16 ***
## luong -1.5933 0.1978 -8.054 7.99e-16 ***
## thuong -1.7500 0.1958 -8.936 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 693.08 on 499 degrees of freedom
## Residual deviance: 317.35 on 495 degrees of freedom
## AIC: 327.35
##
## Number of Fisher Scoring iterations: 6
1.Lưu phần dư có logit
library(performance)
## Warning: package 'performance' was built under R version 4.0.5
library(see)
## Warning: package 'see' was built under R version 4.0.5
ketqua1 <- binned_residuals(logit)
## Warning: Probably bad model fit. Only about 55% of the residuals are inside the error bounds.
ketqua1

2.Kiểm tra tương quan chuỗi
check_autocorrelation(hoiquy)
## OK: Residuals appear to be independent and not autocorrelated (p = 0.624).
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
dwtest(hoiquy)
##
## Durbin-Watson test
##
## data: hoiquy
## DW = 1.9519, p-value = 0.2945
## alternative hypothesis: true autocorrelation is greater than 0
bgtest(hoiquy)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: hoiquy
## LM test = 0.28153, df = 1, p-value = 0.5957
3. Kiểm tra đa cộng tuyến
check_collinearity(hoiquy)
## # Check for Multicollinearity
##
## Low Correlation
##
## Term VIF Increased SE Tolerance
## lanhdao 1.00 1.00 1.00
## sangtao 1.00 1.00 1.00
## luong 1.00 1.00 1.00
## thuong 1.00 1.00 1.00
plot(check_collinearity(hoiquy))

multicollinearity(hoiquy)
## # Check for Multicollinearity
##
## Low Correlation
##
## Term VIF Increased SE Tolerance
## lanhdao 1.00 1.00 1.00
## sangtao 1.00 1.00 1.00
## luong 1.00 1.00 1.00
## thuong 1.00 1.00 1.00
library(car)
## Loading required package: carData
vif(hoiquy)
## lanhdao sangtao luong thuong
## 1 1 1 1
4. Kiểm tra phương sai sai số thay đổi
check_heteroscedasticity(hoiquy)
## OK: Error variance appears to be homoscedastic (p = 0.112).
plot(check_heteroscedasticity(hoiquy))
## OK: Error variance appears to be homoscedastic (p = 0.112).

bptest(hoiquy)
##
## studentized Breusch-Pagan test
##
## data: hoiquy
## BP = 3.4289, df = 4, p-value = 0.4888
5. Kiểm tra phương sai đồng nhất ( ngược với trên)
#check_homogeneity(hoiquy)
#plot(check_homogeneity(hoiquy))
6. Kiểm tra tổng quát
check_model(hoiquy)

7. Kiểm tra phần dư phân phối chuẩn
check_normality(hoiquy)
## OK: residuals appear as normally distributed (p = 0.701).
phandu <-residuals(hoiquy)
shapiro.test(phandu)
##
## Shapiro-Wilk normality test
##
## data: phandu
## W = 0.99767, p-value = 0.7216
8. Kiểm tra phần tử ngoại vi
check_outliers(hoiquy)
## OK: No outliers detected.
check_outliers(phandu)
## Warning: 21 outliers detected (cases 20, 63, 88, 89, 91, 107, 119, 201, 218, 226, 258, 259, 305, 316, 358, 359, 366, 459, 468, 475, 496).
library(outliers)
## Warning: package 'outliers' was built under R version 4.0.3
#Grubbs’s test
grubbs.test(phandu)
##
## Grubbs test for one outlier
##
## data: phandu
## G.91 = 2.88288, U = 0.98331, p-value = 0.9523
## alternative hypothesis: lowest value -1.5892753664043 is an outlier
#Dixon’s test
#dixon.test(phandu)
#Rosner’s test
library(EnvStats)
## Warning: package 'EnvStats' was built under R version 4.0.5
##
## Attaching package: 'EnvStats'
## The following object is masked from 'package:car':
##
## qqPlot
## The following objects are masked from 'package:stats':
##
## predict, predict.lm
## The following object is masked from 'package:base':
##
## print.default
rosnerTest(phandu)
## $distribution
## [1] "Normal"
##
## $statistic
## R.1 R.2 R.3
## 2.882875 2.732191 2.739616
##
## $sample.size
## [1] 500
##
## $parameters
## k
## 3
##
## $alpha
## [1] 0.05
##
## $crit.value
## lambda.1 lambda.2 lambda.3
## 3.863127 3.862597 3.862066
##
## $n.outliers
## [1] 0
##
## $alternative
## [1] "Up to 3 observations are not\n from the same Distribution."
##
## $method
## [1] "Rosner's Test for Outliers"
##
## $data
## 1 2 3 4 5 6
## -0.161797292 -0.466751467 0.310520238 -0.217387287 0.313106338 -0.159017413
## 7 8 9 10 11 12
## 0.449554079 0.920597057 0.489095257 -0.285393510 -0.712663745 0.542822049
## 13 14 15 16 17 18
## 0.220372706 -0.222395660 -0.358640041 0.331202834 0.281856985 -0.215510494
## 19 20 21 22 23 24
## -0.211516849 -1.140961105 0.132585538 -0.030347798 0.202573519 -0.052128380
## 25 26 27 28 29 30
## 0.222316958 0.325773098 0.965019854 0.118487527 0.444016676 -0.003154654
## 31 32 33 34 35 36
## -0.884379389 -0.453799638 -0.385469954 0.156334950 0.092753763 0.599375929
## 37 38 39 40 41 42
## 0.121617698 -0.002040482 0.606126932 -0.220974295 -0.679503871 -0.340634413
## 43 44 45 46 47 48
## 0.015991560 0.374159374 0.040397846 0.589530079 -1.063109137 0.169502369
## 49 50 51 52 53 54
## -1.029420815 -0.053874921 1.035369133 -0.665441186 -0.476950038 -0.682991233
## 55 56 57 58 59 60
## -0.888068541 -0.033970718 0.310697557 -0.171527678 -0.264926209 -0.303229345
## 61 62 63 64 65 66
## -0.090783710 -0.568472447 1.149786326 -0.163099992 0.480589156 0.266670856
## 67 68 69 70 71 72
## 0.306411252 0.371301039 0.031157804 0.269335954 0.518493667 0.447719727
## 73 74 75 76 77 78
## -0.706906003 -0.668765028 -0.482402751 -0.940815128 -0.544065000 0.101148811
## 79 80 81 82 83 84
## 0.046723948 0.393606040 -0.056081510 0.809781761 -0.675221834 -0.470143152
## 85 86 87 88 89 90
## -0.552549456 -0.801573369 0.873425572 1.289099820 -1.264204552 -0.870560791
## 91 92 93 94 95 96
## -1.589275366 -0.276502813 0.156515012 0.139617405 0.222597661 0.644854860
## 97 98 99 100 101 102
## 0.130174407 0.345800598 0.118844400 0.233586377 0.192008655 -0.297627267
## 103 104 105 106 107 108
## -0.026329774 -1.058708155 -0.324924288 0.067620258 1.119994020 0.598281117
## 109 110 111 112 113 114
## 0.648890669 -0.434081326 -0.610005349 0.619902679 0.138294265 0.554036010
## 115 116 117 118 119 120
## -0.014171810 0.582454599 -0.630602239 0.157588729 1.407833917 -0.354009804
## 121 122 123 124 125 126
## -0.739234653 0.279662503 0.413445486 -0.056699985 0.326333872 0.430208520
## 127 128 129 130 131 132
## 0.063295273 0.323757526 -0.527161642 -0.203001963 0.184099969 -0.202316235
## 133 134 135 136 137 138
## -0.083191224 0.934806744 0.834847619 -0.406361552 -0.657300533 -0.594373876
## 139 140 141 142 143 144
## -1.005200094 0.985767271 -0.141739919 0.198450032 0.450430439 0.033275494
## 145 146 147 148 149 150
## 0.934247105 0.175650171 -0.591359833 0.114917290 -0.559372298 -0.275516299
## 151 152 153 154 155 156
## -0.189867582 0.137883623 0.535552695 0.839921570 0.235899814 -0.172193199
## 157 158 159 160 161 162
## -0.556606245 0.297573648 -0.252397737 -0.478483986 -0.007811541 0.196706792
## 163 164 165 166 167 168
## -0.156844767 -0.585827780 0.161245870 0.003476626 0.586788630 0.551747257
## 169 170 171 172 173 174
## -0.113003555 0.846115620 -1.075217597 0.192599414 0.471526002 -0.603723781
## 175 176 177 178 179 180
## -0.027051840 0.033992846 0.467229320 0.007127620 -0.032131198 0.520373420
## 181 182 183 184 185 186
## 0.873745536 -0.449074773 0.098986541 0.332455535 -0.971051268 0.496712726
## 187 188 189 190 191 192
## -0.143496865 0.179656057 0.143431307 0.258411941 0.234418310 -0.708078888
## 193 194 195 196 197 198
## 0.835681009 0.172179088 -0.923273117 -0.069343746 -0.839580490 -0.062517684
## 199 200 201 202 203 204
## 0.377812871 -0.313184985 -1.172330416 -0.257825007 -0.499411018 0.281612443
## 205 206 207 208 209 210
## -0.171134661 -0.698039154 0.202418738 0.337136959 0.274494687 -0.484690479
## 211 212 213 214 215 216
## 0.569273198 -0.886193107 -0.259896956 0.584795570 -0.123603634 -0.866127634
## 217 218 219 220 221 222
## -0.613168437 1.334532032 0.064390723 -0.800831516 0.190065127 0.738381841
## 223 224 225 226 227 228
## -0.231054299 -0.035083775 0.001365181 1.495529172 -0.158610433 0.730724459
## 229 230 231 232 233 234
## 0.254667956 -0.396347134 0.113903501 -0.558517016 -0.335619919 0.210849387
## 235 236 237 238 239 240
## -0.616043741 0.279796382 0.423618734 0.166402841 -0.312021275 0.576327072
## 241 242 243 244 245 246
## -0.579297413 -0.081728231 0.478539088 -0.235893455 -0.403119445 0.224227793
## 247 248 249 250 251 252
## -0.168234878 -0.800098784 -0.878668246 0.644579897 0.148524552 0.533365846
## 253 254 255 256 257 258
## 0.287784034 -0.432787578 0.423662021 0.199561303 -0.833048706 1.297480879
## 259 260 261 262 263 264
## 1.366456222 -0.827497120 0.709045557 -0.186582591 -0.669335765 0.480400204
## 265 266 267 268 269 270
## -1.035757732 -0.864058289 -0.647716735 0.896322314 0.734150002 0.009170966
## 271 272 273 274 275 276
## -0.051928622 -0.606739326 0.582960894 -0.217927208 0.116088991 -0.417381484
## 277 278 279 280 281 282
## 0.122596622 0.315192105 0.118883020 0.195853662 -0.457969601 0.745778205
## 283 284 285 286 287 288
## -0.057462572 0.565372817 0.852178663 -0.445385728 -0.263713305 0.625614302
## 289 290 291 292 293 294
## -0.200835294 -0.151556322 -0.199921788 0.304840482 0.358837763 -0.673885816
## 295 296 297 298 299 300
## -0.414468644 -0.597579810 -0.692514017 -1.069216786 -0.751016831 -0.430934663
## 301 302 303 304 305 306
## 0.291060669 -0.220349918 0.887858286 -0.475742192 -1.163238052 0.088699513
## 307 308 309 310 311 312
## -0.838854500 -0.366636592 -0.384142933 0.065191959 -0.426865053 0.225686612
## 313 314 315 316 317 318
## -0.253755529 -0.448713288 -0.200921527 1.232275185 -0.843370293 0.001814354
## 319 320 321 322 323 324
## -0.032871293 -0.375668736 -0.727799262 0.323542912 0.232401266 1.098574969
## 325 326 327 328 329 330
## 0.676289703 1.105962203 0.437870854 0.520183995 0.422189692 0.625328736
## 331 332 333 334 335 336
## 0.626035472 0.052182937 -0.510129283 0.483155473 -0.722526124 -0.381321626
## 337 338 339 340 341 342
## -0.326796230 0.273152742 0.106336483 0.356616734 0.374604848 -0.067445583
## 343 344 345 346 347 348
## -0.239598943 0.806562182 0.274855823 -0.082647097 0.081261860 0.595899272
## 349 350 351 352 353 354
## 0.689903594 -0.714339249 0.268200134 1.083293710 -0.527896778 0.347669715
## 355 356 357 358 359 360
## 0.589198680 -0.382461248 0.107588210 -1.491898660 1.250869032 -0.476736515
## 361 362 363 364 365 366
## 0.102831691 0.487915237 -0.786618353 -0.191452087 0.142015261 -1.130462233
## 367 368 369 370 371 372
## -0.030648121 -0.329644602 -0.288863891 -0.280178950 0.690408934 0.626327762
## 373 374 375 376 377 378
## 0.396279138 0.138065041 -0.029477818 0.294743892 -0.098241844 -0.719754882
## 379 380 381 382 383 384
## 0.202301522 -0.523292638 0.833941535 0.298796833 1.031573631 -0.475120111
## 385 386 387 388 389 390
## -0.354778499 0.243875756 0.095048753 -0.372404396 0.044084864 -0.269315117
## 391 392 393 394 395 396
## 0.333939127 -0.140776454 0.413118095 -0.813247336 -0.818067266 0.513565519
## 397 398 399 400 401 402
## -0.255235048 -0.587513864 1.013790031 -0.065054484 -0.465069729 0.742418481
## 403 404 405 406 407 408
## 0.331230772 0.488483006 -0.789436788 -0.410867026 0.999089623 -0.167985690
## 409 410 411 412 413 414
## -0.157838499 -0.094018200 -0.459073438 -0.801067194 -0.314689534 0.479918449
## 415 416 417 418 419 420
## 0.003942093 0.402767040 -0.544892395 0.283496956 0.075698609 1.047943189
## 421 422 423 424 425 426
## 0.271910424 -0.221217095 0.167934620 0.333915021 0.130472950 0.627724463
## 427 428 429 430 431 432
## 0.348988254 0.606975907 0.089485963 0.815314955 -0.239951558 -0.175542801
## 433 434 435 436 437 438
## -0.690639984 -0.074045846 -0.471573901 0.485714317 0.313685895 -1.024865048
## 439 440 441 442 443 444
## -0.650420446 0.634861457 0.703527696 -0.272321109 -0.463533244 0.260621465
## 445 446 447 448 449 450
## 0.622685878 0.203637781 -0.055874291 -0.041082762 -0.455265293 0.414685261
## 451 452 453 454 455 456
## -0.248262649 0.167079682 -0.452270018 -0.123768556 0.034311314 -0.289610204
## 457 458 459 460 461 462
## -0.406412852 0.187208574 1.176514132 -0.136611752 -0.152516519 1.044733430
## 463 464 465 466 467 468
## 0.618201828 0.412807018 0.081870774 0.819251855 0.635232887 -1.214622519
## 469 470 471 472 473 474
## 0.465254233 0.178754047 0.717245308 -0.023015105 0.586422967 -0.843444492
## 475 476 477 478 479 480
## -1.325479724 0.281139149 -0.288214428 -0.155783703 0.005900241 -0.293794530
## 481 482 483 484 485 486
## -0.213487199 -0.815573530 0.145074804 0.839493446 -0.552425843 0.193520833
## 487 488 489 490 491 492
## -0.581878590 -0.149154778 0.729424464 -0.917142757 0.439401057 -0.591180712
## 493 494 495 496 497 498
## -0.031905148 0.386146245 0.111189696 -1.231968585 -0.751231390 -0.752617143
## 499 500
## -0.364792987 -0.308777740
##
## $data.name
## [1] "phandu"
##
## $bad.obs
## [1] 0
##
## $all.stats
## i Mean.i SD.i Value Obs.Num R.i+1 lambda.i+1 Outlier
## 1 0 -2.624680e-17 0.5512814 -1.589275 91 2.882875 3.863127 FALSE
## 2 1 3.184921e-03 0.5472105 -1.491899 358 2.732191 3.862597 FALSE
## 3 2 6.187096e-03 0.5436316 1.495529 226 2.739616 3.862066 FALSE
##
## attr(,"class")
## [1] "gofOutlier"
#outlierTest(phandu)
9. So sánh model
compare_performance(hoiquy, logit)
## Warning: When comparing models, please note that probably not all models were fit from
## same data.
## # Comparison of Model Performance Indices
##
## Name | Model | AIC | BIC | RMSE | Sigma | R2 | R2 (adj.) | Tjur's R2 | Log_loss | Score_log | Score_spherical | PCP
## -----------------------------------------------------------------------------------------------------------------------------------
## hoiquy | lm | 834.428 | 859.715 | 0.551 | 0.554 | 0.696 | 0.694 | | | | |
## logit | glm | 327.351 | 348.424 | 0.319 | 0.801 | | | 0.598 | 0.317 | -Inf | 0.012 | 0.799
11 Ước lượng thông số hiệu suất
performance_accuracy(hoiquy)
## # Accuracy of Model Predictions
##
## Accuracy: 83.11%
## SE: 2.56%-points
## Method: Correlation between observed and predicted
performance_accuracy(logit)
## # Accuracy of Model Predictions
##
## Accuracy: 93.59%
## SE: 2.22%-points
## Method: Area under Curve