library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ 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)
## Warning: package 'ggridges' was built under R version 4.3.3
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## 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
modelreg= lm(formula = Y ~ X, data = data)
modelreg
##
## Call:
## lm(formula = Y ~ X, data = data)
##
## Coefficients:
## (Intercept) X
## 46.4604 -0.7525
summary(modelreg)
##
## Call:
## lm(formula = Y ~ X, data = data)
##
## 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$X,y = data$Y)
plot
## function (x, y, ...)
## UseMethod("plot")
## <bytecode: 0x0000020b6dd836d8>
## <environment: namespace:base>
Plot Sisaan vs Yduga
(pola kurva perlu suku-suku lain dalam model atau transformasi thdp
Y)
Plot Sisaan vs Urutan
plot(x = 1:dim(data)[1],
y = modelreg$residuals,
type = 'b',
ylab = "Residuals",
xlab = "Observation")
Tebaran memiliki pola → sisaan tidak saling bebas
Eksplorasi Normalitas Sisaan - qq-plot
plot(modelreg,2)
sisaan tidaK menyebar normal
p-value < 0.05 tolak h0
1 Nilai harapan sisaan sama dengan nol
t.test(modelreg$residuals,mu = 0,conf.level = 0.95)
##
## One Sample t-test
##
## data: modelreg$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
homogen = lm(formula = abs(modelreg$residuals) ~ X,
data = data)
summary(homogen)
##
## Call:
## lm(formula = abs(modelreg$residuals) ~ X, data = data)
##
## 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)
## Warning: package 'lmtest' was built under R version 4.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
bptest(modelreg)
##
## studentized Breusch-Pagan test
##
## data: modelreg
## 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(modelreg)
## 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(modelreg$residuals)
##
## Runs Test
##
## data: modelreg$residuals
## statistic = -2.7817, runs = 3, n1 = 7, n2 = 7, n = 14, p-value =
## 0.005407
## alternative hypothesis: nonrandomness
library(lmtest)
dwtest(modelreg)
##
## Durbin-Watson test
##
## data: modelreg
## 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
ks.test(modelreg$residuals, "pnorm", mean=mean(modelreg$residuals), sd=sd(modelreg$residuals))
##
## Exact one-sample Kolmogorov-Smirnov test
##
## data: modelreg$residuals
## D = 0.12432, p-value = 0.9521
## alternative hypothesis: two-sided
library(car)
shapiro.test(modelreg$residuals)
##
## Shapiro-Wilk normality test
##
## data: modelreg$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: 2. Ragam sisaan homogen
ASUMSI TIDAK TERPENUHI: 1. Nilai harapan sisaan sama dengan nol 3. Sisaan saling bebas 4. Sisaan menyebar normal
y_tr <- sqrt(data$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$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
modelreg2= lm(formula = y_tr ~ x_tr, data = data)
modelreg2
##
## Call:
## lm(formula = y_tr ~ x_tr, data = data)
##
## Coefficients:
## (Intercept) x_tr
## 8.7125 -0.8134
summary(modelreg2)
##
## Call:
## lm(formula = y_tr ~ x_tr, data = data)
##
## 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
Autokorelasi: Durbin-Watson
library(lmtest)
dwtest(modelreg2)
##
## Durbin-Watson test
##
## data: modelreg2
## 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(modelreg2)
(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 \]