Load Package

library(readxl)
library(RcmdrMisc)
## Loading required package: car
## Warning: package 'car' was built under R version 4.0.4
## Loading required package: carData
## Loading required package: sandwich
library(olsrr)
## Warning: package 'olsrr' was built under R version 4.0.5
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(DataExplorer)
## Warning: package 'DataExplorer' was built under R version 4.0.5
library(performance)
## Warning: package 'performance' was built under R version 4.0.5
library(sjPlot)
## Warning: package 'sjPlot' was built under R version 4.0.5
## Registered S3 methods overwritten by 'lme4':
##   method                          from
##   cooks.distance.influence.merMod car 
##   influence.merMod                car 
##   dfbeta.influence.merMod         car 
##   dfbetas.influence.merMod        car
library(fBasics)
## Warning: package 'fBasics' was built under R version 4.0.5
## Loading required package: timeDate
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 4.0.5
## 
## Attaching package: 'fBasics'
## The following object is masked from 'package:car':
## 
##     densityPlot
library(qqplotr)
## Warning: package 'qqplotr' was built under R version 4.0.5
## Loading required package: ggplot2
## 
## Attaching package: 'qqplotr'
## The following objects are masked from 'package:ggplot2':
## 
##     stat_qq_line, StatQqLine
library(ggstatsplot)
## Warning: package 'ggstatsplot' was built under R version 4.0.5
## You can cite this package as:
##      Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
##      PsyArxiv. doi:10.31234/osf.io/p7mku
library(PerformanceAnalytics)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## Attaching package: 'PerformanceAnalytics'
## The following objects are masked from 'package:timeDate':
## 
##     kurtosis, skewness
## The following object is masked from 'package:graphics':
## 
##     legend
library(see)
## Warning: package 'see' was built under R version 4.0.5

Preparing Data

antar=read_excel("E:\\Praktikum13.xlsx",sheet="Sheet1")
## New names:
## * `` -> ...1
antar
## # A tibble: 13 x 6
##     ...1       x1    x2    x3        x4       y
##    <dbl>    <dbl> <dbl> <dbl>     <dbl>   <dbl>
##  1  1999  379558.  3.31  7100 210611000  24003.
##  2  2000 1389770. 10.3   9595 213395000  33515.
##  3  2001 1440406. 11.8  20266 216203000  30962.
##  4  2002 1505216.  9     9261 219026000  31289.
##  5  2003 1577171.  5.92  8571 221839000  32551.
##  6  2004 1656517.  6.35  9030 224607000  46525.
##  7  2005 1750815. 15.6   9751 227303000  57701.
##  8  2006 1847127.  5.66  9141 229919000  61066.
##  9  2007 1964327.  6.48  9142 232462000  74473.
## 10  2008 2082456.  4.15  9772 234951000 129197.
## 11  2009 2177742.  3.45 10356 237414000  96856.
## 12  2010 2310690.  5.96  9078 239871000 135606.
## 13  2011 1838058.  4.23  8731 242326000 156130.

Eksplorasi

plot_scatterplot(data = antar[,-1], by="y",geom_point_args= list(color="steelblue") )

ggcorrmat(data = antar %>% select(-1,-y))

#melihat matriks korelasi
chart.Correlation(antar[,-1], histogram=FALSE, pch=19)

model regresi berganda

regresi=lm(y~x1+x2+x3+x4,data=antar) #full model
regresi
## 
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4, data = antar)
## 
## Coefficients:
## (Intercept)           x1           x2           x3           x4  
##  -1.082e+06   -3.088e-02   -5.043e+02    1.252e+00    5.267e-03
summary(regresi)
## 
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4, data = antar)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -19428.4 -12879.7   -858.6   9803.5  27917.0 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -1.082e+06  2.747e+05  -3.939  0.00430 **
## x1          -3.088e-02  2.639e-02  -1.170  0.27567   
## x2          -5.043e+02  2.038e+03  -0.247  0.81081   
## x3           1.252e+00  2.078e+00   0.602  0.56356   
## x4           5.267e-03  1.328e-03   3.966  0.00414 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19850 on 8 degrees of freedom
## Multiple R-squared:  0.872,  Adjusted R-squared:  0.8079 
## F-statistic: 13.62 on 4 and 8 DF,  p-value: 0.001206

Model Checking

check_normality(regresi,effects="random")
## OK: residuals appear as normally distributed (p = 0.063).
plot(check_normality(regresi,effects="random"))
## OK: residuals appear as normally distributed (p = 0.063).

check_model(regresi,panel=T)

check_distribution(regresi)
## # Distribution of Model Family
## 
## Predicted Distribution of Residuals
## 
##  Distribution Probability
##     lognormal         34%
##        normal         34%
##       weibull         12%
## 
## Predicted Distribution of Response
## 
##  Distribution Probability
##     lognormal         81%
##   exponential          6%
##       uniform          6%
plot(check_distribution(regresi))
## Warning: Removed 4 rows containing missing values (geom_segment).
## Warning: Removed 4 rows containing missing values (geom_point).

m1<-lm(y~x1+x2+x3+x4,data=antar) #full model
m2<-lm(y~x1+x3+x4,data=antar) 
m3<-lm(y~x1+x4,data=antar) 
m4<-lm(y~x4,data=antar) 

compare_performance(m1,m2,m3,m4)
## # Comparison of Model Performance Indices
## 
## Name | Model |     AIC |     BIC |    R2 | R2 (adj.) |      RMSE |     Sigma
## ----------------------------------------------------------------------------
## m1   |    lm | 299.878 | 303.267 | 0.872 |     0.808 | 15572.818 | 19851.525
## m2   |    lm | 297.977 | 300.802 | 0.871 |     0.828 | 15632.287 | 18787.671
## m3   |    lm | 296.465 | 298.725 | 0.866 |     0.839 | 15928.702 | 18161.515
## m4   |    lm | 296.762 | 298.457 | 0.840 |     0.826 | 17400.008 | 18915.803
plot(compare_performance(m1,m2,m3,m4))

#corelation partial
a=data.frame(antar$x1,antar$x2,antar$y)
partial.cor(a)
## 
##  Partial correlations:
##          antar.x1 antar.x2  antar.y
## antar.x1  0.00000  0.34596  0.73343
## antar.x2  0.34596  0.00000 -0.52061
## antar.y   0.73343 -0.52061  0.00000
## 
##  Number of observations: 13

backward Elimination

bkward=ols_step_backward_p(regresi, prem = 0.1,details = TRUE)
## Backward Elimination Method 
## ---------------------------
## 
## Candidate Terms: 
## 
## 1 . x1 
## 2 . x2 
## 3 . x3 
## 4 . x4 
## 
## We are eliminating variables based on p value...
## 
## - x2 
## 
## Backward Elimination: Step 1 
## 
##  Variable x2 Removed 
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.933       RMSE                   18787.671 
## R-Squared               0.871       Coef. Var                 26.843 
## Adj. R-Squared          0.828       MSE                352976562.982 
## Pred R-Squared          0.735       MAE                    13030.421 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                     ANOVA                                      
## ------------------------------------------------------------------------------
##                        Sum of                                                 
##                       Squares        DF       Mean Square      F         Sig. 
## ------------------------------------------------------------------------------
## Regression    21445330488.166         3    7148443496.055    20.252     2e-04 
## Residual       3176789066.834         9     352976562.982                     
## Total         24622119555.000        12                                       
## ------------------------------------------------------------------------------
## 
##                                            Parameter Estimates                                            
## ---------------------------------------------------------------------------------------------------------
##       model            Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## ---------------------------------------------------------------------------------------------------------
## (Intercept)    -1116203.978    224622.029                 -4.969    0.001    -1624334.309    -608073.647 
##          x1          -0.034         0.023       -0.359    -1.466    0.177          -0.085          0.018 
##          x3           1.109         1.888        0.078     0.587    0.572          -3.164          5.381 
##          x4           0.005         0.001        1.236     4.953    0.001           0.003          0.008 
## ---------------------------------------------------------------------------------------------------------
## 
## 
## - x3 
## 
## Backward Elimination: Step 2 
## 
##  Variable x3 Removed 
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.931       RMSE                   18161.515 
## R-Squared               0.866       Coef. Var                 25.949 
## Adj. R-Squared          0.839       MSE                329840623.858 
## Pred R-Squared          0.787       MAE                    14111.311 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    21323713316.418         2    10661856658.209    32.324    0.0000 
## Residual       3298406238.582        10      329840623.858                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                            Parameter Estimates                                            
## ---------------------------------------------------------------------------------------------------------
##       model            Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## ---------------------------------------------------------------------------------------------------------
## (Intercept)    -1051096.141    188815.248                 -5.567    0.000    -1471802.731    -630389.551 
##          x1          -0.028         0.020       -0.303    -1.390    0.195          -0.074          0.017 
##          x4           0.005         0.001        1.173     5.388    0.000           0.003          0.007 
## ---------------------------------------------------------------------------------------------------------
## 
## 
## - x1 
## 
## Backward Elimination: Step 3 
## 
##  Variable x1 Removed 
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.917       RMSE                   18915.803 
## R-Squared               0.840       Coef. Var                 27.026 
## Adj. R-Squared          0.826       MSE                357807598.508 
## Pred R-Squared          0.766       MAE                    16333.415 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    20686235971.408         1    20686235971.408    57.814    0.0000 
## Residual       3935883583.592        11      357807598.508                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                           Parameter Estimates                                            
## --------------------------------------------------------------------------------------------------------
##       model           Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## --------------------------------------------------------------------------------------------------------
## (Intercept)    -843383.027    120239.219                 -7.014    0.000    -1108027.763    -578738.291 
##          x4          0.004         0.001        0.917     7.604    0.000           0.003          0.005 
## --------------------------------------------------------------------------------------------------------
## 
## 
## 
## No more variables satisfy the condition of p value = 0.1
## 
## 
## Variables Removed: 
## 
## - x2 
## - x3 
## - x1 
## 
## 
## Final Model Output 
## ------------------
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.917       RMSE                   18915.803 
## R-Squared               0.840       Coef. Var                 27.026 
## Adj. R-Squared          0.826       MSE                357807598.508 
## Pred R-Squared          0.766       MAE                    16333.415 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    20686235971.408         1    20686235971.408    57.814    0.0000 
## Residual       3935883583.592        11      357807598.508                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                           Parameter Estimates                                            
## --------------------------------------------------------------------------------------------------------
##       model           Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## --------------------------------------------------------------------------------------------------------
## (Intercept)    -843383.027    120239.219                 -7.014    0.000    -1108027.763    -578738.291 
##          x4          0.004         0.001        0.917     7.604    0.000           0.003          0.005 
## --------------------------------------------------------------------------------------------------------

forward Selection

forward=ols_step_forward_p(regresi, penter = 0.1,details = TRUE)
## Forward Selection Method    
## ---------------------------
## 
## Candidate Terms: 
## 
## 1. x1 
## 2. x2 
## 3. x3 
## 4. x4 
## 
## We are selecting variables based on p value...
## 
## 
## Forward Selection: Step 1 
## 
## - x4 
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.917       RMSE                   18915.803 
## R-Squared               0.840       Coef. Var                 27.026 
## Adj. R-Squared          0.826       MSE                357807598.508 
## Pred R-Squared          0.766       MAE                    16333.415 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    20686235971.408         1    20686235971.408    57.814    0.0000 
## Residual       3935883583.592        11      357807598.508                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                           Parameter Estimates                                            
## --------------------------------------------------------------------------------------------------------
##       model           Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## --------------------------------------------------------------------------------------------------------
## (Intercept)    -843383.027    120239.219                 -7.014    0.000    -1108027.763    -578738.291 
##          x4          0.004         0.001        0.917     7.604    0.000           0.003          0.005 
## --------------------------------------------------------------------------------------------------------
## 
## 
## 
## No more variables to be added.
## 
## Variables Entered: 
## 
## + x4 
## 
## 
## Final Model Output 
## ------------------
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.917       RMSE                   18915.803 
## R-Squared               0.840       Coef. Var                 27.026 
## Adj. R-Squared          0.826       MSE                357807598.508 
## Pred R-Squared          0.766       MAE                    16333.415 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    20686235971.408         1    20686235971.408    57.814    0.0000 
## Residual       3935883583.592        11      357807598.508                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                           Parameter Estimates                                            
## --------------------------------------------------------------------------------------------------------
##       model           Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## --------------------------------------------------------------------------------------------------------
## (Intercept)    -843383.027    120239.219                 -7.014    0.000    -1108027.763    -578738.291 
##          x4          0.004         0.001        0.917     7.604    0.000           0.003          0.005 
## --------------------------------------------------------------------------------------------------------

stepwise regression

stepw=ols_step_both_p(regresi, penter = 0.15, prem = 0.15,details = TRUE)
## Stepwise Selection Method   
## ---------------------------
## 
## Candidate Terms: 
## 
## 1. x1 
## 2. x2 
## 3. x3 
## 4. x4 
## 
## We are selecting variables based on p value...
## 
## 
## Stepwise Selection: Step 1 
## 
## - x4 added 
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.917       RMSE                   18915.803 
## R-Squared               0.840       Coef. Var                 27.026 
## Adj. R-Squared          0.826       MSE                357807598.508 
## Pred R-Squared          0.766       MAE                    16333.415 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    20686235971.408         1    20686235971.408    57.814    0.0000 
## Residual       3935883583.592        11      357807598.508                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                           Parameter Estimates                                            
## --------------------------------------------------------------------------------------------------------
##       model           Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## --------------------------------------------------------------------------------------------------------
## (Intercept)    -843383.027    120239.219                 -7.014    0.000    -1108027.763    -578738.291 
##          x4          0.004         0.001        0.917     7.604    0.000           0.003          0.005 
## --------------------------------------------------------------------------------------------------------
## 
## 
## 
## No more variables to be added/removed.
## 
## 
## Final Model Output 
## ------------------
## 
##                             Model Summary                             
## ---------------------------------------------------------------------
## R                       0.917       RMSE                   18915.803 
## R-Squared               0.840       Coef. Var                 27.026 
## Adj. R-Squared          0.826       MSE                357807598.508 
## Pred R-Squared          0.766       MAE                    16333.415 
## ---------------------------------------------------------------------
##  RMSE: Root Mean Square Error 
##  MSE: Mean Square Error 
##  MAE: Mean Absolute Error 
## 
##                                      ANOVA                                      
## -------------------------------------------------------------------------------
##                        Sum of                                                  
##                       Squares        DF        Mean Square      F         Sig. 
## -------------------------------------------------------------------------------
## Regression    20686235971.408         1    20686235971.408    57.814    0.0000 
## Residual       3935883583.592        11      357807598.508                     
## Total         24622119555.000        12                                        
## -------------------------------------------------------------------------------
## 
##                                           Parameter Estimates                                            
## --------------------------------------------------------------------------------------------------------
##       model           Beta    Std. Error    Std. Beta      t        Sig            lower          upper 
## --------------------------------------------------------------------------------------------------------
## (Intercept)    -843383.027    120239.219                 -7.014    0.000    -1108027.763    -578738.291 
##          x4          0.004         0.001        0.917     7.604    0.000           0.003          0.005 
## --------------------------------------------------------------------------------------------------------