R Markdown

library(sf)
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(spdep)
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(spatialreg)
## Loading required package: Matrix
## 
## Attaching package: 'spatialreg'
## The following objects are masked from 'package:spdep':
## 
##     get.ClusterOption, get.coresOption, get.mcOption,
##     get.VerboseOption, get.ZeroPolicyOption, set.ClusterOption,
##     set.coresOption, set.mcOption, set.VerboseOption,
##     set.ZeroPolicyOption
library(readxl)
library(ggplot2)
library(car)
## Warning: package 'car' was built under R version 4.6.2
## Loading required package: carData
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
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
dataset <- as.data.frame(
  read_excel(
    "C:\\Users\\LENOVO\\Downloads\\Data Ujian 2026.xlsx"
  )
)

# 5. Membersihkan Dataset
dataset <- dataset %>%
  filter(
    !is.na(Provinsi),
    toupper(trimws(Provinsi)) != "INDONESIA"
  )

names(dataset)
## [1] "Provinsi"                                             
## [2] "Kepemilikan Komputer"                                 
## [3] "Akses Internet"                                       
## [4] "Komsumsi Telkomunikasi"                               
## [5] "Rata-Rata Lama Sekolah Penduduk Umur 15 Tahun ke Atas"
## [6] "Angka Partisipasi Kasar (APK) Perguruan Tinggi (PT)"  
## [7] "Indeks Pembangunan Teknologi Informasi dan Komunikasi"
# 6. Mengganti Nama Variabel
dataset <- dataset %>%
  rename(
    x1 = `Kepemilikan Komputer`,
    x2 = `Akses Internet`,
    x3 = `Komsumsi Telkomunikasi`,
    x4 = `Rata-Rata Lama Sekolah Penduduk Umur 15 Tahun ke Atas`,
    x5 = `Angka Partisipasi Kasar (APK) Perguruan Tinggi (PT)`,
    y  = `Indeks Pembangunan Teknologi Informasi dan Komunikasi`
  )

# 7. Eksplorasi Dataset
str(dataset)
## 'data.frame':    34 obs. of  7 variables:
##  $ Provinsi: chr  "ACEH" "SUMATERA UTARA" "SUMATERA BARAT" "RIAU" ...
##  $ x1      : num  13.7 15.4 20.1 18.6 16.2 ...
##  $ x2      : num  2.41 2.71 2.7 2.75 2.57 2.61 2.54 2.58 2.59 2.83 ...
##  $ x3      : num  3.18 3.59 3.67 3.95 3.76 3.5 3.68 3.46 3.73 4.23 ...
##  $ x4      : num  9.79 9.99 9.51 9.54 9.07 ...
##  $ x5      : num  44.5 30.9 43.8 35.3 30.1 ...
##  $ y       : num  5.6 5.9 6.01 6.02 5.8 5.76 5.95 5.63 5.82 6.69 ...
summary(dataset)
##       Provinsi        x1              x2              x3       
##  Length   :34   Min.   : 9.36   Min.   :2.390   Min.   :2.580  
##  N.unique :34   1st Qu.:14.54   1st Qu.:2.572   1st Qu.:3.598  
##  N.blank  : 0   Median :17.48   Median :2.660   Median :3.820  
##  Min.nchar: 4   Mean   :18.51   Mean   :2.680   Mean   :3.895  
##  Max.nchar:20   3rd Qu.:19.97   3rd Qu.:2.745   3rd Qu.:4.027  
##                 Max.   :36.83   Max.   :3.120   Max.   :5.010  
##        x4               x5              y        
##  Min.   : 7.310   Min.   :14.85   Min.   :3.220  
##  1st Qu.: 8.580   1st Qu.:26.81   1st Qu.:5.605  
##  Median : 9.225   Median :32.58   Median :5.825  
##  Mean   : 9.247   Mean   :34.40   Mean   :5.881  
##  3rd Qu.: 9.777   3rd Qu.:39.54   3rd Qu.:6.018  
##  Max.   :11.300   Max.   :75.59   Max.   :7.640
# Memeriksa jumlah observasi
nrow(dataset)
## [1] 34
#A.Ekplorasi Data Peta
sf_use_s2(FALSE)
## Spherical geometry (s2) switched off
Sys.setenv(SHAPE_RESTORE_SHX = "YES") 
peta <- st_read("C:\\Users\\LENOVO\\Downloads\\Shapefile_34_Provinsi_IPTIK\\Provinsi_34\\Provinsi_34.shp")
## Reading layer `Provinsi_34' from data source 
##   `C:\Users\LENOVO\Downloads\Shapefile_34_Provinsi_IPTIK\Provinsi_34\Provinsi_34.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 34 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 95.01079 ymin: -11.00762 xmax: 141.0194 ymax: 6.07693
## Geodetic CRS:  WGS 84
peta <- st_make_valid(peta)
print(names(peta))      
## [1] "Provinsi" "geometry"
nrow(peta)
## [1] 34
nrow(dataset)
## [1] 34
peta <- cbind(peta, dataset)

peta$y  <- dataset$y
peta$x1 <- dataset$x1
peta$x2 <- dataset$x2
peta$x3 <- dataset$x3
peta$x4 <- dataset$x4
peta$x5 <- dataset$x5

judul <- c(y  = "Sebaran Peubah Respon (Y)",
           x1 = "Sebaran Peubah X1",
           x2 = "Sebaran Peubah X2",
           x3 = "Sebaran Peubah X3",
           x4 = "Sebaran Peubah X4",
           x5 = "Sebaran Peubah X5")

for (v in names(judul)) {
  gambar <- ggplot(peta) +
    geom_sf(aes(fill = .data[[v]]), colour = "red", linewidth = 0.2) +
    scale_fill_gradient(
      low = "green",   # Nilai rendah: hijau
      high = "blue",  # Nilai tinggi: biru
      name = toupper(v)) +
    labs(title = judul[[v]], subtitle = "Provinsi di Indonesia") +
    theme_minimal() +
    theme(legend.position = "bottom")
  print(gambar)
  ggsave(paste0("Peta_", toupper(v), ".png"), gambar,
         width = 10, height = 5, dpi = 300, bg = "white")
}

# ==============================================================================
# B. MODEL REGRESI LINEAR KLASIK (OLS) & UJI ASUMSI KLASIK
# ==============================================================================
form_model <- y ~ x1 + x2 + x3 + x4 + x5
model_ols <- lm(form_model, data = peta)
summary(model_ols)
## 
## Call:
## lm(formula = form_model, data = peta)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.18443 -0.12203  0.04992  0.16147  0.43301 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.152131   1.840463   3.343  0.00237 ** 
## x1           0.107505   0.021833   4.924 3.42e-05 ***
## x2          -0.999792   0.743125  -1.345  0.18929    
## x3          -0.342037   0.148939  -2.296  0.02934 *  
## x4           0.199139   0.110467   1.803  0.08221 .  
## x5          -0.002659   0.005886  -0.452  0.65487    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3097 on 28 degrees of freedom
## Multiple R-squared:  0.8332, Adjusted R-squared:  0.8034 
## F-statistic: 27.97 on 5 and 28 DF,  p-value: 4.488e-10
# Uji Multikolinearitas (VIF < 10 umumnya menandakan tidak ada multikolinearitas serius)
vif(model_ols)
##       x1       x2       x3       x4       x5 
## 6.283402 5.265240 1.886887 2.805523 1.437699
# Uji Normalitas Residual (Shapiro-Wilk)
shapiro.test(residuals(model_ols))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model_ols)
## W = 0.82457, p-value = 7.947e-05
# Uji Homoskedastisitas Residual (Breusch-Pagan)
bptest(model_ols)
## 
##  studentized Breusch-Pagan test
## 
## data:  model_ols
## BP = 8.8741, df = 5, p-value = 0.1142
# ==============================================================================
# C. PEMBOBOT SPASIAL & UJI EFEK SPASIAL (MORAN'S I & LAGRANGE MULTIPLIER)
# ==============================================================================
# Matriks ketetanggaan Queen Contiguity & bobot row-standardized ("W")
nb_queen <- poly2nb(peta, queen = TRUE)
## although coordinates are longitude/latitude, st_intersects assumes that they
## are planar
## Warning in poly2nb(peta, queen = TRUE): some observations have no neighbours;
## if this seems unexpected, try increasing the snap argument.
## Warning in poly2nb(peta, queen = TRUE): neighbour object has 11 sub-graphs;
## if this sub-graph count seems unexpected, try increasing the snap argument.
lw_w     <- nb2listw(nb_queen, style = "W", zero.policy = TRUE)

# Cek wilayah tanpa tetangga (jika ada)
summary(nb_queen)
## Neighbour list object:
## Number of regions: 34 
## Number of nonzero links: 66 
## Percentage nonzero weights: 5.709343 
## Average number of links: 1.941176 
## 6 regions with no links:
## 2, 17, 20, 21, 22, 23
## 11 disjoint connected subgraphs
## Link number distribution:
## 
## 0 1 2 3 4 5 
## 6 8 8 7 4 1 
## 8 least connected regions:
## 1 5 11 16 18 24 25 31 with 1 link
## 1 most connected region:
## 8 with 5 links
# Uji autokorelasi spasial pada peubah respon Y
moran.test(peta$y, lw_w, zero.policy = TRUE)
## 
##  Moran I test under randomisation
## 
## data:  peta$y  
## weights: lw_w  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 2.2163, p-value = 0.01334
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.30903492       -0.03703704        0.02438329
# Uji autokorelasi spasial pada residual model OLS
lm.morantest(model_ols, lw_w, zero.policy = TRUE)
## 
##  Global Moran I for regression residuals
## 
## data:  
## model: lm(formula = form_model, data = peta)
## weights: lw_w
## 
## Moran I statistic standard deviate = 1.5634, p-value = 0.05898
## alternative hypothesis: greater
## sample estimates:
## Observed Moran I      Expectation         Variance 
##       0.25184455      -0.03559486       0.03380352
# Uji Lagrange Multiplier (LMerr, LMlag, RLMerr, RLMlag, SARMA)
lm_tests <- lm.RStests(model_ols, lw_w, test = "all", zero.policy = TRUE)
summary(lm_tests)
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## data:  
## model: lm(formula = form_model, data = peta)
## test weights: lw_w
##  
##          statistic parameter p.value  
## RSerr      2.65305         1 0.10335  
## RSlag      0.78218         1 0.37648  
## adjRSerr   3.03015         1 0.08173 .
## adjRSlag   1.15928         1 0.28162  
## SARMA      3.81233         2 0.14865  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# ==============================================================================
# D. PEMODELAN REGRESI SPASIAL (SAR / SEM / SDM)
# ==============================================================================
# Spatial Autoregressive Model (SAR / Spatial Lag)
model_sar <- lagsarlm(form_model, data = peta, listw = lw_w, zero.policy = TRUE)
summary(model_sar)
## 
## Call:
## lagsarlm(formula = form_model, data = peta, listw = lw_w, zero.policy = TRUE)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -1.156291 -0.113983  0.010272  0.163930  0.450875 
## 
## Type: lag 
## Regions with no neighbours included:
##  2 17 20 21 22 23 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  6.4364700  1.6797849  3.8317 0.0001272
## x1           0.1089274  0.0196350  5.5476 2.896e-08
## x2          -1.0614288  0.6707248 -1.5825 0.1135331
## x3          -0.3504978  0.1338784 -2.6180 0.0088439
## x4           0.1957957  0.0991057  1.9756 0.0481973
## x5          -0.0024178  0.0052854 -0.4574 0.6473550
## 
## Rho: -0.018831, LR test value: 0.77842, p-value: 0.37762
## Asymptotic standard error: 0.02146
##     z-value: -0.87748, p-value: 0.38023
## Wald statistic: 0.76997, p-value: 0.38023
## 
## Log likelihood: -4.697389 for lag model
## ML residual variance (sigma squared): 0.077175, (sigma: 0.2778)
## Number of observations: 34 
## Number of parameters estimated: 8 
## AIC: 25.395, (AIC for lm: 24.173)
## LM test for residual autocorrelation
## test value: 3.4203, p-value: 0.064398
# Spatial Error Model (SEM)
model_sem <- errorsarlm(form_model, data = peta, listw = lw_w, zero.policy = TRUE)
summary(model_sem)
## 
## Call:
## errorsarlm(formula = form_model, data = peta, listw = lw_w, zero.policy = TRUE)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -1.112619 -0.134149  0.055451  0.148096  0.424952 
## 
## Type: error 
## Regions with no neighbours included:
##  2 17 20 21 22 23 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  5.7446182  1.5293461  3.7563 0.0001725
## x1           0.1020643  0.0193772  5.2672 1.385e-07
## x2          -1.0290919  0.6205902 -1.6582 0.0972676
## x3          -0.3101707  0.1308044 -2.3713 0.0177277
## x4           0.2567413  0.0924830  2.7761 0.0055017
## x5          -0.0042883  0.0052546 -0.8161 0.4144369
## 
## Lambda: 0.26939, LR test value: 2.449, p-value: 0.1176
## Asymptotic standard error: 0.16988
##     z-value: 1.5858, p-value: 0.11278
## Wald statistic: 2.5148, p-value: 0.11278
## 
## Log likelihood: -3.862113 for error model
## ML residual variance (sigma squared): 0.071494, (sigma: 0.26738)
## Number of observations: 34 
## Number of parameters estimated: 8 
## AIC: 23.724, (AIC for lm: 24.173)
# Spatial Durbin Model (SDM) - opsional bila ingin menguji lag prediktor juga
model_sdm <- lagsarlm(form_model, data = peta, listw = lw_w, Durbin = TRUE, zero.policy = TRUE)
summary(model_sdm)
## 
## Call:lagsarlm(formula = form_model, data = peta, listw = lw_w, Durbin = TRUE, 
##     zero.policy = TRUE)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -1.003672 -0.096589 -0.007839  0.095109  0.513133 
## 
## Type: mixed 
## Regions with no neighbours included:
##  2 17 20 21 22 23 
## Coefficients: (asymptotic standard errors) 
##                Estimate  Std. Error z value  Pr(>|z|)
## (Intercept)  6.64328746  1.55447697  4.2736 1.923e-05
## x1           0.10406072  0.01932580  5.3846 7.263e-08
## x2          -1.36307671  0.66393030 -2.0530  0.040069
## x3          -0.32631963  0.12485364 -2.6136  0.008959
## x4           0.26175838  0.09832613  2.6621  0.007764
## x5          -0.00065671  0.00575136 -0.1142  0.909092
## lag.x1       0.00097136  0.01710796  0.0568  0.954722
## lag.x2       0.21634382  0.53857179  0.4017  0.687905
## lag.x3      -0.25784549  0.26782538 -0.9627  0.335679
## lag.x4      -0.09250357  0.13897611 -0.6656  0.505662
## lag.x5       0.00779949  0.00699921  1.1143  0.265134
## 
## Rho: 0.13746, LR test value: 0.63009, p-value: 0.42732
## Asymptotic standard error: 0.16707
##     z-value: 0.82278, p-value: 0.41063
## Wald statistic: 0.67696, p-value: 0.41063
## 
## Log likelihood: -0.723965 for mixed model
## ML residual variance (sigma squared): 0.060678, (sigma: 0.24633)
## Number of observations: 34 
## Number of parameters estimated: 13 
## AIC: 27.448, (AIC for lm: 26.078)
## LM test for residual autocorrelation
## test value: 0.017573, p-value: 0.89454
# ==============================================================================
# PERBANDINGAN MODEL
# ==============================================================================
AIC(model_ols, model_sar, model_sem, model_sdm)
##           df      AIC
## model_ols  7 24.17320
## model_sar  8 25.39478
## model_sem  8 23.72423
## model_sdm 13 27.44793
logLik(model_ols)
## 'log Lik.' -5.0866 (df=7)
logLik(model_sar)
## 'log Lik.' -4.697389 (df=8)
logLik(model_sem)
## 'log Lik.' -3.862113 (df=8)
logLik(model_sdm)
## 'log Lik.' -0.723965 (df=13)
# Uji Likelihood Ratio SAR terhadap OLS
LR_SAR <- 2 * (
  as.numeric(logLik(model_sar)) -
  as.numeric(logLik(model_ols))
)

df_SAR <- attr(logLik(model_sar), "df") -
          attr(logLik(model_ols), "df")

p_SAR <- pchisq(LR_SAR, df = df_SAR, lower.tail = FALSE)

# Uji Likelihood Ratio SEM terhadap OLS
LR_SEM <- 2 * (
  as.numeric(logLik(model_sem)) -
  as.numeric(logLik(model_ols))
)

df_SEM <- attr(logLik(model_sem), "df") -
          attr(logLik(model_ols), "df")

p_SEM <- pchisq(LR_SEM, df = df_SEM, lower.tail = FALSE)

# Menampilkan hasil
cat("LR SAR vs OLS:", LR_SAR, "\n")
## LR SAR vs OLS: 0.7784221
cat("p-value SAR:", p_SAR, "\n\n")
## p-value SAR: 0.3776242
cat("LR SEM vs OLS:", LR_SEM, "\n")
## LR SEM vs OLS: 2.448973
cat("p-value SEM:", p_SEM, "\n")
## p-value SEM: 0.1176018
# Moran's I residual model SAR
moran.test(
  residuals(model_sar),
  lw_w,
  zero.policy = TRUE
)
## 
##  Moran I test under randomisation
## 
## data:  residuals(model_sar)  
## weights: lw_w  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 2.1172, p-value = 0.01712
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.28413727       -0.03703704        0.02301158
# Moran's I residual model SEM
moran.test(
  residuals(model_sem),
  lw_w,
  zero.policy = TRUE
)
## 
##  Moran I test under randomisation
## 
## data:  residuals(model_sem)  
## weights: lw_w  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 0.11936, p-value = 0.4525
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       -0.01879231       -0.03703704        0.02336638

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.