Tugas Akhir Anreg Analisis

Alur = Input data -> Cek eksploratif -> Cek gauss markov dan normalitas -> Cek Pencilan dll

library(readxl)
data<- read_excel("C:/Users/User/Documents/raziq/semes 4/anreg/tugas ahir/data tugas akhir.xlsx")
data
## # A tibble: 29 × 17
##    daerah      y     x1     x2    x3    x4    x5    x6     x7    x8    x9    x10
##    <chr>   <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>
##  1 Merauke  10.2 45013. 230932  3.43   260 29.7    503 366296  70.1  67   1.62e6
##  2 Jayawi…  37.1  2629. 269553  2.51   153  9.62   481 461058  58.0  59.6 1.46e6
##  3 Jayapu…  12.1 14082. 166171 10.3    174 30.0    302 601005  71.7  67.0 1.21e6
##  4 Nabire   23.8 11806. 169136  6.65   141 21.6    271 612872  68.8  68.1 1.61e6
##  5 Kepula…  26.1  2429. 112676  5.3    152 41.2    122 653819  67.7  69.1 1.07e6
##  6 Biak N…  24.4  2340. 134650 10.4    221 35.6    435 574402  72.2  68.2 1.18e6
##  7 Paniai   36.6  5307. 220410  0      110 61.3     18 512058  56.3  66.4 9.01e5
##  8 Puncak…  36    5986. 224527  1.5     38 31.9      2 639503  48.4  65.2 2.04e6
##  9 Mimika   14.2 18299. 311969  7.8    152 13.5    647 870355  74.2  72.3 1.54e6
## 10 Boven …  19.9 23558.  64285  8.09   112 36.2    127 486179  61.5  60.0 1.18e6
## # … with 19 more rows, and 5 more variables: x11 <dbl>, x12 <dbl>, x13 <dbl>,
## #   x14 <dbl>, x15 <dbl>

Model Regresi

modelreg <- lm(y~x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13+x14+x15,data= data)
modelreg
## 
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + 
##     x10 + x11 + x12 + x13 + x14 + x15, data = data)
## 
## Coefficients:
## (Intercept)           x1           x2           x3           x4           x5  
##  -4.910e+00    8.103e-05   -9.974e-05    4.161e-01   -5.601e-02    6.304e-03  
##          x6           x7           x8           x9          x10          x11  
##   1.706e-02   -9.222e-06   -2.809e-01    6.591e-01   -2.152e-07   -1.379e-04  
##         x12          x13          x14          x15  
##   1.360e-06    4.627e-01    1.778e+00    3.753e-02

Summary

summary(modelreg)
## 
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + 
##     x10 + x11 + x12 + x13 + x14 + x15, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.1117 -1.5951 -0.1434  1.6801  8.9780 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -4.910e+00  3.168e+01  -0.155  0.87921   
## x1           8.103e-05  2.139e-04   0.379  0.71096   
## x2          -9.974e-05  4.868e-05  -2.049  0.06125 . 
## x3           4.161e-01  6.746e-01   0.617  0.54805   
## x4          -5.601e-02  7.660e-02  -0.731  0.47761   
## x5           6.304e-03  7.480e-02   0.084  0.93412   
## x6           1.706e-02  1.899e-02   0.898  0.38532   
## x7          -9.223e-06  1.343e-05  -0.687  0.50418   
## x8          -2.809e-01  2.262e-01  -1.242  0.23624   
## x9           6.591e-01  5.290e-01   1.246  0.23480   
## x10         -2.152e-07  3.773e-06  -0.057  0.95538   
## x11         -1.379e-04  2.264e-04  -0.609  0.55294   
## x12          1.360e-06  1.641e-06   0.829  0.42205   
## x13          4.627e-01  1.480e-01   3.126  0.00803 **
## x14          1.778e+00  7.619e-01   2.334  0.03630 * 
## x15          3.753e-02  2.363e-02   1.588  0.13627   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.614 on 13 degrees of freedom
## Multiple R-squared:  0.8956, Adjusted R-squared:  0.7752 
## F-statistic: 7.436 on 15 and 13 DF,  p-value: 0.0004026

Anova

anova(modelreg)
## Analysis of Variance Table
## 
## Response: y
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## x1         1 676.27  676.27 31.7703 8.111e-05 ***
## x2         1  67.15   67.15  3.1549  0.099097 .  
## x3         1 956.01  956.01 44.9123 1.467e-05 ***
## x4         1   0.03    0.03  0.0015  0.969818    
## x5         1  43.07   43.07  2.0232  0.178465    
## x6         1   7.52    7.52  0.3534  0.562398    
## x7         1 105.78  105.78  4.9693  0.044066 *  
## x8         1 197.75  197.75  9.2902  0.009336 ** 
## x9         1   1.49    1.49  0.0702  0.795270    
## x10        1   0.01    0.01  0.0004  0.984677    
## x11        1  92.29   92.29  4.3355  0.057644 .  
## x12        1  12.65   12.65  0.5941  0.454607    
## x13        1  92.24   92.24  4.3336  0.057693 .  
## x14        1  68.36   68.36  3.2115  0.096413 .  
## x15        1  53.69   53.69  2.5222  0.136269    
## Residuals 13 276.72   21.29                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Cek eksploratif

plot(modelreg)

## Warning in sqrt(crit * p * (1 - hh)/hh): NaNs produced

## Warning in sqrt(crit * p * (1 - hh)/hh): NaNs produced

## Asumsi GAUSS MARKOV # 1. Nilai Harapan galat sama dengan nol

t.test(modelreg$residuals,
       mu = 0,
       conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  modelreg$residuals
## t = 4.9633e-18, df = 28, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -1.195797  1.195797
## sample estimates:
##    mean of x 
## 2.897437e-18

2. galat saling bebas

library(randtests)
runs.test(modelreg$residuals)
## 
##  Runs Test
## 
## data:  modelreg$residuals
## statistic = 1.5407, runs = 19, n1 = 14, n2 = 14, n = 28, p-value =
## 0.1234
## alternative hypothesis: nonrandomness
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dwtest(modelreg)
## 
##  Durbin-Watson test
## 
## data:  modelreg
## DW = 1.7203, p-value = 0.1173
## alternative hypothesis: true autocorrelation is greater than 0
bgtest(modelreg)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  modelreg
## LM test = 0.76559, df = 1, p-value = 0.3816

3. Ragam galat homogen

cek.homogen = lm(formula = abs(modelreg$residuals) ~ data$y,
                 data = data)
summary(cek.homogen)
## 
## Call:
## lm(formula = abs(modelreg$residuals) ~ data$y, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1818 -1.7791 -0.4564  1.4330  6.9180 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  2.305044   1.352882   1.704   0.0999 .
## data$y      -0.006463   0.045174  -0.143   0.8873  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.326 on 27 degrees of freedom
## Multiple R-squared:  0.0007575,  Adjusted R-squared:  -0.03625 
## F-statistic: 0.02047 on 1 and 27 DF,  p-value: 0.8873
library(lmtest)
bptest(modelreg)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelreg
## BP = 12.999, df = 15, p-value = 0.6023
library(car)
## Loading required package: carData
ncvTest(modelreg)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.2095992, Df = 1, p = 0.64708

4. cek normalitas

ks.test(modelreg$residuals, "pnorm", mean=mean(modelreg$residuals), sd=sd(modelreg$residuals))
## 
##  Exact one-sample Kolmogorov-Smirnov test
## 
## data:  modelreg$residuals
## D = 0.14338, p-value = 0.5426
## alternative hypothesis: two-sided
shapiro.test(modelreg$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  modelreg$residuals
## W = 0.9472, p-value = 0.1547

Kesimpulan

y=data$y
x1=data$x1
x2=data$x2
x3=data$x3
x4=data$x4
x5=data$x5
x6=data$x6
x7=data$x7
x8=data$x8
x9=data$x9
x10=data$x10
x11=data$x11
x12=data$x12
x13=data$x13
x14=data$x14
x15=data$x15
data<-data.frame(cbind(y,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15))
data
##        y       x1     x2    x3  x4    x5  x6      x7    x8    x9     x10
## 1  10.16 45013.35 230932  3.43 260 29.67 503  366296 70.09 67.00 1619834
## 2  37.09  2629.01 269553  2.51 153  9.62 481  461058 58.03 59.64 1459370
## 3  12.13 14082.21 166171 10.33 174 29.95 302  601005 71.69 67.05 1210925
## 4  23.83 11806.09 169136  6.65 141 21.57 271  612872 68.83 68.06 1606658
## 5  26.09  2429.03 112676  5.30 152 41.20 122  653819 67.66 69.12 1071327
## 6  24.45  2339.78 134650 10.38 221 35.56 435  574402 72.19 68.25 1180692
## 7  36.59  5306.87 220410  0.00 110 61.31  18  512058 56.31 66.44  901010
## 8  36.00  5986.19 224527  1.50  38 31.86   2  639503 48.37 65.15 2037148
## 9  14.17 18298.95 311969  7.80 152 13.52 647  870355 74.19 72.32 1536464
## 10 19.90 23558.27  64285  8.09 112 36.22 127  486179 61.53 59.97 1176631
## 11 26.09 24262.23 108295  5.77 172 23.58 127  351801 58.15 65.11  701155
## 12 24.83 25015.31 110105  2.38 149 91.81  23  383790 50.55 58.05  814912
## 13 37.64 16365.94 350880  3.88 187 30.59  26  428433 49.37 65.93 1077844
## 14 30.46 13751.92  77872  4.12  99 68.47  12  580393 45.44 64.44 1266742
## 15 32.60  2990.01 236986  1.07  86 21.35  51  404812 49.50 65.71 1177982
## 16 13.84 14068.37  41515  4.83  87 32.01  42  535586 63.63 66.36 1073259
## 17 16.00  9526.32  61623  2.56 102 22.71 175  677050 66.40 66.69  798421
## 18 29.85 10778.76  33943  4.76  66 41.77  68  691544 64.94 66.33 1578129
## 19 37.91   660.61  22547  4.12  58 15.06  82  471401 62.30 65.94 1099147
## 20 28.78 28042.39  36483  2.55  74 92.60  13  727830 51.78 57.77  544272
## 21 37.18  5886.89 106533  0.00  38  6.69   0  360779 31.55 55.27 1462880
## 22 38.73  2339.87 196399  0.00  82  6.37   6  490508 47.86 66.06 1505891
## 23 36.76  4101.50  50685  0.00  46  2.40  75  412574 47.57 63.59 1096064
## 24 33.25  3148.29 101973  0.00  69 54.25   0  347171 48.34 65.42 1683573
## 25 36.26  7701.03 114741  0.00  55 31.65   0  664865 43.04 65.74 1007631
## 26 28.81  3792.93 116206  0.00  69 21.17   0  512654 54.84 65.73  485881
## 27 41.66  5334.45 135043  1.22  31 61.99   0  662465 47.79 65.60 1230693
## 28 40.59  2846.41  99091  0.00  46 51.50   0  608868 49.46 65.24 1055174
## 29 11.39   835.48 398478 11.62  94 22.88   4 1021759 79.94 70.45 1819277
##         x11     x12   x13   x14    x15
## 1  16026.18 3264222 22.89  1.67   5.24
## 2   8255.38 4309795 81.71  3.23  38.34
## 3  15974.73 3651343 16.61  4.03  14.89
## 4  11178.38 3559535 36.75  2.68  15.22
## 5   4150.23 3314294 27.04  3.11  54.96
## 6   5223.67 3195686 38.02  0.60  51.75
## 7   4283.39 3881394 65.98  3.69  44.17
## 8   1387.23 4036841 46.07  8.30  34.41
## 9  63716.34 5926620 31.75  5.54  14.42
## 10  4820.54 3053268 13.86  1.43   3.62
## 11  2991.71 3017944 26.91  2.86  12.91
## 12  2569.01 5137378 25.12  3.70   7.02
## 13  2504.26 5825556 71.76  7.87  20.46
## 14  1968.75 4967729 23.03  1.76  13.94
## 15  1672.32 4493501 44.88  7.55  21.59
## 16  2937.45 3443388  5.70  2.33   1.53
## 17  2925.04 3527627  9.42  2.42   2.56
## 18  2044.75 3601837  9.44  3.26   1.06
## 19  1042.05 2684956  7.78  3.57  33.24
## 20  1722.34 3112124  6.98  7.11   1.53
## 21  1269.94 4015607 36.54  3.03  83.56
## 22  1930.04 3095693 68.62  2.83 156.74
## 23  1213.55 4178269 17.72  2.52  22.55
## 24  1283.66 2875730 20.84  7.22  47.04
## 25  1438.05 3169255 42.43  2.10  14.24
## 26  1355.63 5566846 28.31  3.27  27.42
## 27  1273.03 2613058 20.46 12.80  34.43
## 28  1435.66 3909419 30.98  4.78 184.39
## 29 32019.13 4123268 33.80  4.50 425.76
#deteksi pencilan
s = sqrt(21.29)
n = dim(data)[1]
p = length(modelreg$coefficients)
hii=hatvalues(modelreg)
Obs = c(1:n)
ei = modelreg$residuals
ri = ei/(s*sqrt(1-hii))
summ <- cbind.data.frame(Obs, data, hii, ri)
summ
##    Obs     y       x1     x2    x3  x4    x5  x6      x7    x8    x9     x10
## 1    1 10.16 45013.35 230932  3.43 260 29.67 503  366296 70.09 67.00 1619834
## 2    2 37.09  2629.01 269553  2.51 153  9.62 481  461058 58.03 59.64 1459370
## 3    3 12.13 14082.21 166171 10.33 174 29.95 302  601005 71.69 67.05 1210925
## 4    4 23.83 11806.09 169136  6.65 141 21.57 271  612872 68.83 68.06 1606658
## 5    5 26.09  2429.03 112676  5.30 152 41.20 122  653819 67.66 69.12 1071327
## 6    6 24.45  2339.78 134650 10.38 221 35.56 435  574402 72.19 68.25 1180692
## 7    7 36.59  5306.87 220410  0.00 110 61.31  18  512058 56.31 66.44  901010
## 8    8 36.00  5986.19 224527  1.50  38 31.86   2  639503 48.37 65.15 2037148
## 9    9 14.17 18298.95 311969  7.80 152 13.52 647  870355 74.19 72.32 1536464
## 10  10 19.90 23558.27  64285  8.09 112 36.22 127  486179 61.53 59.97 1176631
## 11  11 26.09 24262.23 108295  5.77 172 23.58 127  351801 58.15 65.11  701155
## 12  12 24.83 25015.31 110105  2.38 149 91.81  23  383790 50.55 58.05  814912
## 13  13 37.64 16365.94 350880  3.88 187 30.59  26  428433 49.37 65.93 1077844
## 14  14 30.46 13751.92  77872  4.12  99 68.47  12  580393 45.44 64.44 1266742
## 15  15 32.60  2990.01 236986  1.07  86 21.35  51  404812 49.50 65.71 1177982
## 16  16 13.84 14068.37  41515  4.83  87 32.01  42  535586 63.63 66.36 1073259
## 17  17 16.00  9526.32  61623  2.56 102 22.71 175  677050 66.40 66.69  798421
## 18  18 29.85 10778.76  33943  4.76  66 41.77  68  691544 64.94 66.33 1578129
## 19  19 37.91   660.61  22547  4.12  58 15.06  82  471401 62.30 65.94 1099147
## 20  20 28.78 28042.39  36483  2.55  74 92.60  13  727830 51.78 57.77  544272
## 21  21 37.18  5886.89 106533  0.00  38  6.69   0  360779 31.55 55.27 1462880
## 22  22 38.73  2339.87 196399  0.00  82  6.37   6  490508 47.86 66.06 1505891
## 23  23 36.76  4101.50  50685  0.00  46  2.40  75  412574 47.57 63.59 1096064
## 24  24 33.25  3148.29 101973  0.00  69 54.25   0  347171 48.34 65.42 1683573
## 25  25 36.26  7701.03 114741  0.00  55 31.65   0  664865 43.04 65.74 1007631
## 26  26 28.81  3792.93 116206  0.00  69 21.17   0  512654 54.84 65.73  485881
## 27  27 41.66  5334.45 135043  1.22  31 61.99   0  662465 47.79 65.60 1230693
## 28  28 40.59  2846.41  99091  0.00  46 51.50   0  608868 49.46 65.24 1055174
## 29  29 11.39   835.48 398478 11.62  94 22.88   4 1021759 79.94 70.45 1819277
##         x11     x12   x13   x14    x15       hii          ri
## 1  16026.18 3264222 22.89  1.67   5.24 0.9235368  1.63994541
## 2   8255.38 4309795 81.71  3.23  38.34 0.7289329 -0.09379946
## 3  15974.73 3651343 16.61  4.03  14.89 0.2572984 -0.96671410
## 4  11178.38 3559535 36.75  2.68  15.22 0.1985838 -0.03424250
## 5   4150.23 3314294 27.04  3.11  54.96 0.5340996  0.53344888
## 6   5223.67 3195686 38.02  0.60  51.75 0.6351222 -0.13085041
## 7   4283.39 3881394 65.98  3.69  44.17 0.6622514  0.75610975
## 8   1387.23 4036841 46.07  8.30  34.41 0.4718231 -0.47568672
## 9  63716.34 5926620 31.75  5.54  14.42 0.9683480  0.03853330
## 10  4820.54 3053268 13.86  1.43   3.62 0.4635449 -0.07634986
## 11  2991.71 3017944 26.91  2.86  12.91 0.4902806 -0.04352685
## 12  2569.01 5137378 25.12  3.70   7.02 0.5595174 -0.11934429
## 13  2504.26 5825556 71.76  7.87  20.46 0.7003607  1.38367875
## 14  1968.75 4967729 23.03  1.76  13.94 0.6029280  0.11745027
## 15  1672.32 4493501 44.88  7.55  21.59 0.3741765 -0.55595861
## 16  2937.45 3443388  5.70  2.33   1.53 0.1993608 -1.96474269
## 17  2925.04 3527627  9.42  2.42   2.56 0.4271202 -1.21236425
## 18  2044.75 3601837  9.44  3.26   1.06 0.4533809  1.16869338
## 19  1042.05 2684956  7.78  3.57  33.24 0.2994890  2.32480333
## 20  1722.34 3112124  6.98  7.11   1.53 0.6354963  0.25462704
## 21  1269.94 4015607 36.54  3.03  83.56 0.6914667 -0.12344408
## 22  1930.04 3095693 68.62  2.83 156.74 0.5668155 -1.43851639
## 23  1213.55 4178269 17.72  2.52  22.55 0.2798056  1.05364438
## 24  1283.66 2875730 20.84  7.22  47.04 0.5787016 -0.78699111
## 25  1438.05 3169255 42.43  2.10  14.24 0.6589532  0.73036278
## 26  1355.63 5566846 28.31  3.27  27.42 0.4743302 -0.50137743
## 27  1273.03 2613058 20.46 12.80  34.43 0.5842732  0.03646352
## 28  1435.66 3909419 30.98  4.78 184.39 0.6306886 -0.26093304
## 29 32019.13 4123268 33.80  4.50 425.76 0.9493138  1.17353825
#pencilan jika rii lebih dari 2 ada di amatan ke 19

deteksi titik leverannce

for (i in 1:dim(summ)[1]){
  cutoff <- 2*p/n
  titik_leverage <- which(hii > cutoff)
}
titik_leverage
## named integer(0)