Import Data

library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2
Data<-read_xlsx("C:/Users/HESTY/Desktop/SEMESTER 4/Analisis Regresi/Kuliah/Data tugas kuliah anreg 7.xlsx")
Data
## # A tibble: 15 × 2
##        X     Y
##    <dbl> <dbl>
##  1     2    54
##  2     5    50
##  3     7    45
##  4    10    37
##  5    14    35
##  6    19    25
##  7    26    20
##  8    31    16
##  9    34    18
## 10    38    13
## 11    45     8
## 12    52    11
## 13    53     8
## 14    60     4
## 15    65     6

Eksplorasi Data

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

Pemodelan Regresi Linear

model= lm(formula = Y ~ X, data = Data)
model
## 
## Call:
## lm(formula = Y ~ X, data = Data)
## 
## Coefficients:
## (Intercept)            X  
##     46.4604      -0.7525
summary(model)
## 
## 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

scatter plot

plot(x = Data$X,y = Data$Y)

Pemeriksaan Asumsi

Eksplorasi Kondisi Gauss-Markov

1&2 petak sisaan vs yduga

plot(model,1)

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

3 plot sisaan vs urutan

Sisaan saling bebas

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

3. Tebaran berpola → sisaan tidak saling bebas

Eksplorasi Normalitas Sisaan - qq-plot

sisaan menyebar normal

plot(model,2)

Uji Formal Kondisi Gauss-Markov

p-value < 0.05 tolak h0 ### 1 Nilai harapan sisaan sama dengan nol

t.test(model$residuals,mu = 0,conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  model$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

hipotesis: h0: mu = 0 h1: mu ≠ 0 kesimpulan: tolak H0 karena nilai harapan sisaan tidak sama dengan nol

2 Ragam sisaan homogen

cek.homogen = lm(formula = abs(model$residuals) ~ X, # Y: abs residual
    data = Data)
summary(cek.homogen)
## 
## Call:
## lm(formula = abs(model$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.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
bptest(model)
## 
##  studentized Breusch-Pagan test
## 
## data:  model
## BP = 0.52819, df = 1, p-value = 0.4674
library(car)
## Warning: package 'car' was built under R version 4.3.3
## 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)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.1962841, Df = 1, p = 0.65774

hipotesis: h0: homogen h1: tidak homogen Kesimpulan: karena P value = 0.65774 lebih besar dari 0.05, maka tak tolak H0 yang artinya ragam sisaannya homogen

3 Sisaan saling bebas

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

hipotesis: h0: saling bebas h1: tidak saling bebas Kesimpulan: p-value = 1.333e-05 lebih kecil dari 0.005, maka tolak H0, artinya sisaan tidak saling bebas

Uji Formal Normalitas Sisaan

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

hipotesis: h0: menyebar normal h1: tidak menyebar normal Kesimpulan: p-value = 0.226 lebih besar dari 0.005, maka tak tolak H0, artinya sisaan tidak menyebar normal

Kesimpulan

asumsi terpenuhi : - Ragam sisaan homogen

asumsi tidak terpenuhi : - Nilai harapan sisaan sama dengan nol - Sisaan saling bebas - Sisaan menyebar normal

Trasformasi Data

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
model.reg2= lm(formula = y_tr ~ x_tr, data = Data)
model.reg2
## 
## Call:
## lm(formula = y_tr ~ x_tr, data = Data)
## 
## Coefficients:
## (Intercept)         x_tr  
##      8.7125      -0.8134
summary(model.reg2)
## 
## 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

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 lebih besar dari 0.05, maka tak tolak H0, artinya tidak 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 Ŷ∗=8.71245−0.8139X jika dilakukan transformasi balik menjadi: Y∗= 8,7124535 − 0,8133888X∗

Y=( 8.7124535 − 0.8133888X½²)