Simulasi GLM dengan Respon (Y) Normal
# Pembangkitan data simulasi
set.seed(1001)
n <- 50
x <- runif(n,1,6)
b0 <- 1.5
b1 <- 3.0
y <- c(1:n)
for (i in 1:n) {y[i] <- rnorm(1,b0+b1*x[i],1)}
plot(x,y)

## x y
## [1,] 5.928444 18.690387
## [2,] 3.063142 8.788586
## [3,] 3.147696 11.136597
## [4,] 3.095861 10.205783
## [5,] 3.132533 9.352388
## [6,] 5.438988 18.295776
## [7,] 1.030480 4.820782
## [8,] 1.406079 5.580310
## [9,] 2.443287 11.240599
## [10,] 4.826711 14.057338
## [11,] 3.214621 12.806805
## [12,] 1.691815 8.696805
## [13,] 5.312370 17.248728
## [14,] 3.199381 8.976030
## [15,] 2.333304 8.776508
## [16,] 5.406457 17.093838
## [17,] 1.910408 7.777324
## [18,] 2.813841 10.097222
## [19,] 1.277501 4.628636
## [20,] 4.875929 16.089897
## [21,] 4.094598 14.488958
## [22,] 2.841569 8.300606
## [23,] 5.744559 18.955056
## [24,] 4.632981 15.874495
## [25,] 2.335107 11.890792
## [26,] 5.277346 16.321726
## [27,] 4.399038 12.892394
## [28,] 2.302577 6.787353
## [29,] 5.610446 19.242520
## [30,] 2.403594 8.972020
## [31,] 3.719502 13.239102
## [32,] 5.801831 17.864173
## [33,] 5.846162 20.198548
## [34,] 2.967490 12.549822
## [35,] 1.757231 6.125281
## [36,] 1.619432 5.427364
## [37,] 1.449286 6.633208
## [38,] 4.726190 15.212516
## [39,] 4.551734 14.827102
## [40,] 1.440019 6.735387
## [41,] 3.376266 13.318166
## [42,] 1.831106 8.666407
## [43,] 3.692393 13.376648
## [44,] 1.683731 7.917913
## [45,] 5.704839 17.750434
## [46,] 1.716915 5.218002
## [47,] 4.981152 16.002165
## [48,] 1.008779 3.946260
## [49,] 4.527118 13.109931
## [50,] 4.646557 17.004236
Pemodelan GLM dengan Identity Link-function
# Pemodelan dengan GLM
# Bandingkan nilai parameter (beta0 dan beta1) dengan hasil dugaannya
fit.dataku <- glm(y ~ x, family=gaussian(link="identity"))
summary(fit.dataku)
##
## Call:
## glm(formula = y ~ x, family = gaussian(link = "identity"))
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.0186 0.4436 4.551 3.65e-05 ***
## x 2.8581 0.1176 24.308 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 1.63506)
##
## Null deviance: 1044.593 on 49 degrees of freedom
## Residual deviance: 78.483 on 48 degrees of freedom
## AIC: 170.44
##
## Number of Fisher Scoring iterations: 2
y_duga <- fitted(fit.dataku)
sisaan <- resid(fit.dataku)
cbind(x,y,y_duga,sisaan)
## x y y_duga sisaan
## 1 5.928444 18.690387 18.962859 -0.27247193
## 2 3.063142 8.788586 10.773441 -1.98485474
## 3 3.147696 11.136597 11.015107 0.12148961
## 4 3.095861 10.205783 10.866956 -0.66117247
## 5 3.132533 9.352388 10.971768 -1.61938059
## 6 5.438988 18.295776 17.563928 0.73184798
## 7 1.030480 4.820782 4.963818 -0.14303598
## 8 1.406079 5.580310 6.037330 -0.45701975
## 9 2.443287 11.240599 9.001810 2.23878933
## 10 4.826711 14.057338 15.813957 -1.75661973
## 11 3.214621 12.806805 11.206387 1.60041806
## 12 1.691815 8.696805 6.854003 1.84280170
## 13 5.312370 17.248728 17.202036 0.04669234
## 14 3.199381 8.976030 11.162829 -2.18679949
## 15 2.333304 8.776508 8.687464 0.08904450
## 16 5.406457 17.093838 17.470949 -0.37711181
## 17 1.910408 7.777324 7.478772 0.29855244
## 18 2.813841 10.097222 10.060904 0.03631806
## 19 1.277501 4.628636 5.669836 -1.04120030
## 20 4.875929 16.089897 15.954631 0.13526578
## 21 4.094598 14.488958 13.721480 0.76747727
## 22 2.841569 8.300606 10.140156 -1.83955006
## 23 5.744559 18.955056 18.437291 0.51776471
## 24 4.632981 15.874495 15.260252 0.61424285
## 25 2.335107 11.890792 8.692617 3.19817482
## 26 5.277346 16.321726 17.101934 -0.78020800
## 27 4.399038 12.892394 14.591612 -1.69921862
## 28 2.302577 6.787353 8.599644 -1.81229044
## 29 5.610446 19.242520 18.053979 1.18854146
## 30 2.403594 8.972020 8.888364 0.08365577
## 31 3.719502 13.239102 12.649406 0.58969514
## 32 5.801831 17.864173 18.600984 -0.73681083
## 33 5.846162 20.198548 18.727686 1.47086221
## 34 2.967490 12.549822 10.500052 2.04976992
## 35 1.757231 6.125281 7.040969 -0.91568859
## 36 1.619432 5.427364 6.647121 -1.21975686
## 37 1.449286 6.633208 6.160823 0.47238551
## 38 4.726190 15.212516 15.526655 -0.31413908
## 39 4.551734 14.827102 15.028038 -0.20093541
## 40 1.440019 6.735387 6.134337 0.60105003
## 41 3.376266 13.318166 11.668391 1.64977488
## 42 1.831106 8.666407 7.252115 1.41429234
## 43 3.692393 13.376648 12.571924 0.80472320
## 44 1.683731 7.917913 6.830897 1.08701558
## 45 5.704839 17.750434 18.323768 -0.57333337
## 46 1.716915 5.218002 6.925741 -1.70773832
## 47 4.981152 16.002165 16.255372 -0.25320687
## 48 1.008779 3.946260 4.901792 -0.95553208
## 49 4.527118 13.109931 14.957682 -1.84775158
## 50 4.646557 17.004236 15.299055 1.70518141
# Analisis sisaan
plot(x,y)
par(col="red")
abline(fit.dataku)

par(col="black")
plot(y_duga,sisaan)

qqnorm(sisaan)
qqline(sisaan)
