Membuat data secara acak
set.seed(123)
x1 <- rnorm(100)
x2 <- rnorm(100)
y <- 1 + 2*x1 + 3*x2 + 4*x1*x2 + 5*x1^2 + 6*x2^2 + rnorm(100)
Menghitung model regresi linier kompleks menggunakan fungsi glm()
model <- glm(y ~ x1 + x2 + x1:x2 + poly(x1, 2) + poly(x2, 2), family = gaussian(link = "identity"))
Menghitung nilai koefisien dan p-value untuk setiap variabel prediktor
summary(model)
##
## Call:
## glm(formula = y ~ x1 + x2 + x1:x2 + poly(x1, 2) + poly(x2, 2),
## family = gaussian(link = "identity"))
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.76744 -0.65737 -0.07054 0.59071 2.25075
##
## Coefficients: (2 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.08284 0.09577 115.72 <2e-16 ***
## x1 3.08689 0.11098 27.81 <2e-16 ***
## x2 5.44620 0.09942 54.78 <2e-16 ***
## poly(x1, 2)1 NA NA NA NA
## poly(x1, 2)2 57.23563 0.95199 60.12 <2e-16 ***
## poly(x2, 2)1 NA NA NA NA
## poly(x2, 2)2 83.26731 1.05317 79.06 <2e-16 ***
## x1:x2 4.16062 0.12684 32.80 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 0.8959854)
##
## Null deviance: 11035.817 on 99 degrees of freedom
## Residual deviance: 84.223 on 94 degrees of freedom
## AIC: 280.62
##
## Number of Fisher Scoring iterations: 2
Menggambar plot residual untuk memeriksa apakah asumsi normal terpenuhi
plot(resid(model))
Menggambar plot QQ-plot untuk memeriksa normalitas residual
qqnorm(resid(model))
qqline(resid(model))