Skip to content

空间面板模型

静态固定效应 SAR、SEM、SDM、SAC 与直接、间接、总效应分解。

核心代码

py
    def _fit_panel(self, *, decimals: int, title: str) -> None:
        prepared = self._prepare_panel(require_x=True)
        model_type = str(self.spatial_options.get('model_type') or 'sdm').lower()
        if model_type not in {'sar', 'sdm', 'sem', 'sac'}:
            raise ValueError(_('空间面板模型仅支持 SAR、SDM、SEM 和 SAC'))
        estimator = str(self.spatial_options.get('estimator') or 'fe').lower()
        effect_type = str(self.spatial_options.get('effect_type') or 'both').lower()
        if estimator != 'fe':
            raise ValueError(_('当前仅开放经过 xsmle 黄金样本校准的固定效应空间面板估计'))
        se_type = str(self.se_options.get('type') or 'iid').lower()
        if se_type not in {'iid', 'oim'}:
            raise ValueError(_('当前空间面板仅开放 OIM 系数标准误'))
        result, arrays = self._fit_estimation(
            prepared,
            estimator=estimator,
            effect_type=effect_type,
            model_type=model_type,
        )
        entity_var, time_var = self._panel_columns()
        self.table_custom_rows = []
        if effect_type in {'ind', 'both'}:
            self.table_custom_rows.append({'label': f'{entity_var} FE', 'value': 'Yes'})
        if effect_type in {'time', 'both'}:
            self.table_custom_rows.append({'label': f'{time_var} FE', 'value': 'Yes'})
        impacts = []
        if model_type in {'sar', 'sdm', 'sac'} and bool(self.spatial_options.get('compute_effects', True)):
            impacts = self._compute_impacts(result, arrays)

        self.model_stats = {
            'N': int(result['nobs']),
            'Groups': int(prepared['n']),
            'Periods': int(arrays['t']),
            'Log-Likelihood': result['loglike'],
            'AIC': result['aic'],
            'BIC': result['bic'],
            'Converged': 1,
        }
        self.result_payload = {
            'model': model_type,
            'estimator': estimator,
            'effect_type': effect_type,
            'coefficient_covariance': 'oim',
            'matrix_restriction': (
                'D=W' if model_type == 'sdm' else 'E=W' if model_type == 'sac' else None
            ),
            'parameter_names': result['names'],
            'parameters': result['params'].tolist(),
            'standard_errors': result['std_errors'].tolist(),
            'pvalues': result['pvalues'].tolist(),
            'impacts': impacts,
            'dropped_columns': result['dropped_columns'],
            'numerical_diagnostics': {
                'hessian_rank': result['hessian_rank'],
                'hessian_condition': result['hessian_condition'],
                'hessian_min_eigenvalue': result['hessian_min_eigenvalue'],
                'spatial_bound': result['bound'],
                'boundary_distance': result['boundary_distance'],
                'near_boundary': result['near_boundary'],
            },
            'alignment_report': self.alignment_report,
        }
        self._panel_render_context = {
            'result': result,
            'impacts': impacts,
            'prepared': prepared,
            'arrays': arrays,
            'model_type': model_type,
            'estimator': estimator,
            'effect_type': effect_type,
        }
        # 空间面板接入全站通用合并表路径,支持“追加到当前表”。
        self.custom_html = ''
        self.raw_output = self._raw_model_output(
            result,
            impacts,
            prepared,
            arrays,
            model_type,
            estimator,
            effect_type,
            self.alignment_report,
        )

Released under the AGPL-3.0 License.