外观
空间相关性(全局莫兰指数)
按时期计算全局 Moran’s I、解析检验与置换检验。
核心代码
py
def _fit_moran(self, *, decimals: int, title: str) -> None:
prepared = self._prepare_panel(require_x=False)
raw_permutations = self.spatial_options.get('moran_permutations')
permutations = 999 if raw_permutations is None else max(0, min(int(raw_permutations), 999))
seed = int(self.spatial_options.get('moran_seed') or 5282026)
rng = np.random.default_rng(seed)
rows = []
valid_years = []
for index, time_value in enumerate(prepared['times']):
values = prepared['y'][index]
if np.nanstd(values) <= 1e-12:
rows.append({
'time': time_value, 'i': np.nan, 'expected': -1 / max(len(values) - 1, 1),
'sd': np.nan, 'z': np.nan, 'p': np.nan,
'normality_sd': np.nan, 'normality_z': np.nan, 'normality_p': np.nan,
'randomization_sd': np.nan, 'randomization_z': np.nan,
'randomization_p': np.nan, 'permutation_p': np.nan,
'note': _('该时期变量没有横截面变异,已跳过'),
})
continue
result = self._global_moran(values, self.weight_matrix, permutations, rng)
rows.append({'time': time_value, **result, 'note': ''})
valid_years.append((time_value, result['i']))
if valid_years:
trend = self._moran_trend_image(valid_years)
if trend:
self.chart_images.append(trend)
self.model_stats = {
'N': int(prepared['n'] * prepared['t']),
'Groups': int(prepared['n']),
'Periods': int(prepared['t']),
}
self.result_payload = {
'global_moran': rows,
'alignment_report': self.alignment_report,
'seed': seed,
'permutations': permutations,
}
self.custom_html = self._render_moran_html(rows, decimals, title)
self.raw_output = json_like_text(self.result_payload)