stmmlib 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stmmlib/__init__.py +30 -0
- stmmlib/_backend.py +153 -0
- stmmlib/_utils.py +175 -0
- stmmlib/beta/__init__.py +17 -0
- stmmlib/beta/config.py +108 -0
- stmmlib/beta/criteria.py +247 -0
- stmmlib/beta/design.py +222 -0
- stmmlib/beta/diagnose.py +168 -0
- stmmlib/beta/diagnostics.py +94 -0
- stmmlib/beta/fit.py +598 -0
- stmmlib/beta/gpu_solvers.py +169 -0
- stmmlib/beta/inference.py +403 -0
- stmmlib/beta/lrt_fixed.py +551 -0
- stmmlib/beta/moran_panel.py +733 -0
- stmmlib/beta/plot_data.py +252 -0
- stmmlib/beta/precision.py +98 -0
- stmmlib/beta/report.py +373 -0
- stmmlib/beta/result.py +696 -0
- stmmlib/beta/simulation.py +343 -0
- stmmlib/beta/solvers.py +2021 -0
- stmmlib/beta/structure.py +56 -0
- stmmlib/diagnostics_suite.py +663 -0
- stmmlib/gaussian/__init__.py +1 -0
- stmmlib/gaussian/blup.py +252 -0
- stmmlib/gaussian/covariance.py +599 -0
- stmmlib/gaussian/design.py +312 -0
- stmmlib/gaussian/diagnostics.py +308 -0
- stmmlib/gaussian/gls.py +285 -0
- stmmlib/gaussian/hessian.py +197 -0
- stmmlib/gaussian/lrt_fixed.py +289 -0
- stmmlib/gaussian/models.py +528 -0
- stmmlib/gaussian/optimization.py +361 -0
- stmmlib/gaussian/simulation.py +186 -0
- stmmlib/gaussian/summary.py +277 -0
- stmmlib/gaussian/transforms.py +419 -0
- stmmlib/gaussian/variance_decomp.py +191 -0
- stmmlib/lrt.py +74 -0
- stmmlib/output_format.py +148 -0
- stmmlib/output_format_beta.py +299 -0
- stmmlib/output_format_normal.py +118 -0
- stmmlib/registry.py +326 -0
- stmmlib/stmm.py +265 -0
- stmmlib-0.2.0.dist-info/METADATA +215 -0
- stmmlib-0.2.0.dist-info/RECORD +47 -0
- stmmlib-0.2.0.dist-info/WHEEL +5 -0
- stmmlib-0.2.0.dist-info/licenses/LICENSE +21 -0
- stmmlib-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
"""Uji Moran panel tiga pendekatan untuk residual model STMM Beta.
|
|
2
|
+
|
|
3
|
+
P1 (moran_current) : Moran per tahun pada residual respons (y - mu)
|
|
4
|
+
kondisional, p-value via permutasi.
|
|
5
|
+
P2 (moran_literature) : Moran per tahun pada residual Dunn-Smyth
|
|
6
|
+
kondisional, ekspektasi/varians analitik
|
|
7
|
+
Cliff-Ord (S0, S1, S2), kombinasi Fisher +
|
|
8
|
+
Stouffer + Bonferroni.
|
|
9
|
+
P3 (moran_panel_derived) : Moran panel pooled (W_panel = kron(I_T, W)),
|
|
10
|
+
p-value via block permutation, Stouffer-Hartung
|
|
11
|
+
dengan koreksi korelasi antar tahun, bootstrap
|
|
12
|
+
CI dengan resample area.
|
|
13
|
+
|
|
14
|
+
Semua fungsi publik menerima W=None. Bila efek acak area IID dan W tidak
|
|
15
|
+
diberikan, Moran tidak terdefinisi dan dict skipped dikembalikan.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
from scipy import stats
|
|
22
|
+
|
|
23
|
+
from stmmlib.beta.structure import stmm_beta_structure
|
|
24
|
+
from stmmlib.output_format import _sig
|
|
25
|
+
|
|
26
|
+
# ---------------------------------------------------------------------------
|
|
27
|
+
# BAGIAN 0: konstanta default
|
|
28
|
+
# ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
DEFAULT_N_PERM = 999
|
|
31
|
+
DEFAULT_N_BOOT = 999
|
|
32
|
+
DEFAULT_SEED = 42
|
|
33
|
+
|
|
34
|
+
_BAR = "-" * 60
|
|
35
|
+
|
|
36
|
+
_SKIPPED_REASON = (
|
|
37
|
+
"efek acak area IID: Moran tidak terdefinisi tanpa W; "
|
|
38
|
+
"berikan W eksternal untuk uji pembanding, atau gunakan Ljung-Box"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# ---------------------------------------------------------------------------
|
|
43
|
+
# BAGIAN 1: kernel Moran
|
|
44
|
+
# ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
def _center(v):
|
|
47
|
+
"""Pusatkan vektor residual (kurangi rata-rata)."""
|
|
48
|
+
v = np.asarray(v, dtype=float)
|
|
49
|
+
return v - np.nanmean(v)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _moran_I(v_c, W):
|
|
53
|
+
"""Moran's I dari vektor yang sudah terpusat dan matriks bobot W.
|
|
54
|
+
|
|
55
|
+
Guard: bila S0 = sum(W) = 0 (semua baris W nol) raise ValueError.
|
|
56
|
+
"""
|
|
57
|
+
v_c = np.asarray(v_c, dtype=float)
|
|
58
|
+
m = v_c.size
|
|
59
|
+
S0 = float(np.sum(W))
|
|
60
|
+
if S0 <= 0:
|
|
61
|
+
raise ValueError("semua baris W nol: Moran tidak terdefinisi")
|
|
62
|
+
den = float(v_c @ v_c)
|
|
63
|
+
if not np.isfinite(den) or den <= 0:
|
|
64
|
+
return float("nan")
|
|
65
|
+
lag = np.asarray(W, dtype=float) @ v_c
|
|
66
|
+
return float((m / S0) * float(v_c @ lag) / den)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _EI_var(W, k):
|
|
70
|
+
"""E[I] dan Var[I] rumus Cliff-Ord (asumsi normalitas, randomisasi).
|
|
71
|
+
|
|
72
|
+
E[I] = -1/(m-k) (koreksi derajat bebas efek tetap; k=0 memberi
|
|
73
|
+
-1/(m-1) standar). Var[I] = (m^2 S1 - m S2 + 3 S0^2)/(S0^2 (m^2-1))
|
|
74
|
+
- E[I]^2, dengan S1 = 1/2 sum_ij (w_ij + w_ji)^2 dan
|
|
75
|
+
S2 = sum_k (w_k. + w_.k)^2. Baris W nol tetap ikut dihitung.
|
|
76
|
+
"""
|
|
77
|
+
W = np.asarray(W, dtype=float)
|
|
78
|
+
m = W.shape[0]
|
|
79
|
+
S0 = float(np.sum(W))
|
|
80
|
+
S1 = 0.5 * float(np.sum((W + W.T) ** 2))
|
|
81
|
+
S2 = float(np.sum((W.sum(axis=1) + W.sum(axis=0)) ** 2))
|
|
82
|
+
if m - k <= 1 or S0 <= 0:
|
|
83
|
+
return float("nan"), float("nan")
|
|
84
|
+
e_i = -1.0 / (m - k)
|
|
85
|
+
num = m * m * S1 - m * S2 + 3.0 * S0 * S0
|
|
86
|
+
den = S0 * S0 * (m * m - 1.0)
|
|
87
|
+
return float(e_i), float(num / den - e_i ** 2)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _perm_p(stat, stats_perm, sisi="dua", center=None):
|
|
91
|
+
"""P-value permutasi dua sisi terhadap pusat distribusi permutasi.
|
|
92
|
+
Pusat empiris c = mean(stats_perm) bila center=None."""
|
|
93
|
+
stats_perm = np.asarray(stats_perm, dtype=float)
|
|
94
|
+
stats_perm = stats_perm[np.isfinite(stats_perm)]
|
|
95
|
+
if stats_perm.size == 0:
|
|
96
|
+
return float("nan")
|
|
97
|
+
if sisi == "besar":
|
|
98
|
+
cnt = int(np.sum(stats_perm >= stat))
|
|
99
|
+
elif sisi == "kecil":
|
|
100
|
+
cnt = int(np.sum(stats_perm <= stat))
|
|
101
|
+
else:
|
|
102
|
+
c = float(np.mean(stats_perm)) if center is None else float(center)
|
|
103
|
+
cnt = int(np.sum(np.abs(stats_perm - c) >= abs(float(stat) - c)))
|
|
104
|
+
return float((cnt + 1) / (stats_perm.size + 1))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# ---------------------------------------------------------------------------
|
|
108
|
+
# BAGIAN 2 (internal): ekstraksi fit, guard IID, agregasi residual
|
|
109
|
+
# ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
def _iid_area(fit) -> bool:
|
|
112
|
+
"""True bila efek acak area model IID (termasuk nested IID)."""
|
|
113
|
+
model = (getattr(fit, "_opt", None) or {}).get("model", "")
|
|
114
|
+
try:
|
|
115
|
+
s = stmm_beta_structure(model)
|
|
116
|
+
except KeyError:
|
|
117
|
+
return False
|
|
118
|
+
return s["area"] in ("iid", "nested_iid")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _resolve_W(fit, W):
|
|
122
|
+
"""W efektif: argumen lebih dulu, lalu W tersimpan di fit._opt."""
|
|
123
|
+
if W is None:
|
|
124
|
+
W = (getattr(fit, "_opt", None) or {}).get("W")
|
|
125
|
+
if W is None:
|
|
126
|
+
return None
|
|
127
|
+
if hasattr(W, "toarray"):
|
|
128
|
+
W = W.toarray()
|
|
129
|
+
return np.asarray(W, dtype=float)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _setup(fit, W):
|
|
133
|
+
"""Kembalikan (y, mu, resid_respons, des, k, m, T, area_idx, time_idx, Wd)
|
|
134
|
+
atau dict skipped bila guard IID terpicu."""
|
|
135
|
+
if _iid_area(fit) and W is None:
|
|
136
|
+
return {"skipped": True, "reason": _SKIPPED_REASON}
|
|
137
|
+
Wd = _resolve_W(fit, W)
|
|
138
|
+
if Wd is None:
|
|
139
|
+
raise ValueError("W tidak tersedia: berikan W atau fit dengan W tersimpan")
|
|
140
|
+
opt = fit._opt
|
|
141
|
+
y = np.asarray(opt["y"], dtype=float)
|
|
142
|
+
mu = opt.get("mu_hat")
|
|
143
|
+
if mu is None:
|
|
144
|
+
mu = 1.0 / (1.0 + np.exp(-(np.asarray(opt["X"]) @ fit.beta)))
|
|
145
|
+
mu = np.clip(np.asarray(mu, dtype=float), 1e-15, 1.0 - 1e-15)
|
|
146
|
+
des = opt.get("des", {}) or {}
|
|
147
|
+
m = int(des.get("m", 1))
|
|
148
|
+
T = int(des.get("T", 1))
|
|
149
|
+
k = int(np.asarray(opt["X"]).shape[1]) if opt.get("X") is not None else 0
|
|
150
|
+
area_idx = np.asarray(
|
|
151
|
+
des.get("area_idx", np.zeros(y.size, dtype=int)), dtype=int)
|
|
152
|
+
time_idx = np.asarray(
|
|
153
|
+
des.get("time_idx", np.zeros(y.size, dtype=int)), dtype=int)
|
|
154
|
+
resid = y - mu
|
|
155
|
+
return {"y": y, "mu": mu, "resid": resid, "des": des, "k": k, "m": m,
|
|
156
|
+
"T": T, "area_idx": area_idx, "time_idx": time_idx, "W": Wd}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _resid_area_t(resid_t, area_t, m):
|
|
160
|
+
"""Agregasi residual per area dalam satu tahun: rata-rata bincount."""
|
|
161
|
+
cnt = np.bincount(area_t, minlength=m).astype(float)
|
|
162
|
+
cnt[cnt == 0] = 1.0
|
|
163
|
+
return np.bincount(area_t, weights=np.nan_to_num(resid_t),
|
|
164
|
+
minlength=m) / cnt
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _resid_matrix(resid, area_idx, time_idx, m, T):
|
|
168
|
+
"""Matriks residual m x T: R[a, t] = rata-rata residual area a tahun t."""
|
|
169
|
+
R = np.zeros((m, T))
|
|
170
|
+
cnt = np.zeros((m, T))
|
|
171
|
+
for r, a, t in zip(resid, area_idx, time_idx):
|
|
172
|
+
if 0 <= a < m and 0 <= t < T:
|
|
173
|
+
R[a, t] += np.nan_to_num(r)
|
|
174
|
+
cnt[a, t] += 1.0
|
|
175
|
+
cnt[cnt == 0] = 1.0
|
|
176
|
+
return R / cnt
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _year_labels(des, T):
|
|
180
|
+
labels = des.get("time_labels", []) or []
|
|
181
|
+
if len(labels) == T:
|
|
182
|
+
return [str(l) for l in labels]
|
|
183
|
+
return [str(t) for t in range(T)]
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# ---------------------------------------------------------------------------
|
|
187
|
+
# BAGIAN 3: P1 - Moran per tahun, residual respons, permutasi
|
|
188
|
+
# ---------------------------------------------------------------------------
|
|
189
|
+
|
|
190
|
+
def moran_current(fit, W=None, n_perm=DEFAULT_N_PERM, seed=DEFAULT_SEED):
|
|
191
|
+
"""Moran per tahun pada residual respons (y - mu) kondisional.
|
|
192
|
+
|
|
193
|
+
Per tahun: residual diagregasi rata-rata per area, I_t dihitung,
|
|
194
|
+
p-value via permutasi posisi area (dua sisi terhadap pusat
|
|
195
|
+
distribusi permutasi, p=(1+#ekstrem)/(B+1)).
|
|
196
|
+
"""
|
|
197
|
+
st = _setup(fit, W)
|
|
198
|
+
if "skipped" in st:
|
|
199
|
+
return st
|
|
200
|
+
m, T, Wd = st["m"], st["T"], st["W"]
|
|
201
|
+
rng = np.random.default_rng(seed)
|
|
202
|
+
notes = [
|
|
203
|
+
"Residual respons kondisional (y - mu_hat), diagregasi rata-rata per area per tahun",
|
|
204
|
+
"p-value permutasi dua sisi terhadap pusat distribusi permutasi, p=(1+#ekstrem)/(B+1)",
|
|
205
|
+
f"n_perm={n_perm}, seed={seed}",
|
|
206
|
+
]
|
|
207
|
+
if _iid_area(fit):
|
|
208
|
+
notes.append("W eksternal, bukan struktur model")
|
|
209
|
+
per_year = []
|
|
210
|
+
for t in range(T):
|
|
211
|
+
mask = st["time_idx"] == t
|
|
212
|
+
if mask.sum() < 3:
|
|
213
|
+
per_year.append({"year": _year_labels(st["des"], T)[t],
|
|
214
|
+
"I": float("nan"), "p_value": float("nan")})
|
|
215
|
+
continue
|
|
216
|
+
res_t = _resid_area_t(st["resid"][mask], st["area_idx"][mask], m)
|
|
217
|
+
I_t = _moran_I(_center(res_t), Wd)
|
|
218
|
+
stats_perm = []
|
|
219
|
+
for _ in range(n_perm):
|
|
220
|
+
zp = _center(res_t[rng.permutation(m)])
|
|
221
|
+
stats_perm.append(_moran_I(zp, Wd))
|
|
222
|
+
p_t = _perm_p(I_t, stats_perm)
|
|
223
|
+
per_year.append({"year": _year_labels(st["des"], T)[t],
|
|
224
|
+
"I": float(I_t), "p_value": float(p_t)})
|
|
225
|
+
is_valid = [d["I"] for d in per_year if np.isfinite(d["I"])]
|
|
226
|
+
ps_valid = [d["p_value"] for d in per_year if np.isfinite(d["p_value"])]
|
|
227
|
+
summary = {
|
|
228
|
+
"I_mean": float(np.mean(is_valid)) if is_valid else float("nan"),
|
|
229
|
+
"I_median": float(np.median(is_valid)) if is_valid else float("nan"),
|
|
230
|
+
"p_median": float(np.median(ps_valid)) if ps_valid else float("nan"),
|
|
231
|
+
"n_reject_05": int(sum(p < 0.05 for p in ps_valid)),
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
"approach": "P1",
|
|
235
|
+
"per_year": per_year,
|
|
236
|
+
"summary": summary,
|
|
237
|
+
"notes": notes,
|
|
238
|
+
"meta": {"n_perm": int(n_perm), "seed": int(seed)},
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
# ---------------------------------------------------------------------------
|
|
243
|
+
# BAGIAN 4: P2 - Moran per tahun, residual DS, Anselin/Cliff-Ord
|
|
244
|
+
# ---------------------------------------------------------------------------
|
|
245
|
+
|
|
246
|
+
def moran_literature(fit, W=None):
|
|
247
|
+
"""Moran per tahun pada residual Dunn-Smyth kondisional.
|
|
248
|
+
|
|
249
|
+
Sumber residual: stmmlib.beta.report._dunn_smyth (definisi tunggal yang
|
|
250
|
+
sama dipakai plot_data.resid_normality dan ds_residuals_from_fit).
|
|
251
|
+
E[I] per tahun = tr(M_t W)/(m-k) dengan M_t = I - X_t(X_t'X_t)^-1 X_t'
|
|
252
|
+
(aproksimasi OLS); Var[I] Cliff-Ord (S0, S1, S2).
|
|
253
|
+
"""
|
|
254
|
+
st = _setup(fit, W)
|
|
255
|
+
if "skipped" in st:
|
|
256
|
+
return st
|
|
257
|
+
from stmmlib.beta.report import _dunn_smyth
|
|
258
|
+
resid_ds = np.asarray(_dunn_smyth(st["y"], st["mu"], float(fit.phi)),
|
|
259
|
+
dtype=float)
|
|
260
|
+
m, T, k, Wd = st["m"], st["T"], st["k"], st["W"]
|
|
261
|
+
X = st["des"].get("X")
|
|
262
|
+
if X is None:
|
|
263
|
+
opt = fit._opt
|
|
264
|
+
X = np.asarray(opt["X"], dtype=float) if opt.get("X") is not None else None
|
|
265
|
+
_, v_cliff = _EI_var(Wd, k)
|
|
266
|
+
notes = [
|
|
267
|
+
"Residual Dunn-Smyth kondisional: Phi^-1(F_Beta(y)), sumber tunggal stmmlib.beta.report._dunn_smyth",
|
|
268
|
+
"E[I] per tahun: tr(M_t W)/(m-k), M_t = I - X_t(X_t'X_t)^-1 X_t' (aproksimasi OLS)",
|
|
269
|
+
"Var[I]: rumus Cliff-Ord (S0, S1, S2; asumsi normalitas, randomisasi)",
|
|
270
|
+
"p/Z dua sisi (H1: autokorelasi positif atau negatif)",
|
|
271
|
+
"Kombinasi: Fisher (df=2T), Stouffer, Bonferroni (alpha/T)",
|
|
272
|
+
]
|
|
273
|
+
if _iid_area(fit):
|
|
274
|
+
notes.append("W eksternal, bukan struktur model")
|
|
275
|
+
per_year = []
|
|
276
|
+
z_list = []
|
|
277
|
+
p_list = []
|
|
278
|
+
labels = _year_labels(st["des"], T)
|
|
279
|
+
for t in range(T):
|
|
280
|
+
mask = st["time_idx"] == t
|
|
281
|
+
if mask.sum() < 3 or not np.isfinite(v_cliff) or v_cliff <= 0:
|
|
282
|
+
per_year.append({"year": labels[t], "I": float("nan"),
|
|
283
|
+
"E_I": float("nan"), "Z": float("nan"),
|
|
284
|
+
"p_value": float("nan")})
|
|
285
|
+
continue
|
|
286
|
+
res_t = _resid_area_t(resid_ds[mask], st["area_idx"][mask], m)
|
|
287
|
+
I_t = _moran_I(_center(res_t), Wd)
|
|
288
|
+
# M_t aproksimasi OLS: proyeksi komplement per tahun
|
|
289
|
+
e_t = float("nan")
|
|
290
|
+
if X is not None:
|
|
291
|
+
X_t = np.asarray(X[mask], dtype=float)
|
|
292
|
+
if X_t.shape[0] == m and 0 < k < m:
|
|
293
|
+
try:
|
|
294
|
+
H = X_t @ np.linalg.pinv(X_t.T @ X_t) @ X_t.T
|
|
295
|
+
M_t = np.eye(m) - H
|
|
296
|
+
e_t = float(np.trace(M_t @ Wd)) / (m - k)
|
|
297
|
+
except Exception:
|
|
298
|
+
e_t = float("nan")
|
|
299
|
+
z_t = (I_t - e_t) / np.sqrt(v_cliff) if np.isfinite(e_t) else float("nan")
|
|
300
|
+
p_t = float(2 * stats.norm.sf(abs(z_t))) if np.isfinite(z_t) else float("nan")
|
|
301
|
+
per_year.append({"year": labels[t], "I": float(I_t), "E_I": e_t,
|
|
302
|
+
"Z": float(z_t), "p_value": p_t})
|
|
303
|
+
if np.isfinite(z_t):
|
|
304
|
+
z_list.append(z_t)
|
|
305
|
+
if np.isfinite(p_t):
|
|
306
|
+
p_list.append(p_t)
|
|
307
|
+
# Kombinasi uji per tahun
|
|
308
|
+
fisher_stat = fisher_p = stouffer_z = stouffer_p = float("nan")
|
|
309
|
+
if p_list:
|
|
310
|
+
p_clip = np.clip(p_list, 1e-16, 1.0)
|
|
311
|
+
fisher_stat = float(-2.0 * np.sum(np.log(p_clip)))
|
|
312
|
+
fisher_p = float(stats.chi2.sf(fisher_stat, 2 * len(p_list)))
|
|
313
|
+
if z_list:
|
|
314
|
+
stouffer_z = float(np.sum(z_list) / np.sqrt(len(z_list)))
|
|
315
|
+
stouffer_p = float(2 * stats.norm.sf(abs(stouffer_z)))
|
|
316
|
+
n_reject_bonf = int(sum(p < 0.05 / T for p in p_list)) if p_list else 0
|
|
317
|
+
return {
|
|
318
|
+
"approach": "P2",
|
|
319
|
+
"per_year": per_year,
|
|
320
|
+
"combined": {
|
|
321
|
+
"fisher_stat": float(fisher_stat), "fisher_p": float(fisher_p),
|
|
322
|
+
"stouffer_Z": stouffer_z, "stouffer_p": stouffer_p,
|
|
323
|
+
"n_reject_bonf": n_reject_bonf,
|
|
324
|
+
},
|
|
325
|
+
"notes": notes,
|
|
326
|
+
"meta": {"k": k, "m": m, "T": T},
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
# ---------------------------------------------------------------------------
|
|
331
|
+
# BAGIAN 5: P3 - Moran panel pooled, block permutation, Stouffer-Hartung,
|
|
332
|
+
# bootstrap CI resample area
|
|
333
|
+
# ---------------------------------------------------------------------------
|
|
334
|
+
|
|
335
|
+
def moran_panel_derived(fit, W=None, n_perm=DEFAULT_N_PERM,
|
|
336
|
+
n_boot=DEFAULT_N_BOOT, seed=DEFAULT_SEED):
|
|
337
|
+
"""Moran panel pooled + Stouffer-Hartung + bootstrap CI.
|
|
338
|
+
|
|
339
|
+
W_panel = kron(I_T, W) (urutan time-major: indeks t*m + a).
|
|
340
|
+
I_panel dengan pemusatan global; p via block permutation (permutasi
|
|
341
|
+
residual dalam tiap tahun dengan posisi antar tahun dipertahankan).
|
|
342
|
+
Stouffer-Hartung: Z_t per tahun dari distribusi permutasi + koreksi
|
|
343
|
+
korelasi antar tahun. Bootstrap CI: resample area dengan replacement.
|
|
344
|
+
"""
|
|
345
|
+
st = _setup(fit, W)
|
|
346
|
+
if "skipped" in st:
|
|
347
|
+
return st
|
|
348
|
+
m, T, Wd = st["m"], st["T"], st["W"]
|
|
349
|
+
R = _resid_matrix(st["resid"], st["area_idx"], st["time_idx"], m, T)
|
|
350
|
+
v = R.T.ravel() # time-major: indeks = t*m + a
|
|
351
|
+
Wp = np.kron(np.eye(T), Wd)
|
|
352
|
+
S0p = float(np.sum(Wp))
|
|
353
|
+
rng = np.random.default_rng(seed)
|
|
354
|
+
notes = [
|
|
355
|
+
"Pendekatan pooled panel: residual respons (y - mu_hat) dalam satu vektor m*T",
|
|
356
|
+
"W_panel = kron(I_T, W); I_panel dengan pemusatan global",
|
|
357
|
+
"p-value block permutation dua sisi terhadap pusat distribusi permutasi, p=(1+#ekstrem)/(B+1): posisi area dipermutasi sama di semua tahun (ketergantungan antar tahun dipertahankan)",
|
|
358
|
+
f"Stouffer-Hartung dengan df={T - 1}" if T > 1 else "Stouffer-Hartung: hanya 1 tahun, pakai normal",
|
|
359
|
+
f"Bootstrap CI resample area dengan replacement, B={n_boot}, seed={seed}",
|
|
360
|
+
]
|
|
361
|
+
if _iid_area(fit):
|
|
362
|
+
notes.append("W eksternal, bukan struktur model")
|
|
363
|
+
|
|
364
|
+
# I panel
|
|
365
|
+
I_panel = _moran_I(_center(v), Wp)
|
|
366
|
+
|
|
367
|
+
# Block permutation + distribusi I_t per tahun
|
|
368
|
+
stats_p = []
|
|
369
|
+
per_t = np.zeros((n_perm, T))
|
|
370
|
+
for b in range(n_perm):
|
|
371
|
+
perm = rng.permutation(m)
|
|
372
|
+
vb = v.reshape(T, m)[:, perm]
|
|
373
|
+
stats_p.append(_moran_I(_center(vb.ravel()), Wp))
|
|
374
|
+
for t in range(T):
|
|
375
|
+
per_t[b, t] = _moran_I(_center(vb[t]), Wd)
|
|
376
|
+
p_block = _perm_p(I_panel, stats_p)
|
|
377
|
+
|
|
378
|
+
# Z_t per tahun dari distribusi permutasi
|
|
379
|
+
mu_t = per_t.mean(axis=0)
|
|
380
|
+
sd_t = per_t.std(axis=0, ddof=1)
|
|
381
|
+
I_t_obs = np.array([_moran_I(_center(R[:, t]), Wd) for t in range(T)])
|
|
382
|
+
Z_t = np.where(sd_t > 1e-12, (I_t_obs - mu_t) / np.where(sd_t > 1e-12, sd_t, 1.0),
|
|
383
|
+
np.nan)
|
|
384
|
+
|
|
385
|
+
# Matriks korelasi antar tahun dari replika permutasi
|
|
386
|
+
corr = np.zeros((T, T))
|
|
387
|
+
if T > 1 and n_perm > 2:
|
|
388
|
+
with np.errstate(invalid="ignore", divide="ignore"):
|
|
389
|
+
corr = np.corrcoef(per_t.T)
|
|
390
|
+
corr = np.where(np.isfinite(corr), corr, 0.0)
|
|
391
|
+
corr_list = [[float(x) for x in row] for row in corr]
|
|
392
|
+
|
|
393
|
+
# Stouffer-Hartung
|
|
394
|
+
off = corr[~np.eye(T, dtype=bool)]
|
|
395
|
+
rho_bar = max(float(np.mean(off)), 0.0) if off.size else 0.0
|
|
396
|
+
if T > 1 and np.isfinite(Z_t).sum() > 0:
|
|
397
|
+
denom = np.sqrt(T + T * (T - 1) * rho_bar)
|
|
398
|
+
z_sh = float(np.nansum(Z_t) / denom)
|
|
399
|
+
p_sh = float(2 * stats.t.sf(abs(z_sh), T - 1))
|
|
400
|
+
else:
|
|
401
|
+
z_sh = float(np.nanmean(Z_t)) if np.isfinite(Z_t).any() else float("nan")
|
|
402
|
+
p_sh = float(2 * stats.norm.sf(abs(z_sh))) if np.isfinite(z_sh) else float("nan")
|
|
403
|
+
|
|
404
|
+
# Bootstrap CI: resample area dengan replacement
|
|
405
|
+
ib_boot = []
|
|
406
|
+
for _ in range(n_boot):
|
|
407
|
+
idx = rng.choice(m, m, replace=True)
|
|
408
|
+
Wb = Wd[np.ix_(idx, idx)]
|
|
409
|
+
vb = R[idx, :].T.ravel()
|
|
410
|
+
Wpb = np.kron(np.eye(T), Wb)
|
|
411
|
+
if float(np.sum(Wpb)) <= 0:
|
|
412
|
+
continue # replika degenerate (area unik terlalu sedikit)
|
|
413
|
+
ib_boot.append(_moran_I(_center(vb), Wpb))
|
|
414
|
+
ib_boot = np.asarray([x for x in ib_boot if np.isfinite(x)], dtype=float)
|
|
415
|
+
if ib_boot.size >= 2:
|
|
416
|
+
ci = [float(np.percentile(ib_boot, 2.5)),
|
|
417
|
+
float(np.percentile(ib_boot, 97.5))]
|
|
418
|
+
else:
|
|
419
|
+
ci = [float("nan"), float("nan")]
|
|
420
|
+
|
|
421
|
+
labels = _year_labels(st["des"], T)
|
|
422
|
+
per_year = []
|
|
423
|
+
for t in range(T):
|
|
424
|
+
stats_t = per_t[:, t]
|
|
425
|
+
stats_t = stats_t[np.isfinite(stats_t)]
|
|
426
|
+
p_t = _perm_p(I_t_obs[t], stats_t) if stats_t.size else float("nan")
|
|
427
|
+
per_year.append({"year": labels[t], "I": float(I_t_obs[t]),
|
|
428
|
+
"p_value": float(p_t)})
|
|
429
|
+
|
|
430
|
+
perm_center = float(np.mean(stats_p)) if stats_p else float("nan")
|
|
431
|
+
|
|
432
|
+
return {
|
|
433
|
+
"approach": "P3",
|
|
434
|
+
"I_panel": float(I_panel),
|
|
435
|
+
"p_block": float(p_block),
|
|
436
|
+
"stouffer_Z": float(z_sh),
|
|
437
|
+
"p_stouffer": float(p_sh),
|
|
438
|
+
"ci95": ci,
|
|
439
|
+
"per_year": per_year,
|
|
440
|
+
"perm_center": perm_center,
|
|
441
|
+
"resid_panel": R.T.ravel().copy(),
|
|
442
|
+
"corr_matrix": corr_list,
|
|
443
|
+
"notes": notes,
|
|
444
|
+
"meta": {"n_perm": int(n_perm), "n_boot": int(n_boot),
|
|
445
|
+
"seed": int(seed), "k": st["k"], "m": m, "T": T},
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
# ---------------------------------------------------------------------------
|
|
450
|
+
# BAGIAN 5b: caption + data scatter
|
|
451
|
+
# ---------------------------------------------------------------------------
|
|
452
|
+
|
|
453
|
+
def describe_moran(I, p, konteks="") -> str:
|
|
454
|
+
"""Satu kalimat deskripsi Moran's I dengan p dua sisi."""
|
|
455
|
+
return f"{konteks}: I={I:+.4f}, p={p:.4f} (dua sisi)."
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def build_caption_s4(I_obs, c, p_two, m, S0, T, tol=5e-4) -> str:
|
|
459
|
+
"""Caption plot Moran S4 dengan tafsir arah terhadap pusat permutasi."""
|
|
460
|
+
delta = I_obs - c
|
|
461
|
+
if abs(delta) < tol or p_two >= 0.05:
|
|
462
|
+
arah = "tidak berbeda nyata dari pusat permutasi"
|
|
463
|
+
tafsir = (
|
|
464
|
+
"Residual area tidak menunjukkan pola spasial sistematis: "
|
|
465
|
+
"area dengan residual tinggi tidak mengelompok di dekat area "
|
|
466
|
+
"berresidual tinggi maupun rendah. Tafsir ini TIDAK "
|
|
467
|
+
"membandingkan langsung dengan P1/P2/P3."
|
|
468
|
+
)
|
|
469
|
+
elif delta < 0:
|
|
470
|
+
arah = "negatif"
|
|
471
|
+
tafsir = (
|
|
472
|
+
"Kecenderungan spasial negatif: area berresidual tinggi cenderung "
|
|
473
|
+
"dikelilingi area berresidual rendah (pola HL/LH). Tafsir ini "
|
|
474
|
+
"TIDAK membandingkan langsung dengan P1/P2/P3."
|
|
475
|
+
)
|
|
476
|
+
else:
|
|
477
|
+
arah = "positif"
|
|
478
|
+
tafsir = (
|
|
479
|
+
"Kecenderungan spasial positif: area berresidual tinggi cenderung "
|
|
480
|
+
"dikelilingi area berresidual tinggi, area berresidual rendah "
|
|
481
|
+
"dikelilingi area berresidual rendah (pola HH/LL). Tafsir ini "
|
|
482
|
+
"TIDAK membandingkan langsung dengan P1/P2/P3."
|
|
483
|
+
)
|
|
484
|
+
return (
|
|
485
|
+
f"Sumbu X: residual area (rata-rata {T} tahun); sumbu Y: lag spasial "
|
|
486
|
+
f"(W x residual). Arah {arah} (I={I_obs:+.4f}; pusat permutasi "
|
|
487
|
+
f"c={c:+.4f}; p dua sisi={p_two:.4f}; m={m}; S0={S0:.1f}). {tafsir}"
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
def build_caption_p3(I_panel, c, p_block, ci95, I_t, T_, m, S0_panel) -> str:
|
|
492
|
+
"""Caption plot Moran panel P3 (Panel A pooled, Panel B per tahun)."""
|
|
493
|
+
return (
|
|
494
|
+
f"Sumbu X: residual panel (y - mu); sumbu Y: lag spasial W_panel x "
|
|
495
|
+
f"residual. Panel A: seluruh T tahun (I_panel={I_panel:+.4f}, "
|
|
496
|
+
f"p_block={p_block:.4f}, CI95 bootstrap=[{ci95[0]:+.4f}, "
|
|
497
|
+
f"{ci95[1]:+.4f}]). Panel B: per tahun, I_t berkisar "
|
|
498
|
+
f"{min(I_t):+.3f} sampai {max(I_t):+.3f}. Garis pada plot adalah "
|
|
499
|
+
f"OLS; slope OLS = {I_panel * S0_panel / (m * T_):+.4f} = I_panel x "
|
|
500
|
+
f"S0_panel/(mT) karena S0_panel={S0_panel:.0f} dan mT={m * T_}; "
|
|
501
|
+
f"bukan I_panel. Block permutation mempertahankan ketergantungan "
|
|
502
|
+
f"antar tahun."
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
def moran_panel_scatter_data(fit, W=None, n_perm=DEFAULT_N_PERM,
|
|
507
|
+
n_boot=DEFAULT_N_BOOT, seed=DEFAULT_SEED):
|
|
508
|
+
"""Data plot Moran panel P3 + caption, tanpa menghitung ulang statistik.
|
|
509
|
+
|
|
510
|
+
Memanggil moran_panel_derived SEKALI dan menurunkan koordinat scatter
|
|
511
|
+
(z_flat, lag_flat) serta slope dari hasilnya. Pemanggil wajib memakai
|
|
512
|
+
seed yang sama dengan pemanggilan P3 lain agar residual DS dan
|
|
513
|
+
distribusi permutasi identik.
|
|
514
|
+
|
|
515
|
+
Return dict: z_flat, lag_flat, colors, I_panel, c (perm_center),
|
|
516
|
+
p_block, stouffer_Z, p_stouffer, ci95, I_t (list per tahun),
|
|
517
|
+
slope_ols, slope_I, S0_panel, m, T, caption. Bila skipped, dict
|
|
518
|
+
skipped dikembalikan apa adanya.
|
|
519
|
+
"""
|
|
520
|
+
st = moran_panel_derived(fit, W, n_perm, n_boot, seed)
|
|
521
|
+
if st.get("skipped"):
|
|
522
|
+
return st
|
|
523
|
+
des = (getattr(fit, "_opt", None) or {}).get("des", {}) or {}
|
|
524
|
+
m = int(des.get("m", 1))
|
|
525
|
+
T = int(des.get("T", 1))
|
|
526
|
+
area_idx = np.asarray(
|
|
527
|
+
des.get("area_idx", np.zeros(1, dtype=int)), dtype=int)
|
|
528
|
+
Wd = _resolve_W(fit, W)
|
|
529
|
+
if Wd is None:
|
|
530
|
+
raise ValueError("W tidak tersedia: berikan W atau fit dengan W tersimpan")
|
|
531
|
+
|
|
532
|
+
z_flat = np.asarray(st["resid_panel"], dtype=float)
|
|
533
|
+
z_flat = z_flat - np.mean(z_flat)
|
|
534
|
+
Wp = np.kron(np.eye(T), Wd)
|
|
535
|
+
lag_flat = Wp @ z_flat
|
|
536
|
+
colors = np.repeat(np.arange(T), m)
|
|
537
|
+
S0_panel = float(np.sum(Wp))
|
|
538
|
+
slope_ols = float(z_flat @ lag_flat / (z_flat @ z_flat))
|
|
539
|
+
slope_I = float(st["I_panel"]) * S0_panel / (m * T)
|
|
540
|
+
I_t = [float(d["I"]) for d in st["per_year"]]
|
|
541
|
+
caption = build_caption_p3(float(st["I_panel"]), float(st["perm_center"]),
|
|
542
|
+
float(st["p_block"]), st["ci95"], I_t, T, m,
|
|
543
|
+
S0_panel)
|
|
544
|
+
return {
|
|
545
|
+
"z_flat": z_flat,
|
|
546
|
+
"lag_flat": lag_flat,
|
|
547
|
+
"colors": colors,
|
|
548
|
+
"I_panel": float(st["I_panel"]),
|
|
549
|
+
"c": float(st["perm_center"]),
|
|
550
|
+
"p_block": float(st["p_block"]),
|
|
551
|
+
"stouffer_Z": float(st["stouffer_Z"]),
|
|
552
|
+
"p_stouffer": float(st["p_stouffer"]),
|
|
553
|
+
"ci95": st["ci95"],
|
|
554
|
+
"I_t": I_t,
|
|
555
|
+
"slope_ols": slope_ols,
|
|
556
|
+
"slope_I": slope_I,
|
|
557
|
+
"S0_panel": S0_panel,
|
|
558
|
+
"m": m,
|
|
559
|
+
"T": T,
|
|
560
|
+
"caption": caption,
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
# ---------------------------------------------------------------------------
|
|
565
|
+
# BAGIAN 6: format output
|
|
566
|
+
# ---------------------------------------------------------------------------
|
|
567
|
+
def format_P1(result) -> str:
|
|
568
|
+
"""Format teks hasil P1 (moran_current)."""
|
|
569
|
+
if result.get("skipped"):
|
|
570
|
+
return f"DILEWATI: {result['reason']}"
|
|
571
|
+
L = ["Moran per tahun (P1, residual respons)", _BAR]
|
|
572
|
+
L.append(f" {'Tahun':<10} {'I':>10} {'p':>10} sig")
|
|
573
|
+
for d in result["per_year"]:
|
|
574
|
+
star = _sig(d["p_value"]) if np.isfinite(d.get("p_value", np.nan)) else ""
|
|
575
|
+
L.append(f" {d['year']:<10} {d['I']:>10.4f} {d['p_value']:>10.4f} {star}")
|
|
576
|
+
s = result["summary"]
|
|
577
|
+
n_year = len(result["per_year"])
|
|
578
|
+
L.append(f" Ringkasan: I_mean={s['I_mean']:.4f} I_median={s['I_median']:.4f}"
|
|
579
|
+
f" p_median={s['p_median']:.4f}")
|
|
580
|
+
L.append(f" Tahun signifikan (p<0.05): {s['n_reject_05']}/{n_year}")
|
|
581
|
+
L.append(" CATATAN:")
|
|
582
|
+
for n in result["notes"]:
|
|
583
|
+
L.append(f" - {n}")
|
|
584
|
+
meta = result["meta"]
|
|
585
|
+
L.append(f" Meta: n_perm={meta['n_perm']}, seed={meta['seed']}")
|
|
586
|
+
L.append(_BAR)
|
|
587
|
+
return "\n".join(L)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def format_P2(result) -> str:
|
|
591
|
+
"""Format teks hasil P2 (moran_literature)."""
|
|
592
|
+
if result.get("skipped"):
|
|
593
|
+
return f"DILEWATI: {result['reason']}"
|
|
594
|
+
L = ["Moran per tahun (P2, residual Dunn-Smyth, Anselin/Cliff-Ord)", _BAR]
|
|
595
|
+
L.append(f" {'Tahun':<10} {'I':>10} {'E[I]':>10} {'Z':>10} {'p':>10} sig")
|
|
596
|
+
for d in result["per_year"]:
|
|
597
|
+
star = _sig(d["p_value"]) if np.isfinite(d.get("p_value", np.nan)) else ""
|
|
598
|
+
L.append(f" {d['year']:<10} {d['I']:>10.4f} {d['E_I']:>10.4f} "
|
|
599
|
+
f"{d['Z']:>10.4f} {d['p_value']:>10.4f} {star}")
|
|
600
|
+
c = result["combined"]
|
|
601
|
+
L.append(f" Fisher : X2={c['fisher_stat']:.4f}, p={c['fisher_p']:.4f} (df=2T)")
|
|
602
|
+
L.append(f" Stouffer : Z={c['stouffer_Z']:.4f}, p={c['stouffer_p']:.4f}")
|
|
603
|
+
L.append(f" Bonferroni (alpha/T): {c['n_reject_bonf']}/{len(result['per_year'])} tahun")
|
|
604
|
+
L.append(" CATATAN:")
|
|
605
|
+
for n in result["notes"]:
|
|
606
|
+
L.append(f" - {n}")
|
|
607
|
+
meta = result["meta"]
|
|
608
|
+
L.append(f" Meta: k={meta['k']}, m={meta['m']}, T={meta['T']}")
|
|
609
|
+
L.append(_BAR)
|
|
610
|
+
return "\n".join(L)
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
def format_P3(result) -> str:
|
|
614
|
+
"""Format teks hasil P3 (moran_panel_derived)."""
|
|
615
|
+
if result.get("skipped"):
|
|
616
|
+
return f"DILEWATI: {result['reason']}"
|
|
617
|
+
L = ["Moran panel pooled (P3, W_panel = kron(I_T, W))", _BAR]
|
|
618
|
+
L.append(f" I_panel : {result['I_panel']:+.4f}")
|
|
619
|
+
L.append(f" p (block) : {result['p_block']:.4f}")
|
|
620
|
+
L.append(f" Stouffer-Hartung : Z={result['stouffer_Z']:+.4f}, "
|
|
621
|
+
f"p={result['p_stouffer']:.4f} (df={result['meta']['T'] - 1})")
|
|
622
|
+
L.append(f" CI95 bootstrap: [{result['ci95'][0]:+.4f}, {result['ci95'][1]:+.4f}] (B={result['meta']['n_boot']})")
|
|
623
|
+
L.append(f" {'Tahun':<10} {'I':>10} {'p':>10} sig")
|
|
624
|
+
for d in result["per_year"]:
|
|
625
|
+
star = _sig(d["p_value"]) if np.isfinite(d.get("p_value", np.nan)) else ""
|
|
626
|
+
L.append(f" {d['year']:<10} {d['I']:>10.4f} {d['p_value']:>10.4f} {star}")
|
|
627
|
+
L.append(" CATATAN:")
|
|
628
|
+
for n in result["notes"]:
|
|
629
|
+
L.append(f" - {n}")
|
|
630
|
+
meta = result["meta"]
|
|
631
|
+
L.append(f" Meta: n_perm={meta['n_perm']}, n_boot={meta['n_boot']}, seed={meta['seed']}, "
|
|
632
|
+
f"k={meta['k']}, m={meta['m']}, T={meta['T']}")
|
|
633
|
+
L.append(_BAR)
|
|
634
|
+
return "\n".join(L)
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
def format_compare(p1, p2, p3) -> str:
|
|
638
|
+
"""Format teks perbandingan tiga pendekatan."""
|
|
639
|
+
lines = []
|
|
640
|
+
for r in (p1, p2, p3):
|
|
641
|
+
if r.get("skipped"):
|
|
642
|
+
lines.append(f"DILEWATI: {r['reason']}")
|
|
643
|
+
elif r.get("error"):
|
|
644
|
+
lines.append(f"GAGAL ({r.get('approach', '?')}): {r['error']}")
|
|
645
|
+
if lines:
|
|
646
|
+
return "\n".join(lines)
|
|
647
|
+
L = ["Perbandingan uji Moran panel (3 pendekatan)", _BAR]
|
|
648
|
+
s1, c2 = p1["summary"], p2["combined"]
|
|
649
|
+
L.append(f" P1 respons (per tahun) : I_median={s1['I_median']:+.4f} "
|
|
650
|
+
f"p_median={s1['p_median']:.4f} reject={s1['n_reject_05']}/{len(p1['per_year'])}")
|
|
651
|
+
L.append(f" P2 Anselin (residual DS) : Stouffer Z={c2['stouffer_Z']:+.4f} "
|
|
652
|
+
f"p={c2['stouffer_p']:.4f} bonf_reject={c2['n_reject_bonf']}/{len(p2['per_year'])}")
|
|
653
|
+
L.append(f" P3 pooled panel : I_panel={p3['I_panel']:+.4f} "
|
|
654
|
+
f"p_block={p3['p_block']:.4f} p_Stouffer-Hartung={p3['p_stouffer']:.4f}")
|
|
655
|
+
L.append(" Semua p/Z dua sisi (H1: positif atau negatif).")
|
|
656
|
+
L.append(" CATATAN:")
|
|
657
|
+
for n in p3["notes"][:2]:
|
|
658
|
+
L.append(f" - {n}")
|
|
659
|
+
L.append(f" df Stouffer-Hartung = {p3['meta']['T'] - 1}, B bootstrap = {p3['meta']['n_boot']}")
|
|
660
|
+
L.append(_BAR)
|
|
661
|
+
return "\n".join(L)
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
# ---------------------------------------------------------------------------
|
|
665
|
+
# BAGIAN 7: fasade
|
|
666
|
+
# ---------------------------------------------------------------------------
|
|
667
|
+
|
|
668
|
+
def compare_moran(fit, W=None, n_perm=DEFAULT_N_PERM, n_boot=DEFAULT_N_BOOT,
|
|
669
|
+
seed=DEFAULT_SEED):
|
|
670
|
+
"""Jalankan ketiga pendekatan, kembalikan dict {P1, P2, P3, compare}."""
|
|
671
|
+
out = {}
|
|
672
|
+
for name, fn in (("P1", moran_current), ("P2", moran_literature),
|
|
673
|
+
("P3", moran_panel_derived)):
|
|
674
|
+
try:
|
|
675
|
+
if name == "P1":
|
|
676
|
+
out[name] = fn(fit, W, n_perm=n_perm, seed=seed)
|
|
677
|
+
elif name == "P3":
|
|
678
|
+
out[name] = fn(fit, W, n_perm=n_perm, n_boot=n_boot, seed=seed)
|
|
679
|
+
else:
|
|
680
|
+
out[name] = fn(fit, W)
|
|
681
|
+
except Exception as e:
|
|
682
|
+
out[name] = {"approach": name, "error": str(e)}
|
|
683
|
+
out["compare"] = format_compare(out["P1"], out["P2"], out["P3"])
|
|
684
|
+
return out
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
# ---------------------------------------------------------------------------
|
|
688
|
+
# BAGIAN 8: self-check
|
|
689
|
+
# ---------------------------------------------------------------------------
|
|
690
|
+
|
|
691
|
+
class _FakeFit:
|
|
692
|
+
"""Fit tiruan minimal untuk self-check (tanpa refit)."""
|
|
693
|
+
|
|
694
|
+
def __init__(self, model, m, T, seed):
|
|
695
|
+
rng = np.random.default_rng(seed)
|
|
696
|
+
n = m * T
|
|
697
|
+
X = np.column_stack([np.ones(n), rng.normal(size=n), rng.normal(size=n)])
|
|
698
|
+
mu = 1.0 / (1.0 + np.exp(-(X @ np.array([0.0, 0.3, -0.2]))))
|
|
699
|
+
area_idx = np.repeat(np.arange(m), T)
|
|
700
|
+
time_idx = np.tile(np.arange(T), m)
|
|
701
|
+
W = np.zeros((m, m))
|
|
702
|
+
for i in range(m - 1):
|
|
703
|
+
W[i, i + 1] = W[i + 1, i] = 1.0
|
|
704
|
+
self.phi = 25.0
|
|
705
|
+
self.beta = np.zeros(3)
|
|
706
|
+
self.model = model
|
|
707
|
+
self._opt = {
|
|
708
|
+
"y": np.clip(mu + rng.normal(0, 0.05, n), 0.01, 0.99),
|
|
709
|
+
"mu_hat": mu, "X": X, "des": {
|
|
710
|
+
"m": m, "T": T, "area_idx": area_idx, "time_idx": time_idx,
|
|
711
|
+
"area_labels": [f"A{i}" for i in range(m)],
|
|
712
|
+
"time_labels": [str(2018 + t) for t in range(T)],
|
|
713
|
+
},
|
|
714
|
+
"W": W, "model": model,
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
def _self_check():
|
|
719
|
+
m, T = 8, 3
|
|
720
|
+
print(f"Self-check moran_panel: panel sintetis m={m}, T={T}, W rantai")
|
|
721
|
+
fit = _FakeFit("m3_sar_iid", m, T, DEFAULT_SEED)
|
|
722
|
+
r1 = moran_current(fit)
|
|
723
|
+
r2 = moran_literature(fit)
|
|
724
|
+
r3 = moran_panel_derived(fit)
|
|
725
|
+
print(format_compare(r1, r2, r3))
|
|
726
|
+
# guard IID: M1 tanpa W harus skipped
|
|
727
|
+
fit_iid = _FakeFit("m1_iid_iid", m, T, DEFAULT_SEED)
|
|
728
|
+
sk = moran_current(fit_iid)
|
|
729
|
+
print("Guard IID:", sk["reason"] if sk.get("skipped") else "GAGAL: guard tidak terpicu")
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
if __name__ == "__main__":
|
|
733
|
+
_self_check()
|