

#Pemilih Prabowo Gibran
#Korelasi Spasial_Moran
# Nilai variabel y untuk tiap area (A–F)
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')`
## Loading required package: sf
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
set.seed(231)
x <- c("Jaktim", "Jaksel", "Jakpus", "Jakbar", "Jakut", "Kep")
y <- c(568344, 394021, 211166, 490359, 283641, 8972)
# Matriks ketetanggaan berdasarkan rook contiguity
W_rook <- matrix(c(
0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 0, 0,
1, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 1, 0
), nrow = 6, byrow = TRUE)
# Standardisasi baris
row_sums <- rowSums(W_rook)
W_std <- sweep(W_rook, 1, row_sums, FUN = "/")
# Hitung Moran's I
N <- length(y)
y_bar <- mean(y)
numerator <- 0
for (i in 1:N) {
for (j in 1:N) {
numerator <- numerator + W_std[i,j] * (y[i] - y_bar) * (y[j] - y_bar)
}
}
denominator <- sum((y - y_bar)^2)
moran_I <- (N / sum(W_std)) * (numerator / denominator)
cat("Indeks Moran (rook contiguity):", round(moran_I, 3), "\n")
## Indeks Moran (rook contiguity): -0.02
#Alternatif menghitung global moran
GlobalMoran<-moran.test(y, mat2listw(W_std,style = "W"),
randomisation = F)
GlobalMoran
##
## Moran I test under normality
##
## data: y
## weights: mat2listw(W_std, style = "W")
##
## Moran I statistic standard deviate = 0.80369, p-value = 0.2108
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## -0.02038475 -0.20000000 0.04994709
#Menghitung Local Moran (LISA)
LocalMoran<-localmoran(y,
mat2listw(W_std, style = "W"))
round(LocalMoran[1:nrow(LocalMoran),],4)
## Ii E.Ii Var.Ii Z.Ii Pr(z != E(Ii))
## 1 -0.2105 -0.3421 0.2251 0.2775 0.7814
## 2 0.1925 -0.0269 0.0262 1.3558 0.1751
## 3 -0.3618 -0.0770 0.0266 -1.7446 0.0811
## 4 -0.1427 -0.1573 0.1326 0.0401 0.9680
## 5 0.0079 -0.0105 0.0039 0.2945 0.7684
## 6 0.3923 -0.5862 1.4554 0.8111 0.4173
#Plot Kuadran untuk menemukan Hotspot
moran <- moran.plot(as.vector(scale(y)),
mat2listw(W_std, style="W"),
xlab = "Xi",
ylab = "WXj",
main = "Moran Scatterplot",
labels = x)
text(moran,x,cex=1, col="blue")

#Pemilih Anies Muhaimin
#Korelasi Spasial_Moran
# Nilai variabel y untuk tiap area (A–F)
library(spdep)
set.seed(231)
y <- c(626451, 482155, 220707, 395486, 203566, 7043)
# Matriks ketetanggaan berdasarkan rook contiguity
W_rook <- matrix(c(
0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 0, 0,
1, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 1, 0
), nrow = 6, byrow = TRUE)
# Standardisasi baris
row_sums <- rowSums(W_rook)
W_std <- sweep(W_rook, 1, row_sums, FUN = "/")
# Hitung Moran's I
N <- length(y)
y_bar <- mean(y)
numerator <- 0
for (i in 1:N) {
for (j in 1:N) {
numerator <- numerator + W_std[i,j] * (y[i] - y_bar) * (y[j] - y_bar)
}
}
denominator <- sum((y - y_bar)^2)
moran_I <- (N / sum(W_std)) * (numerator / denominator)
cat("Indeks Moran (rook contiguity):", round(moran_I, 3), "\n")
## Indeks Moran (rook contiguity): 0.142
#Alternatif menghitung global moran
GlobalMoran<-moran.test(y, mat2listw(W_std,style = "W"),
randomisation = F)
GlobalMoran
##
## Moran I test under normality
##
## data: y
## weights: mat2listw(W_std, style = "W")
##
## Moran I statistic standard deviate = 1.5294, p-value = 0.06308
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.14179914 -0.20000000 0.04994709
#Menghitung Local Moran (LISA)
LocalMoran<-localmoran(y,
mat2listw(W_std, style = "W"))
round(LocalMoran[1:nrow(LocalMoran),],4)
## Ii E.Ii Var.Ii Z.Ii Pr(z != E(Ii))
## 1 -0.1506 -0.4482 0.2473 0.5984 0.5496
## 2 0.3550 -0.1236 0.1083 1.4540 0.1459
## 3 -0.2580 -0.0504 0.0179 -1.5501 0.1211
## 4 -0.0361 -0.0258 0.0251 -0.0652 0.9480
## 5 0.0293 -0.0687 0.0240 0.6328 0.5269
## 6 0.9113 -0.4832 1.4983 1.1393 0.2546
#Plot Kuadran untuk menemukan Hotspot
moran <- moran.plot(as.vector(scale(y)),
mat2listw(W_std, style="W"),
xlab = "Xi",
ylab = "WXj",
main = "Moran Scatterplot",
labels = x)
text(moran,x,cex=1, col="blue")

#===Pemilih Ganjar Mahmud
#Korelasi Spasial_Moran
# Nilai variabel y untuk tiap area (A–F)
library(spdep)
set.seed(231)
y <- c(208899, 165228, 95576, 233594, 119301, 2336)
# Matriks ketetanggaan berdasarkan rook contiguity
W_rook <- matrix(c(
0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 0, 0,
1, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0,
1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 1, 0
), nrow = 6, byrow = TRUE)
# Standardisasi baris
row_sums <- rowSums(W_rook)
W_std <- sweep(W_rook, 1, row_sums, FUN = "/")
# Hitung Moran's I
N <- length(y)
y_bar <- mean(y)
numerator <- 0
for (i in 1:N) {
for (j in 1:N) {
numerator <- numerator + W_std[i,j] * (y[i] - y_bar) * (y[j] - y_bar)
}
}
denominator <- sum((y - y_bar)^2)
moran_I <- (N / sum(W_std)) * (numerator / denominator)
cat("Indeks Moran (rook contiguity):", round(moran_I, 3), "\n")
## Indeks Moran (rook contiguity): 0
#Alternatif menghitung global moran
GlobalMoran<-moran.test(y, mat2listw(W_std,style = "W"),
randomisation = F)
GlobalMoran
##
## Moran I test under normality
##
## data: y
## weights: mat2listw(W_std, style = "W")
##
## Moran I statistic standard deviate = 0.89497, p-value = 0.1854
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 1.599495e-05 -2.000000e-01 4.994709e-02
#Menghitung Local Moran (LISA)
LocalMoran<-localmoran(y,
mat2listw(W_std, style = "W"))
round(LocalMoran[1:nrow(LocalMoran),],4)
## Ii E.Ii Var.Ii Z.Ii Pr(z != E(Ii))
## 1 -0.1303 -0.1726 0.1428 0.1117 0.9110
## 2 0.1965 -0.0260 0.0254 1.3975 0.1623
## 3 -0.3139 -0.0595 0.0210 -1.7575 0.0788
## 4 -0.1754 -0.3126 0.2149 0.2959 0.7673
## 5 0.0073 -0.0112 0.0042 0.2878 0.7735
## 6 0.4159 -0.6182 1.4162 0.8690 0.3849
#Plot Kuadran untuk menemukan Hotspot
moran <- moran.plot(as.vector(scale(y)),
mat2listw(W_std, style="W"),
xlab = "Xi",
ylab = "WXj",
main = "Moran Scatterplot",
labels = x)
text(moran,x,cex=1, col="blue")


# === Optimasi Produksi PT LAQUNATEKSTIL ===
# Dua produk: kain sutera (x) dan kain wol (y)
# Maksimalkan laba: Z = 40x + 30y
# Kendala bahan baku:
# Lokasi S : 2x + 3y ≤ 60
# Lokasi W : 2y ≤ 30
# Lokasi K : 2x + y ≤ 40
# x, y ≥ 0
# --- 1. Load library ---
library(lpSolve)
# --- 2. Koefisien fungsi tujuan ---
obj <- c(40, 30) # koefisien laba (dalam juta)
# --- 3. Matriks kendala ---
A <- matrix(c(
2, 3, # Lokasi S
0, 2, # Lokasi W
2, 1 # Lokasi K
), nrow = 3, byrow = TRUE)
# --- 4. Tanda ketidaksamaan dan RHS ---
dir <- c("<=", "<=", "<=")
rhs <- c(60, 30, 40)
# --- 5. Menyelesaikan LP (maksimasi) ---
sol <- lp(direction = "max",
objective.in = obj,
const.mat = A,
const.dir = dir,
const.rhs = rhs)
# --- 6. Tampilkan hasil ---
cat("Status solusi :", sol$status, "\n") # 0 berarti optimal
## Status solusi : 0
cat("Laba maksimum (juta) :", sol$objval, "\n")
## Laba maksimum (juta) : 900
cat("Produksi optimal:\n")
## Produksi optimal:
cat(" Kain Sutera (x) =", sol$solution[1], "unit\n")
## Kain Sutera (x) = 15 unit
cat(" Kain Wol (y) =", sol$solution[2], "unit\n\n")
## Kain Wol (y) = 10 unit
# --- 7. Hitung pemakaian & sisa bahan ---
x <- sol$solution[1]
y <- sol$solution[2]
S_used <- 2*x + 3*y
W_used <- 2*y
K_used <- 2*x + y
S_rem <- 60 - S_used
W_rem <- 30 - W_used
K_rem <- 40 - K_used
cat("Pemakaian & sisa bahan baku (kg):\n")
## Pemakaian & sisa bahan baku (kg):
cat(sprintf(" Lokasi S: terpakai %.1f, sisa %.1f\n", S_used, S_rem))
## Lokasi S: terpakai 60.0, sisa -0.0
cat(sprintf(" Lokasi W: terpakai %.1f, sisa %.1f\n", W_used, W_rem))
## Lokasi W: terpakai 20.0, sisa 10.0
cat(sprintf(" Lokasi K: terpakai %.1f, sisa %.1f\n", K_used, K_rem))
## Lokasi K: terpakai 40.0, sisa 0.0

# Data
df <- data.frame(
id = 1:10,
x = c(779892, 777940, 776421, 775376, 774000, 771986, 768683, 765140, 769396, 772274),
y = c(9455266, 9455100, 9454924, 9454760, 9454570, 9454374, 9460956, 9465610, 9473570, 9473296),
TSS = c(43, 11, 7, 13, 6, 3, 2, 2, 2, 4)
)
x0 <- 773610
y0 <- 9466084
# Euclidean distance (meter)
dist <- sqrt( (df$x - x0)^2 + (df$y - y0)^2 )
df$dist <- dist
# --- Nearest Neighbor ---
nn_idx <- which.min(df$dist)
TSS_NN <- df$TSS[nn_idx]
# --- IDW (p = 2 by default) ---
idw <- function(z, d, p = 2) {
w <- 1 / (d^p)
sum(w * z) / sum(w)
}
TSS_IDW_p2 <- idw(df$TSS, df$dist, p = 2)
# (opsional) nilai untuk p lain
TSS_IDW_p1 <- idw(df$TSS, df$dist, p = 1)
TSS_IDW_p3 <- idw(df$TSS, df$dist, p = 3)
# Output ringkas
cat(sprintf("Nearest Neighbor: titik %d, TSS = %.3f Mg/l\n", df$id[nn_idx], TSS_NN))
## Nearest Neighbor: titik 7, TSS = 2.000 Mg/l
cat(sprintf("IDW p=2: TSS = %.3f Mg/l\n", TSS_IDW_p2))
## IDW p=2: TSS = 6.784 Mg/l
cat(sprintf("IDW p=1: TSS = %.3f Mg/l | IDW p=3: TSS = %.3f Mg/l\n",
TSS_IDW_p1, TSS_IDW_p3))
## IDW p=1: TSS = 8.010 Mg/l | IDW p=3: TSS = 5.704 Mg/l

# ----- Data -----
df <- data.frame(
X = c(61,63,64,68,71,73,75),
Y = c(139,140,129,128,140,141,128),
V = c(477,696,227,646,606,791,783)
)
A <- c(65, 137)
# ----- Fungsi kovarian -----
Cfun <- function(h) 8 * exp(-0.4 * h)
# ----- Matriks jarak antarsampel & ke A -----
D <- as.matrix(dist(df[, c("X","Y")], method = "euclidean", diag = TRUE, upper = TRUE))
dA <- sqrt((df$X - A[1])^2 + (df$Y - A[2])^2)
# ----- Matriks kovarian K dan vektor k (ke A) -----
K <- Cfun(D) # 7x7
k <- Cfun(dA) # 7x1
# ----- Sistem Ordinary Kriging: [K 1; 1^T 0][lambda; mu] = [k; 1] -----
n <- nrow(K)
Aok <- rbind(cbind(K, rep(1, n)), c(rep(1, n), 0))
bok <- c(k, 1)
sol <- solve(Aok, bok)
lambda <- sol[1:n] # bobot
mu <- sol[n + 1] # Lagrange multiplier
# ----- Prediksi & (opsional) ragam kriging -----
Vhat <- sum(lambda * df$V)
sigma2 <- 8 - sum(lambda * k) + mu
# ----- Tampilkan hasil -----
cat("Bobot (lambda):", round(lambda, 3), " | Jumlah =", round(sum(lambda), 3), "\n")
## Bobot (lambda): 0.165 0.276 0.128 0.104 0.135 0.081 0.111 | Jumlah = 1
cat(sprintf("Prediksi V(A) = %.2f\n", Vhat))
## Prediksi V(A) = 600.01
cat(sprintf("Ragam kriging (opsional) = %.3f\n", sigma2))
## Ragam kriging (opsional) = 6.173