library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggridges)
library(GGally) 
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(plotly) 
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
model.reg= lm(formula = Y ~ X, data = data.m)
model.reg
## 
## Call:
## lm(formula = Y ~ X, data = data.m)
## 
## Coefficients:
## (Intercept)            X  
##     46.4604      -0.7525
summary(model.reg)
## 
## Call:
## lm(formula = Y ~ X, data = data.m)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.1628 -4.7313 -0.9253  3.7386  9.0446 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 46.46041    2.76218   16.82 3.33e-10 ***
## X           -0.75251    0.07502  -10.03 1.74e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.891 on 13 degrees of freedom
## Multiple R-squared:  0.8856, Adjusted R-squared:  0.8768 
## F-statistic: 100.6 on 1 and 13 DF,  p-value: 1.736e-07
plot(x = data.m$X,y = data.m$Y)

Eksplorasi Kondisi Gauss-Markov

plot sisaan vs yduga

(pola kurva) Model tidak pas (perlu suku-suku lain dalam model atau transformasi thdp Y)

Plot Sisaan vs Urutan

plot(x = 1:dim(data.m)[1],
     y = model.reg$residuals,
     type = 'b', 
     ylab = "Residuals",
     xlab = "Observation")

Tebaran memiliki pola → sisaan tidak saling bebas

Eksplorasi Normalitas Sisaan - qq-plot

plot(model.reg,2)

sisaan tidaK menyebar normal

Uji Formal Kondisi Gauss-Markov

p-value < 0.05 tolak h0

1 Nilai harapan sisaan sama dengan nol

t.test(model.reg$residuals,mu = 0,conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  model.reg$residuals
## t = -4.9493e-16, df = 14, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -3.143811  3.143811
## sample estimates:
##     mean of x 
## -7.254614e-16

H0: Nilai harapan sisaan sama dengan nol

H1: Nilai harapan sisaan tidak sama dengan nol

kesimpulan: Tolak H0, Nilai harapan sisaan tidak sama dengan nol

2 Ragam sisaan homogen

cek.homogen = lm(formula = abs(model.reg$residuals) ~ X, 
    data = data.m)
summary(cek.homogen)
## 
## Call:
## lm(formula = abs(model.reg$residuals) ~ X, data = data.m)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.2525 -1.7525  0.0235  2.0168  4.2681 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5.45041    1.27241   4.284  0.00089 ***
## X           -0.01948    0.03456  -0.564  0.58266    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.714 on 13 degrees of freedom
## Multiple R-squared:  0.02385,    Adjusted R-squared:  -0.05124 
## F-statistic: 0.3176 on 1 and 13 DF,  p-value: 0.5827
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
bptest(model.reg)
## 
##  studentized Breusch-Pagan test
## 
## data:  model.reg
## BP = 0.52819, df = 1, p-value = 0.4674
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
ncvTest(model.reg)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.1962841, Df = 1, p = 0.65774

H0: Ragam sisaan homogen

H1: Ragam sisaan tidak homogen

Kesimpulan: P value = 0.65774 > 0.05, maka tak tolak H0, artinya ragam sisaan homogen

3 Sisaan saling bebas

library(randtests)
runs.test(model.reg$residuals)
## 
##  Runs Test
## 
## data:  model.reg$residuals
## statistic = -2.7817, runs = 3, n1 = 7, n2 = 7, n = 14, p-value =
## 0.005407
## alternative hypothesis: nonrandomness
library(lmtest)
dwtest(model.reg)
## 
##  Durbin-Watson test
## 
## data:  model.reg
## DW = 0.48462, p-value = 1.333e-05
## alternative hypothesis: true autocorrelation is greater than 0

H0: Sisaan saling bebas

H1: Sisaan tidak saling bebas

Kesimpulan: p-value = 1.333e-05 < 0.005, maka tolak H0, artinya sisaan tidak saling bebas

Uji Formal Normalitas Sisaan

ks.test(model.reg$residuals, "pnorm", mean=mean(model.reg$residuals), sd=sd(model.reg$residuals))
## 
##  Exact one-sample Kolmogorov-Smirnov test
## 
## data:  model.reg$residuals
## D = 0.12432, p-value = 0.9521
## alternative hypothesis: two-sided
library(car)
shapiro.test(model.reg$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model.reg$residuals
## W = 0.92457, p-value = 0.226

H0: Sisaan menyebar normal

H1: Sisaan tidak menyebar normal

Kesimpulan: p-value = 0.226 > 0.005, maka tak tolak H0, artinya sisaan tidak menyebar normal

ASUMSI TERPENUHI:

  1. Ragam sisaan homogen

ASUMSI TIDAK TERPENUHI:

  1. Nilai harapan sisaan sama dengan nol

  2. Sisaan saling bebas

  3. Sisaan menyebar normal

Transformasi Data

y_tr <- sqrt(data.m$Y)
y_tr
##  [1] 7.348469 7.071068 6.708204 6.082763 5.916080 5.000000 4.472136 4.000000
##  [9] 4.242641 3.605551 2.828427 3.316625 2.828427 2.000000 2.449490
x_tr <- sqrt(data.m$X)
x_tr
##  [1] 1.414214 2.236068 2.645751 3.162278 3.741657 4.358899 5.099020 5.567764
##  [9] 5.830952 6.164414 6.708204 7.211103 7.280110 7.745967 8.062258
model.reg2= lm(formula = y_tr ~ x_tr, data = data.m)
model.reg2
## 
## Call:
## lm(formula = y_tr ~ x_tr, data = data.m)
## 
## Coefficients:
## (Intercept)         x_tr  
##      8.7125      -0.8134
summary(model.reg2)
## 
## Call:
## lm(formula = y_tr ~ x_tr, data = data.m)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.42765 -0.17534 -0.05753  0.21223  0.46960 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.71245    0.19101   45.61 9.83e-16 ***
## x_tr        -0.81339    0.03445  -23.61 4.64e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2743 on 13 degrees of freedom
## Multiple R-squared:  0.9772, Adjusted R-squared:  0.9755 
## F-statistic: 557.3 on 1 and 13 DF,  p-value: 4.643e-12

Uji Asumsi Transformasi

Autokorelasi: Durbin-Watson

library(lmtest)
dwtest(model.reg2)
## 
##  Durbin-Watson test
## 
## data:  model.reg2
## DW = 2.6803, p-value = 0.8629
## alternative hypothesis: true autocorrelation is greater than 0

p-value = 0.8629 > 0.05, maka tak tolak H0, artinya tidak terdapat terdapat autokorlasi

Normalitas: Kolmogorov-Smirnov

library(nortest)
sisaan_model2 <- resid(model.reg2)
(norm_model3 <- lillie.test(sisaan_model2))
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  sisaan_model2
## D = 0.11948, p-value = 0.817

p-value = 0.817 > 0.05, maka tak tolak H0, artinya Sisaan Menyebar Normal

\[ \hat{Y}^* = 8.71245 - 0.8139X \]