library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readxl)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(TTR)
library(tseries)
library(ggplot2)
library(lmtest) #uji-Durbin Watson
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(orcutt) #Cochrane-Orcutt
library(HoRM)#Hildreth Lu
setwd("C:/Users/Nabil/Documents/SEM 5/MPDW/Individu")
dataregresi <- read_excel("dataregresi.xlsx")
head(dataregresi)
## # A tibble: 6 × 2
## x y
## <dbl> <dbl>
## 1 2010 67.5
## 2 2011 68.2
## 3 2012 68.9
## 4 2013 69.5
## 5 2014 69.9
## 6 2015 70.3
x <- dataregresi$x
y <- dataregresi$y
#diagram pencar identifikasi model
ggplot(dataregresi,aes(x,y))+geom_point()+scale_x_continuous(labels=as.character(x),breaks=x)
cor(x,y)
## [1] 0.9910039
model <- lm(y~x, data = dataregresi)
summary(model)
##
## Call:
## lm(formula = y ~ x, data = dataregresi)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.44256 -0.16263 0.08495 0.18377 0.23807
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -897.55341 41.34255 -21.71 9.61e-10 ***
## x 0.48031 0.02051 23.42 4.57e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2453 on 10 degrees of freedom
## Multiple R-squared: 0.9821, Adjusted R-squared: 0.9803
## F-statistic: 548.3 on 1 and 10 DF, p-value: 4.571e-10
#sisaan dan fitted value
resi1 <- residuals(model)
fit <- predict(model)
#Diagnostik dengan eksploratif
par(mfrow = c(2,2))
qqnorm(resi1)
qqline(resi1, col = "steelblue", lwd = 2)
plot(fit, resi1, col = "steelblue", pch = 20, xlab = "Sisaan",
ylab = "Fitted Values", main = "Sisaan vs Fitted Values")
abline(a = 0, b = 0, lwd = 2)
hist(resi1, col = "steelblue")
plot(seq(1,12 ,1), resi1, col = "steelblue", pch = 20,
xlab = "Sisaan", ylab = "Order", main = "Sisaan vs Order")
lines(seq(1,12,1), resi1, col = "red")
abline(a = 0, b = 0, lwd = 2)
Dari hasil eksplorasi dapat terlihat bahwa sisaan membentuk pola yang
tidak acak sehingga dapat diindikasikan terdapat autokorelasi pada data.
Dalam upaya memperkuat hal tersebut akan dilakukan uji statistik.
H0: tidak ada autokorelasi
H1: ada autokorelasi
lmtest::dwtest(model, alternative = 'two.sided')
##
## Durbin-Watson test
##
## data: model
## DW = 0.69649, p-value = 0.001294
## alternative hypothesis: true autocorrelation is not 0
Karena p-value < 5% sehingga tolak H0 sehingga cukup bukti untuk menyatakan bahwa terdapat autokorelasi (positif) pada taraf nyata 5%
modelco <- orcutt::cochrane.orcutt(model)
## Warning in orcutt::cochrane.orcutt(model): Did not converge
modelco
## Cochrane-orcutt estimation for first order autocorrelation
##
## Call:
## lm(formula = y ~ x, data = dataregresi)
##
## number of interaction: 100
## rho 0.826619
##
## Durbin-Watson statistic
## (original): 0.69649 , p-value: 6.471e-04
## (transformed): NA , p-value: NA
##
## coefficients:
## [1] NA
rho <- modelco$rho
y
## [1] 67.54 68.22 68.92 69.47 69.89 70.27 70.96 71.42 71.95 72.44 72.45 72.72
#transformasi terhadap y dan x
(y.trans <- y[-1]-y[-24]*rho)
## Warning in y[-1] - y[-24] * rho: longer object length is not a multiple of
## shorter object length
## [1] 12.390178 12.528077 12.499444 12.464804 12.497624 12.873509 12.763142
## [8] 12.912897 12.964789 12.569746 12.831480 8.108293
(x.trans <- x[-1]-x[-24]*rho)
## Warning in x[-1] - x[-24] * rho: longer object length is not a multiple of
## shorter object length
## [1] 349.4965 349.6699 349.8433 350.0167 350.1901 350.3635 350.5368 350.7102
## [9] 350.8836 351.0570 351.2304 340.4037
modelcorho <- lm(y.trans~x.trans)
lmtest::dwtest(modelcorho,alternative = 'two.sided')
##
## Durbin-Watson test
##
## data: modelcorho
## DW = 1.3113, p-value = 0.1666
## alternative hypothesis: true autocorrelation is not 0
Karena p-value > 5% sehingga tak tolak H0 sehingga cukup bukti untuk menyatakan bahwa tidak terdapat autokorelasi pada taraf nyata 5%