UAS Nomor 1

Tujuan

Melakukan simulasi pemodelan regresi linier berganda untuk memprediksi harga rumah berdasarkan:

  1. Luas rumah (X1)

  2. Jumlah kamar tidur (X2)

  3. Lokasi (X3) dengan kategori: cluster, town house, dan residence Semua variabel disimulasikan dari distribusi normal sesuai dengan ketentuan soal.

Pembangkitan Data

set.seed(123)

Set.seed digunakan untuk memastikan bahwa hasil simulasi bersifat tetap (reproducible), yang jika dijalankan ulang akan menghasilkan data yang sama.

Jumlah data
n <- 200
n
## [1] 200

Menentukan jumlah data rumah yang disimulasikan, yaitu sebanyak 200 data.

X1 = Luas rumah
X1 <- rnorm(n, mean = 100, sd = 20)
X1
##   [1]  88.79049  95.39645 131.17417 101.41017 102.58575 134.30130 109.21832
##   [8]  74.69878  86.26294  91.08676 124.48164 107.19628 108.01543 102.21365
##  [15]  88.88318 135.73826 109.95701  60.66766 114.02712  90.54417  78.64353
##  [22]  95.64050  79.47991  85.42218  87.49921  66.26613 116.75574 103.06746
##  [29]  77.23726 125.07630 108.52928  94.09857 117.90251 117.56267 116.43162
##  [36] 113.77281 111.07835  98.76177  93.88075  92.39058  86.10586  95.84165
##  [43]  74.69207 143.37912 124.15924  77.53783  91.94230  90.66689 115.59930
##  [50]  98.33262 105.06637  99.42906  99.14259 127.37205  95.48458 130.32941
##  [57]  69.02494 111.69227 102.47708 104.31883 107.59279  89.95353  93.33585
##  [64]  79.62849  78.56418 106.07057 108.96420 101.06008 118.44535 141.00169
##  [71]  90.17938  53.81662 120.11477  85.81598  86.23983 120.51143  94.30454
##  [78]  75.58565 103.62607  97.22217 100.11528 107.70561  92.58680 112.88753
##  [85]  95.59027 106.63564 121.93678 108.70363  93.48137 122.97615 119.87008
##  [92] 110.96794 104.77463  87.44188 127.21305  87.99481 143.74666 130.65221
##  [99]  95.28599  79.47158  85.79187 105.13767  95.06616  93.04915  80.96763
## [106]  99.09945  84.30191  66.64116  92.39547 118.37993  88.49306 112.15929
## [113]  67.64235  98.88876 110.38814 106.02307 102.11352  87.18588  83.00591
## [120]  79.51742 102.35293  81.05051  90.18885  94.87816 136.87724  86.96100
## [127] 104.70773 101.55922  80.76287  98.57384 128.89102 109.03008 100.82466
## [134]  91.55006  58.93506 122.62674  70.78720 114.79895 138.18207  71.12214
## [141] 114.03569  94.75605  68.55712  69.70665  67.96928  89.38187  70.76489
## [148] 113.75834 142.00218  74.25939 115.75478 115.38084 106.64405  79.83247
## [155]  97.61095  94.39209 111.25979  92.55122 119.53947  92.50838 121.05423
## [162]  79.01646  74.79690 164.82080  91.66285 105.96455 112.73139  90.32439
## [169] 110.33724 107.37929  95.69239 101.30586  99.31865 142.56904  85.17328
## [176]  78.08007 100.75577 106.20961 108.73047  90.83269  78.73348 125.26370
## [183]  93.00699  82.68974  95.27441  96.05648 122.19841 101.69475 115.08108
## [190]  90.01416 104.28891  93.50628 101.89167  82.09273  73.78397 139.94427
## [197] 112.01418  74.97457  87.77668  76.29040

Simulasi luas rumah dalam meter persegi menggunakan distribusi normal dengan rata-rata 100 dan standar deviasi 20.

X2 = Jumlah kamar tidur
X2 <- round(rnorm(n, mean = 3, sd = 1))
X2
##   [1] 5 4 3 4 3 3 2 2 5 3 3 3 4 2 2 5 3 2 2 2 2 4 4 4 3 3 2 2 4 2 5 3 3 2 2 2 3
##  [38] 3 3 2 2 2 4 2 3 5 3 2 2 3 3 2 3 3 5 3 4 4 3 1 2 3 3 4 5 5 3 1 3 3 4 4 4 2
##  [75] 4 3 3 3 3 3 1 4 3 3 3 3 3 5 3 3 4 4 4 2 5 3 5 2 3 4 2 2 2 2 3 3 1 3 4 5 4
## [112] 4 1 2 3 4 3 2 5 4 3 4 2 4 2 4 3 4 4 3 4 2 2 5 3 1 2 3 4 3 4 4 5 3 3 1 3 2
## [149] 2 3 4 1 3 3 2 3 3 3 1 6 3 4 3 4 4 3 3 2 4 3 5 1 3 4 4 2 2 3 3 1 3 3 3 2 3
## [186] 4 3 2 3 3 2 1 4 2 2 5 2 4 2 3

Simulasi jumlah kamar tidur, dibulatkan karena kamar tidak bisa pecahan.

Lokasi
X3 <- factor(sample(c("cluster", "town house", "residence"), size = n, replace = TRUE))
X3
##   [1] residence  cluster    residence  cluster    residence  residence 
##   [7] cluster    residence  residence  residence  residence  residence 
##  [13] town house residence  town house residence  town house cluster   
##  [19] cluster    residence  cluster    town house cluster    town house
##  [25] town house cluster    cluster    town house residence  cluster   
##  [31] town house cluster    town house residence  town house town house
##  [37] residence  cluster    town house residence  residence  cluster   
##  [43] residence  cluster    cluster    residence  cluster    cluster   
##  [49] residence  cluster    cluster    cluster    town house town house
##  [55] town house town house residence  residence  town house cluster   
##  [61] residence  cluster    residence  residence  residence  cluster   
##  [67] town house residence  residence  cluster    cluster    cluster   
##  [73] residence  residence  cluster    cluster    town house residence 
##  [79] residence  town house cluster    residence  cluster    cluster   
##  [85] residence  town house residence  residence  residence  town house
##  [91] cluster    residence  residence  town house town house town house
##  [97] residence  residence  town house cluster    cluster    cluster   
## [103] town house cluster    town house town house residence  residence 
## [109] residence  residence  town house town house town house residence 
## [115] residence  residence  cluster    residence  cluster    residence 
## [121] town house cluster    cluster    cluster    residence  cluster   
## [127] town house cluster    residence  residence  residence  town house
## [133] residence  town house cluster    residence  town house town house
## [139] cluster    town house cluster    town house residence  residence 
## [145] town house town house cluster    town house residence  town house
## [151] residence  cluster    town house residence  cluster    town house
## [157] residence  town house cluster    residence  residence  residence 
## [163] cluster    residence  town house town house town house residence 
## [169] cluster    cluster    town house town house residence  town house
## [175] town house cluster    residence  cluster    residence  residence 
## [181] residence  residence  town house town house cluster    cluster   
## [187] residence  town house cluster    town house town house town house
## [193] cluster    residence  residence  town house town house town house
## [199] cluster    residence 
## Levels: cluster residence town house

Lokasi disimulasikan secara acak dari tiga kategori menggunakan distribusi uniform (masing-masing punya peluang yang sama).

Koefisien simulasi
beta0 <- 50000
beta1 <- 3000
beta2 <- 10000
beta3 <- c("cluster" = 0, "town house" = 15000, "residence" = 30000)  # efek lokasi terhadap harga

Koefisien yang menggambarkan pengaruh masing-masing variabel terhadap harga rumah. Lokasi memiliki efek tetap sesuai kategorinya.

Bangkitkan harga rumah
Y <- beta0 + beta1 * X1 + beta2 * X2 + beta3[X3] + rnorm(n, mean = 0, sd = 20000)
Y
## town house    cluster town house    cluster town house town house    cluster 
##   384490.4   396825.1   505389.0   400810.8   365123.7   490490.1   378724.2 
## town house town house town house town house town house  residence town house 
##   304728.1   361646.7   330447.6   467448.2   388353.0   421463.2   405581.0 
##  residence town house  residence    cluster    cluster town house    cluster 
##   372998.7   505376.1   451992.2   265132.3   413556.9   387336.5   314208.9 
##  residence    cluster  residence  residence    cluster    cluster  residence 
##   425716.9   322917.4   362416.3   403224.1   282253.0   402915.1   399484.3 
## town house    cluster  residence    cluster  residence town house  residence 
##   305281.3   448671.8   458850.6   380578.8   467120.4   451332.0   438110.7 
##  residence town house    cluster  residence town house town house    cluster 
##   428429.7   423445.3   409934.2   386866.0   368724.4   321612.9   362546.2 
## town house    cluster    cluster town house    cluster    cluster town house 
##   357037.7   493605.0   473584.9   331484.9   339515.0   327971.1   402645.0 
##    cluster    cluster    cluster  residence  residence  residence  residence 
##   363752.0   433555.2   363314.1   435040.6   494528.3   411244.1   507885.2 
## town house town house  residence    cluster town house    cluster town house 
##   295982.7   462247.9   395369.7   388979.1   412549.4   352630.6   389533.2 
## town house town house    cluster  residence town house town house    cluster 
##   326705.6   326140.8   412374.6   461147.3   389723.9   495944.6   482922.9 
##    cluster    cluster town house town house    cluster    cluster  residence 
##   362657.1   312592.4   465739.2   360277.9   365708.4   473752.4   394019.6 
## town house town house  residence    cluster town house    cluster    cluster 
##   345864.9   370952.5   386629.1   361898.3   416314.7   362244.8   384350.5 
## town house  residence town house town house town house  residence    cluster 
##   392475.4   420056.1   451553.8   438103.8   363744.7   444398.3   461690.7 
## town house town house  residence  residence  residence town house town house 
##   438441.4   358384.6   384476.2   497805.6   377898.6   552085.1   455918.7 
##  residence    cluster    cluster    cluster  residence    cluster  residence 
##   365664.1   359069.1   365849.5   397381.9   388531.7   351891.4   359177.1 
##  residence town house town house town house town house  residence  residence 
##   401826.4   348874.4   308799.6   370285.0   472616.6   386229.6   457795.9 
##  residence town house town house town house    cluster town house    cluster 
##   313494.1   371369.1   438630.0   426548.3   385645.0   366174.1   351789.6 
## town house  residence    cluster    cluster    cluster town house    cluster 
##   321628.9   391384.6   326301.1   326238.6   349847.6   482461.5   314356.7 
##  residence    cluster town house town house town house  residence town house 
##   415751.1   387353.6   341408.6   390259.1   498921.0   417756.0   393444.2 
##  residence    cluster town house  residence  residence    cluster  residence 
##   385596.0   270156.0   444625.0   310177.4   440847.0   538522.7   331083.0 
##    cluster  residence town house town house  residence  residence    cluster 
##   437042.2   352529.5   281409.3   302893.8   331812.9   357584.4   272299.7 
##  residence town house  residence town house    cluster  residence town house 
##   438082.9   500452.7   337101.3   433885.2   388130.6   435704.3   353075.1 
##    cluster  residence town house  residence    cluster town house town house 
##   396293.8   398886.6   421327.7   358976.0   400929.0   425008.8   443706.6 
## town house    cluster town house  residence  residence  residence town house 
##   351302.7   298139.6   616427.5   393282.2   413586.8   433460.3   355828.7 
##    cluster    cluster  residence  residence town house  residence  residence 
##   435021.3   434665.7   449570.4   422291.3   373146.1   559974.5   365743.3 
##    cluster town house    cluster town house town house town house town house 
##   295788.5   391332.2   428533.4   395485.7   390428.3   327947.5   460323.5 
##  residence  residence    cluster    cluster town house  residence    cluster 
##   405889.9   342043.1   387580.1   378988.1   467325.9   405002.0   438979.5 
##  residence  residence  residence    cluster town house town house  residence 
##   377148.9   400625.2   383130.2   371821.0   347977.5   318298.5   526216.5 
##  residence  residence    cluster town house 
##   449392.0   327359.7   355279.5   302050.4

Harga rumah dihitung berdasarkan model linear dengan tambahan error acak dari distribusi normal untuk mensimulasikan variasi alami.

Data frame
data_rumah <- data.frame(Harga = Y, Luas = X1, Kamar = X2, Lokasi = X3)
data_rumah
##        Harga      Luas Kamar     Lokasi
## 1   384490.4  88.79049     5  residence
## 2   396825.1  95.39645     4    cluster
## 3   505389.0 131.17417     3  residence
## 4   400810.8 101.41017     4    cluster
## 5   365123.7 102.58575     3  residence
## 6   490490.1 134.30130     3  residence
## 7   378724.2 109.21832     2    cluster
## 8   304728.1  74.69878     2  residence
## 9   361646.7  86.26294     5  residence
## 10  330447.6  91.08676     3  residence
## 11  467448.2 124.48164     3  residence
## 12  388353.0 107.19628     3  residence
## 13  421463.2 108.01543     4 town house
## 14  405581.0 102.21365     2  residence
## 15  372998.7  88.88318     2 town house
## 16  505376.1 135.73826     5  residence
## 17  451992.2 109.95701     3 town house
## 18  265132.3  60.66766     2    cluster
## 19  413556.9 114.02712     2    cluster
## 20  387336.5  90.54417     2  residence
## 21  314208.9  78.64353     2    cluster
## 22  425716.9  95.64050     4 town house
## 23  322917.4  79.47991     4    cluster
## 24  362416.3  85.42218     4 town house
## 25  403224.1  87.49921     3 town house
## 26  282253.0  66.26613     3    cluster
## 27  402915.1 116.75574     2    cluster
## 28  399484.3 103.06746     2 town house
## 29  305281.3  77.23726     4  residence
## 30  448671.8 125.07630     2    cluster
## 31  458850.6 108.52928     5 town house
## 32  380578.8  94.09857     3    cluster
## 33  467120.4 117.90251     3 town house
## 34  451332.0 117.56267     2  residence
## 35  438110.7 116.43162     2 town house
## 36  428429.7 113.77281     2 town house
## 37  423445.3 111.07835     3  residence
## 38  409934.2  98.76177     3    cluster
## 39  386866.0  93.88075     3 town house
## 40  368724.4  92.39058     2  residence
## 41  321612.9  86.10586     2  residence
## 42  362546.2  95.84165     2    cluster
## 43  357037.7  74.69207     4  residence
## 44  493605.0 143.37912     2    cluster
## 45  473584.9 124.15924     3    cluster
## 46  331484.9  77.53783     5  residence
## 47  339515.0  91.94230     3    cluster
## 48  327971.1  90.66689     2    cluster
## 49  402645.0 115.59930     2  residence
## 50  363752.0  98.33262     3    cluster
## 51  433555.2 105.06637     3    cluster
## 52  363314.1  99.42906     2    cluster
## 53  435040.6  99.14259     3 town house
## 54  494528.3 127.37205     3 town house
## 55  411244.1  95.48458     5 town house
## 56  507885.2 130.32941     3 town house
## 57  295982.7  69.02494     4  residence
## 58  462247.9 111.69227     4  residence
## 59  395369.7 102.47708     3 town house
## 60  388979.1 104.31883     1    cluster
## 61  412549.4 107.59279     2  residence
## 62  352630.6  89.95353     3    cluster
## 63  389533.2  93.33585     3  residence
## 64  326705.6  79.62849     4  residence
## 65  326140.8  78.56418     5  residence
## 66  412374.6 106.07057     5    cluster
## 67  461147.3 108.96420     3 town house
## 68  389723.9 101.06008     1  residence
## 69  495944.6 118.44535     3  residence
## 70  482922.9 141.00169     3    cluster
## 71  362657.1  90.17938     4    cluster
## 72  312592.4  53.81662     4    cluster
## 73  465739.2 120.11477     4  residence
## 74  360277.9  85.81598     2  residence
## 75  365708.4  86.23983     4    cluster
## 76  473752.4 120.51143     3    cluster
## 77  394019.6  94.30454     3 town house
## 78  345864.9  75.58565     3  residence
## 79  370952.5 103.62607     3  residence
## 80  386629.1  97.22217     3 town house
## 81  361898.3 100.11528     1    cluster
## 82  416314.7 107.70561     4  residence
## 83  362244.8  92.58680     3    cluster
## 84  384350.5 112.88753     3    cluster
## 85  392475.4  95.59027     3  residence
## 86  420056.1 106.63564     3 town house
## 87  451553.8 121.93678     3  residence
## 88  438103.8 108.70363     5  residence
## 89  363744.7  93.48137     3  residence
## 90  444398.3 122.97615     3 town house
## 91  461690.7 119.87008     4    cluster
## 92  438441.4 110.96794     4  residence
## 93  358384.6 104.77463     4  residence
## 94  384476.2  87.44188     2 town house
## 95  497805.6 127.21305     5 town house
## 96  377898.6  87.99481     3 town house
## 97  552085.1 143.74666     5  residence
## 98  455918.7 130.65221     2  residence
## 99  365664.1  95.28599     3 town house
## 100 359069.1  79.47158     4    cluster
## 101 365849.5  85.79187     2    cluster
## 102 397381.9 105.13767     2    cluster
## 103 388531.7  95.06616     2 town house
## 104 351891.4  93.04915     2    cluster
## 105 359177.1  80.96763     3 town house
## 106 401826.4  99.09945     3 town house
## 107 348874.4  84.30191     1  residence
## 108 308799.6  66.64116     3  residence
## 109 370285.0  92.39547     4  residence
## 110 472616.6 118.37993     5  residence
## 111 386229.6  88.49306     4 town house
## 112 457795.9 112.15929     4 town house
## 113 313494.1  67.64235     1 town house
## 114 371369.1  98.88876     2  residence
## 115 438630.0 110.38814     3  residence
## 116 426548.3 106.02307     4  residence
## 117 385645.0 102.11352     3    cluster
## 118 366174.1  87.18588     2  residence
## 119 351789.6  83.00591     5    cluster
## 120 321628.9  79.51742     4  residence
## 121 391384.6 102.35293     3 town house
## 122 326301.1  81.05051     4    cluster
## 123 326238.6  90.18885     2    cluster
## 124 349847.6  94.87816     4    cluster
## 125 482461.5 136.87724     2  residence
## 126 314356.7  86.96100     4    cluster
## 127 415751.1 104.70773     3 town house
## 128 387353.6 101.55922     4    cluster
## 129 341408.6  80.76287     4  residence
## 130 390259.1  98.57384     3  residence
## 131 498921.0 128.89102     4  residence
## 132 417756.0 109.03008     2 town house
## 133 393444.2 100.82466     2  residence
## 134 385596.0  91.55006     5 town house
## 135 270156.0  58.93506     3    cluster
## 136 444625.0 122.62674     1  residence
## 137 310177.4  70.78720     2 town house
## 138 440847.0 114.79895     3 town house
## 139 538522.7 138.18207     4    cluster
## 140 331083.0  71.12214     3 town house
## 141 437042.2 114.03569     4    cluster
## 142 352529.5  94.75605     4 town house
## 143 281409.3  68.55712     5  residence
## 144 302893.8  69.70665     3  residence
## 145 331812.9  67.96928     3 town house
## 146 357584.4  89.38187     1 town house
## 147 272299.7  70.76489     3    cluster
## 148 438082.9 113.75834     2 town house
## 149 500452.7 142.00218     2  residence
## 150 337101.3  74.25939     3 town house
## 151 433885.2 115.75478     4  residence
## 152 388130.6 115.38084     1    cluster
## 153 435704.3 106.64405     3 town house
## 154 353075.1  79.83247     3  residence
## 155 396293.8  97.61095     2    cluster
## 156 398886.6  94.39209     3 town house
## 157 421327.7 111.25979     3  residence
## 158 358976.0  92.55122     3 town house
## 159 400929.0 119.53947     1    cluster
## 160 425008.8  92.50838     6  residence
## 161 443706.6 121.05423     3  residence
## 162 351302.7  79.01646     4  residence
## 163 298139.6  74.79690     3    cluster
## 164 616427.5 164.82080     4  residence
## 165 393282.2  91.66285     4 town house
## 166 413586.8 105.96455     3 town house
## 167 433460.3 112.73139     3 town house
## 168 355828.7  90.32439     2  residence
## 169 435021.3 110.33724     4    cluster
## 170 434665.7 107.37929     3    cluster
## 171 449570.4  95.69239     5 town house
## 172 422291.3 101.30586     1 town house
## 173 373146.1  99.31865     3  residence
## 174 559974.5 142.56904     4 town house
## 175 365743.3  85.17328     4 town house
## 176 295788.5  78.08007     2    cluster
## 177 391332.2 100.75577     2  residence
## 178 428533.4 106.20961     3    cluster
## 179 395485.7 108.73047     3  residence
## 180 390428.3  90.83269     1  residence
## 181 327947.5  78.73348     3  residence
## 182 460323.5 125.26370     3  residence
## 183 405889.9  93.00699     3 town house
## 184 342043.1  82.68974     2 town house
## 185 387580.1  95.27441     3    cluster
## 186 378988.1  96.05648     4    cluster
## 187 467325.9 122.19841     3  residence
## 188 405002.0 101.69475     2 town house
## 189 438979.5 115.08108     3    cluster
## 190 377148.9  90.01416     3 town house
## 191 400625.2 104.28891     2 town house
## 192 383130.2  93.50628     1 town house
## 193 371821.0 101.89167     4    cluster
## 194 347977.5  82.09273     2  residence
## 195 318298.5  73.78397     2  residence
## 196 526216.5 139.94427     5 town house
## 197 449392.0 112.01418     2 town house
## 198 327359.7  74.97457     4 town house
## 199 355279.5  87.77668     2    cluster
## 200 302050.4  76.29040     3  residence

Menggabungkan seluruh variabel menjadi satu tabel agar mudah dianalisis.

Menampilkan sebagian data
head(data_rumah)
##      Harga      Luas Kamar    Lokasi
## 1 384490.4  88.79049     5 residence
## 2 396825.1  95.39645     4   cluster
## 3 505389.0 131.17417     3 residence
## 4 400810.8 101.41017     4   cluster
## 5 365123.7 102.58575     3 residence
## 6 490490.1 134.30130     3 residence

Visualisasi

Scatterplot luas rumah vs harga
plot(data_rumah$Luas, data_rumah$Harga,
     main = "Scatterplot Luas vs Harga Rumah",
     xlab = "Luas Rumah (m2)",
     ylab = "Harga Rumah",
     pch = 16, col = "darkgreen")

Scatterplot untuk melihat hubungan antara luas rumah dan harga rumah secara visual.

Model Regresi Linear Berganda

model_rumah <- lm(Harga ~ Luas + Kamar + Lokasi, data = data_rumah)
summary(model_rumah)
## 
## Call:
## lm(formula = Harga ~ Luas + Kamar + Lokasi, data = data_rumah)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -56052 -11127  -1056  12919  55462 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      70025.71    8230.88   8.508 4.65e-15 ***
## Luas              2920.53      70.56  41.393  < 2e-16 ***
## Kamar             7482.90    1274.52   5.871 1.84e-08 ***
## Lokasiresidence   8481.88    3243.99   2.615  0.00963 ** 
## Lokasitown house 23522.27    3383.15   6.953 5.26e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 18740 on 195 degrees of freedom
## Multiple R-squared:  0.9033, Adjusted R-squared:  0.9013 
## F-statistic: 455.4 on 4 and 195 DF,  p-value: < 2.2e-16

Membangun model regresi linear berganda untuk memprediksi harga berdasarkan semua variabel input. Hasil summary menunjukkan signifikansi dan kontribusi masing-masing variabel.

Plot Prediksi vs Harga Aktual

plot(data_rumah$Harga, fitted(model_rumah),
     main = "Harga Aktual vs Prediksi",
     xlab = "Harga Aktual",
     ylab = "Harga Prediksi",
     pch = 19, col = "blue")
abline(a = 0, b = 1, col = "red", lwd = 2)

Visualisasi untuk membandingkan seberapa dekat prediksi model terhadap harga aktual. Garis merah y = x membantu melihat kesesuaian.

Kesimpulan

Model regresi menunjukkan bahwa:

  1. Semakin luas rumah dan semakin banyak kamar tidur, harga semakin tinggi.

  2. Lokasi town house dan residence memberikan peningkatan harga dibanding cluster.

  3. Model dapat digunakan untuk memperkirakan harga rumah berdasarkan kriteria simulasi.

UAS Nomor 2

Tujuan

Mensimulasikan sistem antrian dengan distribusi kedatangan pelanggan mengikuti Poisson (λ = 50 pelanggan per jam), serta menganalisis ukuran kinerja sistem antrian pada skenario jumlah pelayan yang berbeda (1 dan 2 pelayan)

Pembangkitan Data Poisson

set.seed(123)
Parameter Poisson
lambda <- 50
pelanggan <- rpois(100, lambda = lambda)

Data jumlah pelanggan perjam diasumsikan menggunakan distribusi poisson dengan rata-rata 50 pelanggan perjam. Sebanyak 100 observasi dibuat untuk mewakili data simulasi selama 100 jam.

Menampilkan ringkasan data
summary(pelanggan)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   37.00   46.00   49.00   50.11   53.00   71.00

Visualisasi Distribusi Jumlah Pelanggan

hist(pelanggan, col = "lightblue", breaks = 15,
     main = "Distribusi Jumlah Pelanggan per Jam",
     xlab = "Jumlah Pelanggan")

Histogram menunjukkan sebaran jumlah pelanggan yang datang perjam. Bentuk distribusi poisson akan terlihat dari frekuensi kedatangan per nilai pelanggan.

a. Sistem Antrian M/M/1

mu <- 60  
rho1 <- lambda / mu

L1 <- rho1 / (1 - rho1)         
Lq1 <- rho1^2 / (1 - rho1)      
W1 <- 1 / (mu - lambda)       
Wq1 <- lambda / (mu * (mu - lambda)) 

cat("== M/M/1 ==\n")
## == M/M/1 ==
cat("Utilisasi:", round(rho1, 3), "\n")
## Utilisasi: 0.833
cat("L :", round(L1, 2), "\n")
## L : 5
cat("Lq:", round(Lq1, 2), "\n")
## Lq: 4.17
cat("W :", round(W1, 2), "jam\n")
## W : 0.1 jam
cat("Wq:", round(Wq1, 2), "jam\n")
## Wq: 0.08 jam

Sistem M/M/1 memiliki satu pelayan dengan tingkat layanan rata-rata μ = 60. Dihitung beberapa ukuran performa antrian:

ρ (rho): tingkat utilisasi pelayan

L: rata-rata jumlah pelanggan dalam sistem

Lq: rata-rata jumlah pelanggan dalam antrian

W: rata-rata waktu dalam sistem

Wq: rata-rata waktu tunggu dalam antrian

Hasil cat() menampilkan ringkasan semua metrik kinerja sistem M/M/1.

b. Sistem Antrian M/M/2

c <- 2  
rho2 <- lambda / (c * mu)  

fact <- function(x) ifelse(x == 0, 1, prod(1:x))

sum_terms <- sum(sapply(0:(c-1), function(k) (lambda/mu)^k / fact(k)))
last_term <- ((lambda/mu)^c / (fact(c) * (1 - rho2)))
P0 <- 1 / (sum_terms + last_term)

Pw <- last_term * P0

Lq2 <- Pw * (lambda * mu) / ((c * mu - lambda)^2)
L2 <- Lq2 + lambda / mu
Wq2 <- Lq2 / lambda
W2 <- Wq2 + 1 / mu

cat("== M/M/2 ==\n")
## == M/M/2 ==
cat("Utilisasi per pelayan:", round(rho2, 3), "\n")
## Utilisasi per pelayan: 0.417
cat("P(w) =", round(Pw, 3), "\n")
## P(w) = 0.245
cat("L :", round(L2, 2), "\n")
## L : 0.98
cat("Lq:", round(Lq2, 2), "\n")
## Lq: 0.15
cat("W :", round(W2, 2), "jam\n")
## W : 0.02 jam
cat("Wq:", round(Wq2, 2), "jam\n")
## Wq: 0 jam

Untuk sistem M/M/2, digunakan dua pelayan. Perhitungannya sedikit lebih kompleks karena melibatkan:

P₀: probabilitas sistem kosong

P(w): probabilitas pelanggan harus menunggu

Lq: jumlah rata-rata pelanggan dalam antrian

L: jumlah rata-rata pelanggan dalam sistem

Wq: waktu tunggu rata-rata dalam antrian

W: total waktu rata-rata dalam sistem

Perhitungan ini menggunakan rumus dari teori antrian multi-server (c > 1), termasuk fungsi faktorial untuk menghitung pembagi.

c. Probabilitas Jumlah Pelanggan > 60

p_gt_60 <- 1 - ppois(60, lambda = 50)
cat("P(X > 60) =", round(p_gt_60, 4),
    "atau", round(p_gt_60 * 100, 2), "%")
## P(X > 60) = 0.0722 atau 7.22 %

Kesimpulan

  1. Dengan 1 pelayan, sistem memiliki utilisasi tinggi dan waktu tunggu lebih lama.

  2. Dengan 2 pelayan, sistem menjadi lebih stabil dan waktu tunggu menurun.

  3. Probabilitas lebih dari 60 pelanggan per jam sekitar r round(p_gt_60, 4) atau r round(p_gt_60*100, 2)%.

UAS Nomor 3

Tujuan

Membangun model regresi linear berganda pada data time series dengan karakteristik:

  1. Variabel dependen memiliki tren dan musiman

  2. Variabel independen 1 (X1) mengandung tren

  3. Variabel independen 2 (X2) mengandung musiman

Pembangkitan Data

library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)
set.seed(123)
Bangkitkan data 10 tahun (120 bulan)
t <- 1:120

Membuat vektor waktu dari bulan ke-1 sampai ke-120 (10 tahun).

Variabel dependen Y
trend_Y <- 0.5 * t
season_Y <- 10 * sin(2 * pi * t / 12)
noise_Y <- rnorm(120, mean = 0, sd = 5)
Y <- trend_Y + season_Y + noise_Y
Y_ts <- ts(Y, frequency = 12)
Y_ts
##            Jan         Feb         Mar         Apr         May         Jun
## 1    2.6976218   8.5093666  19.2935416  11.0127960   8.1464387  11.5753249
## 2   13.5038573  16.2136676  14.7207943  25.5948197  15.9892524  -0.8330858
## 3   14.3748037  13.2267875  27.6889352  23.4271196  13.8093153  21.2690746
## 4   26.2695883  27.3506955  27.9701867  26.7578990  22.0264651  19.9604136
## 5   33.3998256  33.2434087  36.7665926  34.5175203  31.2856477  33.8430114
## 6   37.3981974  37.1486368  39.8339631  35.5673771  32.1410439  34.5176432
## 7   46.5286926  42.1142502  44.0599569  51.7881109  42.0761350  32.8964114
## 8   46.3975672  53.3191639  58.9841951  54.8361615  47.8703421  50.7440381
## 9   64.4366650  65.3233072  58.3214982  53.5281495  51.9479672  52.2844185
## 10  57.5988674  68.2552371  62.6232652  67.7000756  53.4105865  56.7221902
##            Jul         Aug         Sep         Oct         Nov         Dec
## 1    0.8045810 -10.9855602  -8.9342643  -5.8885639   6.6204090   7.7990691
## 2    8.0067795  -1.0242111  -4.8391185   1.2498714   1.3699778   8.3555439
## 3   12.6323211   5.8643885  10.9756283  12.7304134  16.6079054  21.4432013
## 4   10.1730182  24.1845258  18.5398100   8.7242030  16.4855758  21.6667232
## 5   21.3711451  26.9220990  10.7562360  23.2628147  25.1192712  31.0797078
## 6   30.7410489  25.6047671  29.1113373  36.5901694  28.0448442  24.4541556
## 7   35.4065174  30.6452891  30.5288209  34.2661480  34.6466998  45.2218827
## 8   45.4675193  40.0817308  37.6936587  35.2002156  49.3032622  44.9987021
## 9   45.2665406  41.6020330  37.7419072  44.1146073  44.5754777  45.6602903
## 10  55.0970360  50.8455128  49.0283810  47.1362159  50.2514783  54.8793560

trend_Y: tren linier meningkat seiring waktu.

season_Y: pola musiman dengan periode 12 bulan (setahun).

noise_Y: error acak dari distribusi normal.

Y: gabungan ketiganya (model aditif).

Y_ts: diubah ke bentuk time series dengan frekuensi tahunan (12 bulan).

Variabel independen X1
X1 <- 0.3 * t + rnorm(120, mean = 0, sd = 3)
X1_ts <- ts(X1, frequency = 12)
X1_ts
##           Jan        Feb        Mar        Apr        May        Jun        Jul
## 1   0.6529398 -2.2424238 -0.5716723  0.4317234  7.0315860 -0.1558497  2.8061597
## 2   4.0236988  2.9325095 -1.6597417  8.1940116  0.7180798  7.6198425 11.4273107
## 3   2.6953915  6.2072804  3.7147332 10.4637503 15.0003268  5.1389086 11.6632165
## 4  12.7889686 10.2826837 14.6309202 10.8762574 15.4581344  9.4524690  9.1195343
## 5  16.2505861 16.1068936 14.6538585 15.7958791 15.7977982 22.5853557 14.2759917
## 6  15.1100216 22.3895555 17.8510488 16.6034614 18.7911613 19.2084723 23.4297609
## 7  22.1837506 19.5139099 18.5675954 28.7916402 24.9021265 19.6461859 21.8665022
## 8  24.2569802 24.3712593 23.7341915 24.6161482 31.6527224 26.8379156 27.6577357
## 9  27.7765103 27.2308021 25.9911806 26.1458528 28.5780796 32.4539575 34.2295444
## 10 35.3539515 29.9532223 39.1658819 33.3290412 34.5436165 31.9844169 32.7768339
##           Aug        Sep        Oct        Nov        Dec
## 1   2.6338825 -0.1855699  2.7860757  7.6336526  4.9545122
## 2   1.6683205  8.4053530  5.8134075  2.1835675  2.6559970
## 3  11.9071267 10.8966077  7.1748702 10.1416422  9.9588140
## 4  22.9231198 12.2494272 14.6946828 16.0097090 12.9486581
## 5  13.5120112 17.2133652 18.3314422 19.0095704 16.6249040
## 6  20.6542119 22.9621614 19.5021239 21.9433359 20.6259423
## 7  20.4435597 30.8964310 28.5372389 24.1045648 26.8295822
## 8  28.3310623 31.5974276 26.6518085 25.5224785 33.8270908
## 9  33.3227651 30.4090281 31.9792498 29.9862106 30.2483455
## 10 30.8489516 34.5512238 36.6569472 36.6729130 33.6553905

Mengandung tren linier + noise, tanpa komponen musiman.

Variabel independen X2
X2 <- 8 * sin(2 * pi * t / 12) + rnorm(120, mean = 0, sd = 2)
X2_ts <- ts(X2, frequency = 12)
X2_ts
##              Jan           Feb           Mar           Apr           May
## 1    2.422756058   5.923805794  10.992121340   4.653595989   3.641896811
## 2    3.312165532   7.109196525  11.197017542   6.751073006   6.161598992
## 3    8.586157948  10.023365348   7.733698071   3.415148439   3.222440272
## 4    4.349605400   7.077305585   8.856333530   6.977553196   0.665049805
## 5    3.561899242   7.264333998  10.336767746   9.036565277   6.290526221
## 6    2.569515626   5.422825294   6.122922593   4.823176672   3.125680934
## 7    0.546539202   5.725189814   7.295907087   8.335251036   3.788657332
## 8    2.954175247   8.295694274   7.878356091   8.194124656   6.671035230
## 9    1.271925096   6.526641199   9.731558809   6.724436719   5.248374944
## 10   2.051980834   6.568390768  10.029886345   2.942706253   3.145441426
##              Jun           Jul           Aug           Sep           Oct
## 1    3.804723643  -4.201949771  -9.647884638  -9.329538871  -5.957283272
## 2    1.261508231  -4.227279791  -9.994007236  -9.042234635  -7.907944137
## 3    0.178414446  -2.309973992  -5.003147293  -6.631381141  -9.718751930
## 4    1.472991930  -3.227946863  -7.459506481  -7.763710978  -6.660125940
## 5   -1.154936002   0.004965461  -6.794801488  -4.266296311  -9.630008602
## 6    0.662358346  -8.028420996  -6.504242364  -5.526649907  -2.853055194
## 7   -2.517297256  -0.631128584  -5.105420647  -7.525139455  -4.491986010
## 8    0.014580181  -1.964882726  -9.305071301  -9.443208881  -3.889767807
## 9    1.918010756  -0.657890342  -6.816169764  -8.103963812 -10.434677949
## 10   0.233274567  -5.786415140  -6.260397345  -7.177140159  -6.994275549
##              Nov           Dec
## 1   -4.751205743  -1.123752727
## 2   -3.905691134   2.600397355
## 3   -2.300713909  -0.893114433
## 4   -3.557961063   3.281692332
## 5   -3.958032827   2.499829142
## 6   -1.397648016   1.513549528
## 7   -6.677548574   1.321640596
## 8   -3.245224054  -4.104445641
## 9   -3.801344812  -1.143700116
## 10  -8.931796388   5.142916292

Variabel musiman dengan variasi bulanan.

Data frame
data_ts <- data.frame(
  Y = as.numeric(Y_ts),
  X1 = as.numeric(X1_ts),
  X2 = as.numeric(X2_ts),
  trend = t,
  season = as.factor(cycle(Y_ts))
)
data_ts
##               Y         X1            X2 trend season
## 1     2.6976218  0.6529398   2.422756058     1      1
## 2     8.5093666 -2.2424238   5.923805794     2      2
## 3    19.2935416 -0.5716723  10.992121340     3      3
## 4    11.0127960  0.4317234   4.653595989     4      4
## 5     8.1464387  7.0315860   3.641896811     5      5
## 6    11.5753249 -0.1558497   3.804723643     6      6
## 7     0.8045810  2.8061597  -4.201949771     7      7
## 8   -10.9855602  2.6338825  -9.647884638     8      8
## 9    -8.9342643 -0.1855699  -9.329538871     9      9
## 10   -5.8885639  2.7860757  -5.957283272    10     10
## 11    6.6204090  7.6336526  -4.751205743    11     11
## 12    7.7990691  4.9545122  -1.123752727    12     12
## 13   13.5038573  4.0236988   3.312165532    13      1
## 14   16.2136676  2.9325095   7.109196525    14      2
## 15   14.7207943 -1.6597417  11.197017542    15      3
## 16   25.5948197  8.1940116   6.751073006    16      4
## 17   15.9892524  0.7180798   6.161598992    17      5
## 18   -0.8330858  7.6198425   1.261508231    18      6
## 19    8.0067795 11.4273107  -4.227279791    19      7
## 20   -1.0242111  1.6683205  -9.994007236    20      8
## 21   -4.8391185  8.4053530  -9.042234635    21      9
## 22    1.2498714  5.8134075  -7.907944137    22     10
## 23    1.3699778  2.1835675  -3.905691134    23     11
## 24    8.3555439  2.6559970   2.600397355    24     12
## 25   14.3748037  2.6953915   8.586157948    25      1
## 26   13.2267875  6.2072804  10.023365348    26      2
## 27   27.6889352  3.7147332   7.733698071    27      3
## 28   23.4271196 10.4637503   3.415148439    28      4
## 29   13.8093153 15.0003268   3.222440272    29      5
## 30   21.2690746  5.1389086   0.178414446    30      6
## 31   12.6323211 11.6632165  -2.309973992    31      7
## 32    5.8643885 11.9071267  -5.003147293    32      8
## 33   10.9756283 10.8966077  -6.631381141    33      9
## 34   12.7304134  7.1748702  -9.718751930    34     10
## 35   16.6079054 10.1416422  -2.300713909    35     11
## 36   21.4432013  9.9588140  -0.893114433    36     12
## 37   26.2695883 12.7889686   4.349605400    37      1
## 38   27.3506955 10.2826837   7.077305585    38      2
## 39   27.9701867 14.6309202   8.856333530    39      3
## 40   26.7578990 10.8762574   6.977553196    40      4
## 41   22.0264651 15.4581344   0.665049805    41      5
## 42   19.9604136  9.4524690   1.472991930    42      6
## 43   10.1730182  9.1195343  -3.227946863    43      7
## 44   24.1845258 22.9231198  -7.459506481    44      8
## 45   18.5398100 12.2494272  -7.763710978    45      9
## 46    8.7242030 14.6946828  -6.660125940    46     10
## 47   16.4855758 16.0097090  -3.557961063    47     11
## 48   21.6667232 12.9486581   3.281692332    48     12
## 49   33.3998256 16.2505861   3.561899242    49      1
## 50   33.2434087 16.1068936   7.264333998    50      2
## 51   36.7665926 14.6538585  10.336767746    51      3
## 52   34.5175203 15.7958791   9.036565277    52      4
## 53   31.2856477 15.7977982   6.290526221    53      5
## 54   33.8430114 22.5853557  -1.154936002    54      6
## 55   21.3711451 14.2759917   0.004965461    55      7
## 56   26.9220990 13.5120112  -6.794801488    56      8
## 57   10.7562360 17.2133652  -4.266296311    57      9
## 58   23.2628147 18.3314422  -9.630008602    58     10
## 59   25.1192712 19.0095704  -3.958032827    59     11
## 60   31.0797078 16.6249040   2.499829142    60     12
## 61   37.3981974 15.1100216   2.569515626    61      1
## 62   37.1486368 22.3895555   5.422825294    62      2
## 63   39.8339631 17.8510488   6.122922593    63      3
## 64   35.5673771 16.6034614   4.823176672    64      4
## 65   32.1410439 18.7911613   3.125680934    65      5
## 66   34.5176432 19.2084723   0.662358346    66      6
## 67   30.7410489 23.4297609  -8.028420996    67      7
## 68   25.6047671 20.6542119  -6.504242364    68      8
## 69   29.1113373 22.9621614  -5.526649907    69      9
## 70   36.5901694 19.5021239  -2.853055194    70     10
## 71   28.0448442 21.9433359  -1.397648016    71     11
## 72   24.4541556 20.6259423   1.513549528    72     12
## 73   46.5286926 22.1837506   0.546539202    73      1
## 74   42.1142502 19.5139099   5.725189814    74      2
## 75   44.0599569 18.5675954   7.295907087    75      3
## 76   51.7881109 28.7916402   8.335251036    76      4
## 77   42.0761350 24.9021265   3.788657332    77      5
## 78   32.8964114 19.6461859  -2.517297256    78      6
## 79   35.4065174 21.8665022  -0.631128584    79      7
## 80   30.6452891 20.4435597  -5.105420647    80      8
## 81   30.5288209 30.8964310  -7.525139455    81      9
## 82   34.2661480 28.5372389  -4.491986010    82     10
## 83   34.6466998 24.1045648  -6.677548574    83     11
## 84   45.2218827 26.8295822   1.321640596    84     12
## 85   46.3975672 24.2569802   2.954175247    85      1
## 86   53.3191639 24.3712593   8.295694274    86      2
## 87   58.9841951 23.7341915   7.878356091    87      3
## 88   54.8361615 24.6161482   8.194124656    88      4
## 89   47.8703421 31.6527224   6.671035230    89      5
## 90   50.7440381 26.8379156   0.014580181    90      6
## 91   45.4675193 27.6577357  -1.964882726    91      7
## 92   40.0817308 28.3310623  -9.305071301    92      8
## 93   37.6936587 31.5974276  -9.443208881    93      9
## 94   35.2002156 26.6518085  -3.889767807    94     10
## 95   49.3032622 25.5224785  -3.245224054    95     11
## 96   44.9987021 33.8270908  -4.104445641    96     12
## 97   64.4366650 27.7765103   1.271925096    97      1
## 98   65.3233072 27.2308021   6.526641199    98      2
## 99   58.3214982 25.9911806   9.731558809    99      3
## 100  53.5281495 26.1458528   6.724436719   100      4
## 101  51.9479672 28.5780796   5.248374944   101      5
## 102  52.2844185 32.4539575   1.918010756   102      6
## 103  45.2665406 34.2295444  -0.657890342   103      7
## 104  41.6020330 33.3227651  -6.816169764   104      8
## 105  37.7419072 30.4090281  -8.103963812   105      9
## 106  44.1146073 31.9792498 -10.434677949   106     10
## 107  44.5754777 29.9862106  -3.801344812   107     11
## 108  45.6602903 30.2483455  -1.143700116   108     12
## 109  57.5988674 35.3539515   2.051980834   109      1
## 110  68.2552371 29.9532223   6.568390768   110      2
## 111  62.6232652 39.1658819  10.029886345   111      3
## 112  67.7000756 33.3290412   2.942706253   112      4
## 113  53.4105865 34.5436165   3.145441426   113      5
## 114  56.7221902 31.9844169   0.233274567   114      6
## 115  55.0970360 32.7768339  -5.786415140   115      7
## 116  50.8455128 30.8489516  -6.260397345   116      8
## 117  49.0283810 34.5512238  -7.177140159   117      9
## 118  47.1362159 36.6569472  -6.994275549   118     10
## 119  50.2514783 36.6729130  -8.931796388   119     11
## 120  54.8793560 33.6553905   5.142916292   120     12

Plot Time Series

autoplot(cbind(Y = Y_ts, X1 = X1_ts, X2 = X2_ts)) +
  ggtitle("Visualisasi Time Series Y, X1, dan X2") +
  xlab("Tahun") + ylab("Nilai") +
  theme_minimal()

Visualisasi deret waktu untuk Y, X1, dan X2 untuk melihat tren dan pola musiman masing-masing variabel.

Model Regresi Time Series

ts_data <- ts(cbind(Y, X1, X2), start = c(1,1), frequency = 12)
model_ts <- tslm(ts_data[, "Y"] ~ trend + X1 + X2 + season, data = data_ts)
model_ts
## 
## Call:
## tslm(formula = ts_data[, "Y"] ~ trend + X1 + X2 + season, data = data_ts)
## 
## Coefficients:
## (Intercept)        trend           X1           X2      season2      season3  
##      6.8785       0.4130       0.2686       0.1080       1.5000       3.4422  
##     season4      season5      season6      season7      season8      season9  
##      2.2669      -4.9967      -5.1168     -10.3215     -13.3242     -16.3728  
##    season10     season11     season12  
##    -13.9906     -11.1496      -8.8431

Dibuat data frame yang menggabungkan semua variabel serta menambahkan:

trend sebagai variabel waktu eksplisit

season sebagai faktor musiman (bulan ke-1 sampai ke-12)

Model dibangun menggunakan fungsi tslm() dari paket forecast, yang cocok untuk regresi dengan komponen waktu dan musiman.

Visualisasi Hasil Fitting

autoplot(Y_ts, series = "Y (aktual)") +
  autolayer(fitted(model_ts), series = "Y (fitted)", PI = FALSE) +
  ggtitle("Hasil Fitting Regresi Time Series") +
  ylab("Y") + xlab("Tahun") +
  theme_minimal()
## Warning in ggplot2::geom_line(ggplot2::aes(x = .data[["timeVal"]], y =
## .data[["seriesVal"]], : Ignoring unknown parameters: `PI`

Grafik ini menunjukkan seberapa baik model mengikuti nilai aktual Y selama 120 bulan. Garis hasil model dibandingkan dengan garis data aktual.

Kesimpulan

Model berhasil memodelkan variabel dependen Y menggunakan variabel X1, X2, serta komponen tren dan musiman. Koefisien model menunjukkan bahwa baik tren maupun musiman memberikan kontribusi signifikan terhadap variabel respon.