uspatial 0.2.0__tar.gz
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.
- uspatial-0.2.0/LICENSE +21 -0
- uspatial-0.2.0/PKG-INFO +224 -0
- uspatial-0.2.0/README.md +173 -0
- uspatial-0.2.0/pyproject.toml +119 -0
- uspatial-0.2.0/setup.cfg +4 -0
- uspatial-0.2.0/src/uspatial/__init__.py +84 -0
- uspatial-0.2.0/src/uspatial/__main__.py +7 -0
- uspatial-0.2.0/src/uspatial/cli.py +269 -0
- uspatial-0.2.0/src/uspatial/data.py +144 -0
- uspatial-0.2.0/src/uspatial/diagnostics.py +608 -0
- uspatial-0.2.0/src/uspatial/models.py +744 -0
- uspatial-0.2.0/src/uspatial/reporting.py +152 -0
- uspatial-0.2.0/src/uspatial/visualization.py +254 -0
- uspatial-0.2.0/src/uspatial/weights.py +168 -0
- uspatial-0.2.0/src/uspatial.egg-info/PKG-INFO +224 -0
- uspatial-0.2.0/src/uspatial.egg-info/SOURCES.txt +25 -0
- uspatial-0.2.0/src/uspatial.egg-info/dependency_links.txt +1 -0
- uspatial-0.2.0/src/uspatial.egg-info/entry_points.txt +2 -0
- uspatial-0.2.0/src/uspatial.egg-info/requires.txt +30 -0
- uspatial-0.2.0/src/uspatial.egg-info/top_level.txt +1 -0
- uspatial-0.2.0/tests/test_cli.py +198 -0
- uspatial-0.2.0/tests/test_columbus_reference.py +200 -0
- uspatial-0.2.0/tests/test_diagnostics.py +325 -0
- uspatial-0.2.0/tests/test_models.py +420 -0
- uspatial-0.2.0/tests/test_reporting.py +94 -0
- uspatial-0.2.0/tests/test_visualization.py +79 -0
- uspatial-0.2.0/tests/test_weights.py +128 -0
uspatial-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dongsoo Jung
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
uspatial-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: uspatial
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Spatial econometric models for urban policy evaluation: spatial weights, Moran/LISA/LM diagnostics, OLS/SLM/SEM/SDM/GWR, LeSage-Pace impacts, publication tables
|
|
5
|
+
Author-email: Dongsoo Jung <jds068888@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/DongsooJung/urban-spatial-analysis
|
|
8
|
+
Project-URL: Repository, https://github.com/DongsooJung/urban-spatial-analysis
|
|
9
|
+
Project-URL: Issues, https://github.com/DongsooJung/urban-spatial-analysis/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/DongsooJung/urban-spatial-analysis/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: spatial-econometrics,gis,urban-analytics,pysal,morans-i,spatial-lag,spatial-error,spatial-durbin,gwr,lisa,korea
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: GIS
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: numpy>=1.26.0
|
|
24
|
+
Requires-Dist: pandas>=2.1.0
|
|
25
|
+
Requires-Dist: scipy>=1.11.0
|
|
26
|
+
Requires-Dist: geopandas>=0.14.0
|
|
27
|
+
Requires-Dist: shapely>=2.0.0
|
|
28
|
+
Requires-Dist: pyproj>=3.6.0
|
|
29
|
+
Requires-Dist: libpysal>=4.8.0
|
|
30
|
+
Requires-Dist: spreg>=1.4.0
|
|
31
|
+
Requires-Dist: esda>=2.5.0
|
|
32
|
+
Requires-Dist: statsmodels>=0.14.0
|
|
33
|
+
Requires-Dist: mapclassify>=2.6.0
|
|
34
|
+
Requires-Dist: matplotlib>=3.8.0
|
|
35
|
+
Provides-Extra: gwr
|
|
36
|
+
Requires-Dist: mgwr>=2.1.2; extra == "gwr"
|
|
37
|
+
Provides-Extra: viz
|
|
38
|
+
Requires-Dist: seaborn>=0.13.0; extra == "viz"
|
|
39
|
+
Requires-Dist: folium>=0.15.0; extra == "viz"
|
|
40
|
+
Requires-Dist: splot>=1.1.5; extra == "viz"
|
|
41
|
+
Provides-Extra: dev
|
|
42
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
43
|
+
Requires-Dist: mgwr>=2.1.2; extra == "dev"
|
|
44
|
+
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
|
|
45
|
+
Requires-Dist: black>=23.12.0; extra == "dev"
|
|
46
|
+
Requires-Dist: ruff<0.18,>=0.17.6; extra == "dev"
|
|
47
|
+
Requires-Dist: mypy>=1.7.0; extra == "dev"
|
|
48
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
49
|
+
Requires-Dist: twine>=5.0.0; extra == "dev"
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# Urban Spatial Econometric Analysis · 도시 공간계량 분석
|
|
53
|
+
|
|
54
|
+
> 파이썬과 GIS 기반 도시정책 평가용 공간계량 모형
|
|
55
|
+
> Spatial econometric models for urban policy evaluation using Python and GIS
|
|
56
|
+
|
|
57
|
+
[](https://github.com/DongsooJung/urban-spatial-analysis/actions/workflows/ci.yml)
|
|
58
|
+
[](https://python.org)
|
|
59
|
+
[](https://pysal.org)
|
|
60
|
+
[](LICENSE)
|
|
61
|
+
|
|
62
|
+
## 개요 · Overview
|
|
63
|
+
|
|
64
|
+
도시 데이터(주택가격·범죄율·인프라 품질)는 인접 지역 간 **공간 자기상관**을 갖습니다. 이를 무시한 전통적 회귀모형은 편향된 계수와 잘못된 정책 결론을 낳습니다. 본 저장소는 공간 가중행렬 구축부터 SLM·SEM·SDM·GWR 추정, 직접·간접효과 분해, 출판용 표 생성까지 완결된 공간계량 파이프라인을 제공합니다.
|
|
65
|
+
|
|
66
|
+
Urban data — property values, crime rates, infrastructure quality — exhibits **spatial autocorrelation** across neighboring areas. Traditional regression that ignores this yields biased coefficients and invalid policy recommendations. This repository implements a complete spatial econometric pipeline: spatial weight matrices, Moran/LISA/LM diagnostics, OLS/SLM/SEM/SDM/GWR estimation, LeSage–Pace impacts, and publication-ready tables.
|
|
67
|
+
|
|
68
|
+
## 문제와 접근 · Problem & Approach
|
|
69
|
+
|
|
70
|
+
1. **공간 가중행렬 / Spatial Weight Matrix** — Queen·Rook 인접성, K-최근접, 거리, 커널 (`weights.py`)
|
|
71
|
+
2. **공간 시차 모형 / Spatial Lag Model (SLM)** — 내생적 공간 상호작용 포착
|
|
72
|
+
3. **공간 오차 모형 / Spatial Error Model (SEM)** — 공간 상관 잔차 보정
|
|
73
|
+
4. **공간 더빈 모형 / Spatial Durbin Model (SDM)** — 시차·오차 효과 결합
|
|
74
|
+
5. **지리가중회귀 / Geographically Weighted Regression (GWR)** — 국지적 계수 추정
|
|
75
|
+
6. **진단 / Diagnostics** — Moran's I, Geary's C, LISA, LM/Robust LM, VIF, Chow (`diagnostics.py`)
|
|
76
|
+
|
|
77
|
+
## 빠른 예시 · Example
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import uspatial as u
|
|
81
|
+
|
|
82
|
+
gdf = u.load_example("columbus") # libpysal 동봉 데이터, 네트워크 불필요
|
|
83
|
+
w = u.build_weights(gdf, method="queen")
|
|
84
|
+
|
|
85
|
+
# 1) OLS → LM/Robust LM 진단 → 모형 추천 (Anselin & Florax 1995 규칙)
|
|
86
|
+
selection = u.select_model(gdf, "CRIME", ["INC", "HOVAL"], w)
|
|
87
|
+
print(selection.recommendation) # → 'SLM'
|
|
88
|
+
print(selection.diagnostics.to_frame())
|
|
89
|
+
|
|
90
|
+
# 2) 추천 모형 적합 + 직접·간접·총 효과 (LeSage & Pace 2009)
|
|
91
|
+
slm = selection.fit(gdf, "CRIME", ["INC", "HOVAL"], w)
|
|
92
|
+
print(slm.summary())
|
|
93
|
+
print(u.impacts_decomposition(slm, draws=1000)) # 시뮬레이션 표준오차 포함
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## 구현 현황 · Implementation Status
|
|
97
|
+
|
|
98
|
+
| 기능 | 함수 | 상태 |
|
|
99
|
+
|------|------|------|
|
|
100
|
+
| 공간가중행렬 5종 (queen·rook·knn·distance·kernel) | `build_weights` | ✅ |
|
|
101
|
+
| OLS + 공간 진단 | `SpatialModel(method="OLS")` | ✅ |
|
|
102
|
+
| SLM · SEM · SDM (최대우도) | `method="SLM"/"SEM"/"SDM"` | ✅ |
|
|
103
|
+
| GWR (AICc 대역폭, mgwr) | `method="GWR"` | ✅ |
|
|
104
|
+
| 직접·간접·총효과 (LeSage & Pace) | `impacts_decomposition` | ✅ |
|
|
105
|
+
| Moran's I · Geary's C · LISA | `morans_i`, `gearys_c`, `local_moran` | ✅ |
|
|
106
|
+
| LM/Robust LM · LM-WX · 잔차 Moran | `lm_diagnostics`, `residual_moran` | ✅ |
|
|
107
|
+
| VIF · 구조 안정성 Chow 검정 | `variance_inflation_factor`, `spatial_chow_test` | ✅ |
|
|
108
|
+
| 모형 비교(ΔAIC) · 모형 선택 | `compare_models`, `select_model` | ✅ |
|
|
109
|
+
| 출판용 표 (Markdown·LaTeX) | `reporting.py` | ✅ |
|
|
110
|
+
| 명령줄 인터페이스 | `python -m uspatial` | ✅ |
|
|
111
|
+
| 시각화 (단계구분도·LISA·GWR 계수 지도) | `visualization.py` | ✅ |
|
|
112
|
+
| 한국 KOSIS·NSDI 실데이터 연결 | — | 🗺 로드맵 |
|
|
113
|
+
|
|
114
|
+
## 정확성 검증 · Validation (Columbus, queen 행표준화)
|
|
115
|
+
|
|
116
|
+
| 모형 | 상수 | INC | HOVAL | 공간계수 | logL | AIC |
|
|
117
|
+
|------|------|-----|-------|----------|------|-----|
|
|
118
|
+
| OLS (Anselin 1988) | 68.619 | −1.597 | −0.274 | — | −187.377 | 380.754 |
|
|
119
|
+
| SLM | 45.603 | −1.049 | −0.266 | ρ = 0.423 | −182.674 | 373.348 |
|
|
120
|
+
| SEM | 60.280 | −0.957 | −0.305 | λ = 0.547 | −183.749 | 373.499 |
|
|
121
|
+
| SDM | 44.320 | −0.920 | −0.297 | ρ = 0.403 | −181.639 | 375.279 |
|
|
122
|
+
|
|
123
|
+
위 계수는 Anselin (1988) 공표값과 일치합니다. LM 진단은 LM-Lag p = 0.003, LM-Error p = 0.023, Robust LM-Lag p = 0.053, Robust LM-Error p = 0.83 으로 SLM을 추천합니다. 전체 흐름과 지도는 [노트북](notebooks/01_columbus_full_analysis.ipynb)에 실행 결과와 함께 있습니다.
|
|
124
|
+
|
|
125
|
+
추가 회귀 검증은 합성 격자 자료로 수행합니다: 참값 복원(SLM ρ=0.6→0.61, SEM λ=0.5→0.39, SDM θ_x1=0.8→1.02), 총효과 항등식(행표준화 W에서 β/(1−ρ)), LM 규칙 4분기 판별, 체커보드 음의 자기상관 등.
|
|
126
|
+
|
|
127
|
+
## 기술 스택 · Tech Stack
|
|
128
|
+
|
|
129
|
+
- **Core:** Python 3.11+, PySAL (libpysal, spreg, esda), GeoPandas, statsmodels
|
|
130
|
+
- **GWR:** mgwr (선택 의존성, `pip install "uspatial[gwr]"`)
|
|
131
|
+
- **Visualization:** Matplotlib, mapclassify
|
|
132
|
+
|
|
133
|
+
## 프로젝트 구조 · Repository Structure
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
urban-spatial-analysis/
|
|
137
|
+
├── src/uspatial/
|
|
138
|
+
│ ├── weights.py # 공간 가중행렬 (W) 구축
|
|
139
|
+
│ ├── models.py # OLS·SLM·SEM·SDM·GWR, 모형 비교, 효과 분해, 모형 선택
|
|
140
|
+
│ ├── diagnostics.py # Moran's I · Geary's C · LISA · LM/Robust LM · VIF · Chow
|
|
141
|
+
│ ├── reporting.py # 출판용 계수표(Markdown/LaTeX)·모형 비교표
|
|
142
|
+
│ ├── cli.py # `python -m uspatial` (examples/diagnose/estimate/compare)
|
|
143
|
+
│ ├── data.py # 데이터 로딩·전처리
|
|
144
|
+
│ └── visualization.py # Choropleth · 군집 지도 · GWR 계수 지도
|
|
145
|
+
├── notebooks/
|
|
146
|
+
│ └── 01_columbus_full_analysis.ipynb # 전체 분석 워크플로(Columbus 예제)
|
|
147
|
+
├── tests/ # 단위·회귀 테스트 (Columbus 기준값 + 합성 격자 회귀)
|
|
148
|
+
├── legacy/ # 패키지 이전 초안 (동작하지 않음, 참고용 — git 이력 보존)
|
|
149
|
+
├── data/README.md # 데이터 출처 및 다운로드 안내
|
|
150
|
+
├── docs/ARCHITECTURE.md # 설계·방법론 문서
|
|
151
|
+
├── pyproject.toml # 패키징·ruff·pytest 설정
|
|
152
|
+
├── CONTRIBUTING.md
|
|
153
|
+
├── requirements.txt
|
|
154
|
+
└── LICENSE
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## 빠른 시작 · Quick Start
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
git clone https://github.com/DongsooJung/urban-spatial-analysis.git
|
|
161
|
+
cd urban-spatial-analysis
|
|
162
|
+
pip install -e ".[gwr,dev]" # 또는 pip install -r requirements.txt
|
|
163
|
+
jupyter notebook notebooks/01_columbus_full_analysis.ipynb
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 라이브러리로 쓰기
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from uspatial import build_weights, select_model, impacts_decomposition
|
|
170
|
+
|
|
171
|
+
w = build_weights(gdf, method="queen") # 행 표준화된 Queen W
|
|
172
|
+
|
|
173
|
+
# 1) OLS → LM/Robust LM 진단 → 모형 추천 (Anselin & Florax 1995 규칙)
|
|
174
|
+
selection = select_model(gdf, "log_price", ["inc", "dist_cbd"], w)
|
|
175
|
+
print(selection.recommendation) # 'OLS' | 'SLM' | 'SEM' | 'SDM'
|
|
176
|
+
print(selection.diagnostics.to_frame()) # LM 4종 통계량·p값
|
|
177
|
+
|
|
178
|
+
# 2) 추천 모형 적합 + 직접·간접·총 효과 (LeSage & Pace 2009)
|
|
179
|
+
sem = selection.fit(gdf, "log_price", ["inc", "dist_cbd"], w)
|
|
180
|
+
print(sem.summary())
|
|
181
|
+
print(impacts_decomposition(sem, draws=1000)) # 시뮬레이션 표준오차 포함
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### 명령줄로 쓰기
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
python -m uspatial examples
|
|
188
|
+
python -m uspatial diagnose --data tracts.csv --y log_price --x inc,dist_cbd --coords lon,lat --knn 6
|
|
189
|
+
python -m uspatial estimate --data tracts.csv --y log_price --x inc,dist_cbd \
|
|
190
|
+
--coords lon,lat --knn 6 --method SEM --format markdown
|
|
191
|
+
python -m uspatial compare --data tracts.csv --y log_price --x inc,dist_cbd \
|
|
192
|
+
--coords lon,lat --knn 6 --methods OLS,SLM,SEM --json
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### 테스트
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
pytest -q # 합성 격자 기반: 참값 복원력·효과 분해 항등식·LM 규칙 + Columbus 공표값
|
|
199
|
+
ruff check src tests
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## 연구 맥락 · Research Context
|
|
203
|
+
|
|
204
|
+
서울대학교 건설환경공학부(스마트도시공학) 박사과정 연구를 지원하며, 군 공항 이전 영향 분석과 대중교통중심개발(TOD) 평가 등 도시정책 평가에 적용됩니다.
|
|
205
|
+
|
|
206
|
+
This work supports doctoral research at Seoul National University, Department of Civil & Environmental Engineering (Smart City Engineering), applied to urban policy evaluation including military airport relocation impact analysis and transit-oriented development assessment.
|
|
207
|
+
|
|
208
|
+
## 참고문헌 · References
|
|
209
|
+
|
|
210
|
+
- Anselin, L. (1988). *Spatial Econometrics: Methods and Models*. Springer.
|
|
211
|
+
- LeSage, J. & Pace, R.K. (2009). *Introduction to Spatial Econometrics*. CRC Press.
|
|
212
|
+
- Anselin, L., Bera, A. K., Florax, R., & Yoon, M. J. (1996). Simple diagnostic tests for spatial dependence. *Regional Science and Urban Economics*, 26(1), 77–104.
|
|
213
|
+
- Anselin, L. (1995). Local indicators of spatial association — LISA. *Geographical Analysis*, 27(2), 93–115.
|
|
214
|
+
- Fotheringham, A. S., Brunsdon, C., & Charlton, M. (2002). *Geographically Weighted Regression*. Wiley.
|
|
215
|
+
- Rey, S.J. & Anselin, L. (2010). PySAL: A Python Library of Spatial Analytical Methods.
|
|
216
|
+
|
|
217
|
+
## 라이선스 · License
|
|
218
|
+
|
|
219
|
+
MIT License — 자세한 내용은 [LICENSE](LICENSE) 참조.
|
|
220
|
+
|
|
221
|
+
## 저자 · Author
|
|
222
|
+
|
|
223
|
+
**정동수 (Dongsoo Jung)** — 서울대학교 박사과정 · 스마트도시공학
|
|
224
|
+
[GitHub @DongsooJung](https://github.com/DongsooJung)
|
uspatial-0.2.0/README.md
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# Urban Spatial Econometric Analysis · 도시 공간계량 분석
|
|
2
|
+
|
|
3
|
+
> 파이썬과 GIS 기반 도시정책 평가용 공간계량 모형
|
|
4
|
+
> Spatial econometric models for urban policy evaluation using Python and GIS
|
|
5
|
+
|
|
6
|
+
[](https://github.com/DongsooJung/urban-spatial-analysis/actions/workflows/ci.yml)
|
|
7
|
+
[](https://python.org)
|
|
8
|
+
[](https://pysal.org)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
|
|
11
|
+
## 개요 · Overview
|
|
12
|
+
|
|
13
|
+
도시 데이터(주택가격·범죄율·인프라 품질)는 인접 지역 간 **공간 자기상관**을 갖습니다. 이를 무시한 전통적 회귀모형은 편향된 계수와 잘못된 정책 결론을 낳습니다. 본 저장소는 공간 가중행렬 구축부터 SLM·SEM·SDM·GWR 추정, 직접·간접효과 분해, 출판용 표 생성까지 완결된 공간계량 파이프라인을 제공합니다.
|
|
14
|
+
|
|
15
|
+
Urban data — property values, crime rates, infrastructure quality — exhibits **spatial autocorrelation** across neighboring areas. Traditional regression that ignores this yields biased coefficients and invalid policy recommendations. This repository implements a complete spatial econometric pipeline: spatial weight matrices, Moran/LISA/LM diagnostics, OLS/SLM/SEM/SDM/GWR estimation, LeSage–Pace impacts, and publication-ready tables.
|
|
16
|
+
|
|
17
|
+
## 문제와 접근 · Problem & Approach
|
|
18
|
+
|
|
19
|
+
1. **공간 가중행렬 / Spatial Weight Matrix** — Queen·Rook 인접성, K-최근접, 거리, 커널 (`weights.py`)
|
|
20
|
+
2. **공간 시차 모형 / Spatial Lag Model (SLM)** — 내생적 공간 상호작용 포착
|
|
21
|
+
3. **공간 오차 모형 / Spatial Error Model (SEM)** — 공간 상관 잔차 보정
|
|
22
|
+
4. **공간 더빈 모형 / Spatial Durbin Model (SDM)** — 시차·오차 효과 결합
|
|
23
|
+
5. **지리가중회귀 / Geographically Weighted Regression (GWR)** — 국지적 계수 추정
|
|
24
|
+
6. **진단 / Diagnostics** — Moran's I, Geary's C, LISA, LM/Robust LM, VIF, Chow (`diagnostics.py`)
|
|
25
|
+
|
|
26
|
+
## 빠른 예시 · Example
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import uspatial as u
|
|
30
|
+
|
|
31
|
+
gdf = u.load_example("columbus") # libpysal 동봉 데이터, 네트워크 불필요
|
|
32
|
+
w = u.build_weights(gdf, method="queen")
|
|
33
|
+
|
|
34
|
+
# 1) OLS → LM/Robust LM 진단 → 모형 추천 (Anselin & Florax 1995 규칙)
|
|
35
|
+
selection = u.select_model(gdf, "CRIME", ["INC", "HOVAL"], w)
|
|
36
|
+
print(selection.recommendation) # → 'SLM'
|
|
37
|
+
print(selection.diagnostics.to_frame())
|
|
38
|
+
|
|
39
|
+
# 2) 추천 모형 적합 + 직접·간접·총 효과 (LeSage & Pace 2009)
|
|
40
|
+
slm = selection.fit(gdf, "CRIME", ["INC", "HOVAL"], w)
|
|
41
|
+
print(slm.summary())
|
|
42
|
+
print(u.impacts_decomposition(slm, draws=1000)) # 시뮬레이션 표준오차 포함
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 구현 현황 · Implementation Status
|
|
46
|
+
|
|
47
|
+
| 기능 | 함수 | 상태 |
|
|
48
|
+
|------|------|------|
|
|
49
|
+
| 공간가중행렬 5종 (queen·rook·knn·distance·kernel) | `build_weights` | ✅ |
|
|
50
|
+
| OLS + 공간 진단 | `SpatialModel(method="OLS")` | ✅ |
|
|
51
|
+
| SLM · SEM · SDM (최대우도) | `method="SLM"/"SEM"/"SDM"` | ✅ |
|
|
52
|
+
| GWR (AICc 대역폭, mgwr) | `method="GWR"` | ✅ |
|
|
53
|
+
| 직접·간접·총효과 (LeSage & Pace) | `impacts_decomposition` | ✅ |
|
|
54
|
+
| Moran's I · Geary's C · LISA | `morans_i`, `gearys_c`, `local_moran` | ✅ |
|
|
55
|
+
| LM/Robust LM · LM-WX · 잔차 Moran | `lm_diagnostics`, `residual_moran` | ✅ |
|
|
56
|
+
| VIF · 구조 안정성 Chow 검정 | `variance_inflation_factor`, `spatial_chow_test` | ✅ |
|
|
57
|
+
| 모형 비교(ΔAIC) · 모형 선택 | `compare_models`, `select_model` | ✅ |
|
|
58
|
+
| 출판용 표 (Markdown·LaTeX) | `reporting.py` | ✅ |
|
|
59
|
+
| 명령줄 인터페이스 | `python -m uspatial` | ✅ |
|
|
60
|
+
| 시각화 (단계구분도·LISA·GWR 계수 지도) | `visualization.py` | ✅ |
|
|
61
|
+
| 한국 KOSIS·NSDI 실데이터 연결 | — | 🗺 로드맵 |
|
|
62
|
+
|
|
63
|
+
## 정확성 검증 · Validation (Columbus, queen 행표준화)
|
|
64
|
+
|
|
65
|
+
| 모형 | 상수 | INC | HOVAL | 공간계수 | logL | AIC |
|
|
66
|
+
|------|------|-----|-------|----------|------|-----|
|
|
67
|
+
| OLS (Anselin 1988) | 68.619 | −1.597 | −0.274 | — | −187.377 | 380.754 |
|
|
68
|
+
| SLM | 45.603 | −1.049 | −0.266 | ρ = 0.423 | −182.674 | 373.348 |
|
|
69
|
+
| SEM | 60.280 | −0.957 | −0.305 | λ = 0.547 | −183.749 | 373.499 |
|
|
70
|
+
| SDM | 44.320 | −0.920 | −0.297 | ρ = 0.403 | −181.639 | 375.279 |
|
|
71
|
+
|
|
72
|
+
위 계수는 Anselin (1988) 공표값과 일치합니다. LM 진단은 LM-Lag p = 0.003, LM-Error p = 0.023, Robust LM-Lag p = 0.053, Robust LM-Error p = 0.83 으로 SLM을 추천합니다. 전체 흐름과 지도는 [노트북](notebooks/01_columbus_full_analysis.ipynb)에 실행 결과와 함께 있습니다.
|
|
73
|
+
|
|
74
|
+
추가 회귀 검증은 합성 격자 자료로 수행합니다: 참값 복원(SLM ρ=0.6→0.61, SEM λ=0.5→0.39, SDM θ_x1=0.8→1.02), 총효과 항등식(행표준화 W에서 β/(1−ρ)), LM 규칙 4분기 판별, 체커보드 음의 자기상관 등.
|
|
75
|
+
|
|
76
|
+
## 기술 스택 · Tech Stack
|
|
77
|
+
|
|
78
|
+
- **Core:** Python 3.11+, PySAL (libpysal, spreg, esda), GeoPandas, statsmodels
|
|
79
|
+
- **GWR:** mgwr (선택 의존성, `pip install "uspatial[gwr]"`)
|
|
80
|
+
- **Visualization:** Matplotlib, mapclassify
|
|
81
|
+
|
|
82
|
+
## 프로젝트 구조 · Repository Structure
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
urban-spatial-analysis/
|
|
86
|
+
├── src/uspatial/
|
|
87
|
+
│ ├── weights.py # 공간 가중행렬 (W) 구축
|
|
88
|
+
│ ├── models.py # OLS·SLM·SEM·SDM·GWR, 모형 비교, 효과 분해, 모형 선택
|
|
89
|
+
│ ├── diagnostics.py # Moran's I · Geary's C · LISA · LM/Robust LM · VIF · Chow
|
|
90
|
+
│ ├── reporting.py # 출판용 계수표(Markdown/LaTeX)·모형 비교표
|
|
91
|
+
│ ├── cli.py # `python -m uspatial` (examples/diagnose/estimate/compare)
|
|
92
|
+
│ ├── data.py # 데이터 로딩·전처리
|
|
93
|
+
│ └── visualization.py # Choropleth · 군집 지도 · GWR 계수 지도
|
|
94
|
+
├── notebooks/
|
|
95
|
+
│ └── 01_columbus_full_analysis.ipynb # 전체 분석 워크플로(Columbus 예제)
|
|
96
|
+
├── tests/ # 단위·회귀 테스트 (Columbus 기준값 + 합성 격자 회귀)
|
|
97
|
+
├── legacy/ # 패키지 이전 초안 (동작하지 않음, 참고용 — git 이력 보존)
|
|
98
|
+
├── data/README.md # 데이터 출처 및 다운로드 안내
|
|
99
|
+
├── docs/ARCHITECTURE.md # 설계·방법론 문서
|
|
100
|
+
├── pyproject.toml # 패키징·ruff·pytest 설정
|
|
101
|
+
├── CONTRIBUTING.md
|
|
102
|
+
├── requirements.txt
|
|
103
|
+
└── LICENSE
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## 빠른 시작 · Quick Start
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
git clone https://github.com/DongsooJung/urban-spatial-analysis.git
|
|
110
|
+
cd urban-spatial-analysis
|
|
111
|
+
pip install -e ".[gwr,dev]" # 또는 pip install -r requirements.txt
|
|
112
|
+
jupyter notebook notebooks/01_columbus_full_analysis.ipynb
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 라이브러리로 쓰기
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from uspatial import build_weights, select_model, impacts_decomposition
|
|
119
|
+
|
|
120
|
+
w = build_weights(gdf, method="queen") # 행 표준화된 Queen W
|
|
121
|
+
|
|
122
|
+
# 1) OLS → LM/Robust LM 진단 → 모형 추천 (Anselin & Florax 1995 규칙)
|
|
123
|
+
selection = select_model(gdf, "log_price", ["inc", "dist_cbd"], w)
|
|
124
|
+
print(selection.recommendation) # 'OLS' | 'SLM' | 'SEM' | 'SDM'
|
|
125
|
+
print(selection.diagnostics.to_frame()) # LM 4종 통계량·p값
|
|
126
|
+
|
|
127
|
+
# 2) 추천 모형 적합 + 직접·간접·총 효과 (LeSage & Pace 2009)
|
|
128
|
+
sem = selection.fit(gdf, "log_price", ["inc", "dist_cbd"], w)
|
|
129
|
+
print(sem.summary())
|
|
130
|
+
print(impacts_decomposition(sem, draws=1000)) # 시뮬레이션 표준오차 포함
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 명령줄로 쓰기
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
python -m uspatial examples
|
|
137
|
+
python -m uspatial diagnose --data tracts.csv --y log_price --x inc,dist_cbd --coords lon,lat --knn 6
|
|
138
|
+
python -m uspatial estimate --data tracts.csv --y log_price --x inc,dist_cbd \
|
|
139
|
+
--coords lon,lat --knn 6 --method SEM --format markdown
|
|
140
|
+
python -m uspatial compare --data tracts.csv --y log_price --x inc,dist_cbd \
|
|
141
|
+
--coords lon,lat --knn 6 --methods OLS,SLM,SEM --json
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 테스트
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
pytest -q # 합성 격자 기반: 참값 복원력·효과 분해 항등식·LM 규칙 + Columbus 공표값
|
|
148
|
+
ruff check src tests
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## 연구 맥락 · Research Context
|
|
152
|
+
|
|
153
|
+
서울대학교 건설환경공학부(스마트도시공학) 박사과정 연구를 지원하며, 군 공항 이전 영향 분석과 대중교통중심개발(TOD) 평가 등 도시정책 평가에 적용됩니다.
|
|
154
|
+
|
|
155
|
+
This work supports doctoral research at Seoul National University, Department of Civil & Environmental Engineering (Smart City Engineering), applied to urban policy evaluation including military airport relocation impact analysis and transit-oriented development assessment.
|
|
156
|
+
|
|
157
|
+
## 참고문헌 · References
|
|
158
|
+
|
|
159
|
+
- Anselin, L. (1988). *Spatial Econometrics: Methods and Models*. Springer.
|
|
160
|
+
- LeSage, J. & Pace, R.K. (2009). *Introduction to Spatial Econometrics*. CRC Press.
|
|
161
|
+
- Anselin, L., Bera, A. K., Florax, R., & Yoon, M. J. (1996). Simple diagnostic tests for spatial dependence. *Regional Science and Urban Economics*, 26(1), 77–104.
|
|
162
|
+
- Anselin, L. (1995). Local indicators of spatial association — LISA. *Geographical Analysis*, 27(2), 93–115.
|
|
163
|
+
- Fotheringham, A. S., Brunsdon, C., & Charlton, M. (2002). *Geographically Weighted Regression*. Wiley.
|
|
164
|
+
- Rey, S.J. & Anselin, L. (2010). PySAL: A Python Library of Spatial Analytical Methods.
|
|
165
|
+
|
|
166
|
+
## 라이선스 · License
|
|
167
|
+
|
|
168
|
+
MIT License — 자세한 내용은 [LICENSE](LICENSE) 참조.
|
|
169
|
+
|
|
170
|
+
## 저자 · Author
|
|
171
|
+
|
|
172
|
+
**정동수 (Dongsoo Jung)** — 서울대학교 박사과정 · 스마트도시공학
|
|
173
|
+
[GitHub @DongsooJung](https://github.com/DongsooJung)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "uspatial"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Spatial econometric models for urban policy evaluation: spatial weights, Moran/LISA/LM diagnostics, OLS/SLM/SEM/SDM/GWR, LeSage-Pace impacts, publication tables"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Dongsoo Jung", email = "jds068888@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"spatial-econometrics",
|
|
17
|
+
"gis",
|
|
18
|
+
"urban-analytics",
|
|
19
|
+
"pysal",
|
|
20
|
+
"morans-i",
|
|
21
|
+
"spatial-lag",
|
|
22
|
+
"spatial-error",
|
|
23
|
+
"spatial-durbin",
|
|
24
|
+
"gwr",
|
|
25
|
+
"lisa",
|
|
26
|
+
"korea",
|
|
27
|
+
]
|
|
28
|
+
classifiers = [
|
|
29
|
+
"Development Status :: 4 - Beta",
|
|
30
|
+
"Intended Audience :: Science/Research",
|
|
31
|
+
"License :: OSI Approved :: MIT License",
|
|
32
|
+
"Operating System :: OS Independent",
|
|
33
|
+
"Programming Language :: Python :: 3",
|
|
34
|
+
"Programming Language :: Python :: 3.11",
|
|
35
|
+
"Programming Language :: Python :: 3.12",
|
|
36
|
+
"Topic :: Scientific/Engineering :: GIS",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# Core estimation stack. GWR lives in the optional `gwr` extra so a plain
|
|
40
|
+
# `pip install uspatial` does not pull mgwr's heavier dependency tree.
|
|
41
|
+
dependencies = [
|
|
42
|
+
"numpy>=1.26.0",
|
|
43
|
+
"pandas>=2.1.0",
|
|
44
|
+
"scipy>=1.11.0",
|
|
45
|
+
"geopandas>=0.14.0",
|
|
46
|
+
"shapely>=2.0.0",
|
|
47
|
+
"pyproj>=3.6.0",
|
|
48
|
+
"libpysal>=4.8.0",
|
|
49
|
+
"spreg>=1.4.0",
|
|
50
|
+
"esda>=2.5.0",
|
|
51
|
+
"statsmodels>=0.14.0",
|
|
52
|
+
"mapclassify>=2.6.0",
|
|
53
|
+
"matplotlib>=3.8.0",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[project.optional-dependencies]
|
|
57
|
+
gwr = ["mgwr>=2.1.2"]
|
|
58
|
+
viz = ["seaborn>=0.13.0", "folium>=0.15.0", "splot>=1.1.5"]
|
|
59
|
+
dev = [
|
|
60
|
+
"pytest>=7.4.0",
|
|
61
|
+
"mgwr>=2.1.2",
|
|
62
|
+
"pytest-cov>=4.1.0",
|
|
63
|
+
"black>=23.12.0",
|
|
64
|
+
# Pinned so a new ruff release cannot silently change CI lint rules.
|
|
65
|
+
"ruff>=0.17.6,<0.18",
|
|
66
|
+
"mypy>=1.7.0",
|
|
67
|
+
"build>=1.0.0",
|
|
68
|
+
"twine>=5.0.0",
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
[project.scripts]
|
|
72
|
+
uspatial = "uspatial.cli:main"
|
|
73
|
+
|
|
74
|
+
[project.urls]
|
|
75
|
+
Homepage = "https://github.com/DongsooJung/urban-spatial-analysis"
|
|
76
|
+
Repository = "https://github.com/DongsooJung/urban-spatial-analysis"
|
|
77
|
+
Issues = "https://github.com/DongsooJung/urban-spatial-analysis/issues"
|
|
78
|
+
Changelog = "https://github.com/DongsooJung/urban-spatial-analysis/blob/main/CHANGELOG.md"
|
|
79
|
+
|
|
80
|
+
[tool.setuptools]
|
|
81
|
+
package-dir = { "" = "src" }
|
|
82
|
+
|
|
83
|
+
[tool.setuptools.packages.find]
|
|
84
|
+
where = ["src"]
|
|
85
|
+
|
|
86
|
+
[tool.setuptools.dynamic]
|
|
87
|
+
version = { attr = "uspatial.__version__" }
|
|
88
|
+
|
|
89
|
+
[tool.ruff]
|
|
90
|
+
target-version = "py311"
|
|
91
|
+
line-length = 100
|
|
92
|
+
|
|
93
|
+
[tool.ruff.lint]
|
|
94
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM"]
|
|
95
|
+
ignore = [
|
|
96
|
+
"E741", # 짧은 수학 표기(L, I 등)를 변수명으로 쓰는 관행
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
[tool.ruff.lint.per-file-ignores]
|
|
100
|
+
# 테스트는 `pytest.importorskip` 뒤에 import를 두는 관행(E402)과 긴 표 문자열을 허용한다.
|
|
101
|
+
"tests/*" = ["E501", "E402"]
|
|
102
|
+
|
|
103
|
+
[tool.pytest.ini_options]
|
|
104
|
+
pythonpath = ["src"]
|
|
105
|
+
testpaths = ["tests"]
|
|
106
|
+
addopts = "-ra"
|
|
107
|
+
filterwarnings = [
|
|
108
|
+
# PySAL emits harmless numeric warnings on small synthetic systems.
|
|
109
|
+
"ignore::RuntimeWarning",
|
|
110
|
+
"ignore::FutureWarning",
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
[tool.coverage.run]
|
|
114
|
+
source = ["uspatial"]
|
|
115
|
+
branch = true
|
|
116
|
+
|
|
117
|
+
[tool.coverage.report]
|
|
118
|
+
show_missing = true
|
|
119
|
+
skip_covered = true
|
uspatial-0.2.0/setup.cfg
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Urban Spatial Econometric Analysis — 공간계량 분석 라이브러리
|
|
3
|
+
|
|
4
|
+
PySAL(spreg·esda·libpysal)과 mgwr를 감싸 도시·부동산 연구의 표준 워크플로를
|
|
5
|
+
제공한다: 가중행렬 → 공간 진단(Moran's I / LISA / LM) → 모형 추정
|
|
6
|
+
(OLS·SLM·SEM·SDM·GWR) → 직접·간접효과 분해 → 출판용 표.
|
|
7
|
+
|
|
8
|
+
사용 예:
|
|
9
|
+
>>> from uspatial import SpatialModel, build_weights, select_model
|
|
10
|
+
>>> w = build_weights(gdf, method="queen")
|
|
11
|
+
>>> selection = select_model(gdf, "log_price", ["inc", "dist_cbd"], w)
|
|
12
|
+
>>> print(selection.recommendation) # 'SLM' | 'SEM' | 'OLS' | 'SDM'
|
|
13
|
+
>>> result = selection.fit(gdf, "log_price", ["inc", "dist_cbd"], w)
|
|
14
|
+
>>> print(result.summary())
|
|
15
|
+
|
|
16
|
+
명령줄:
|
|
17
|
+
python -m uspatial examples
|
|
18
|
+
python -m uspatial estimate --data tracts.csv --y log_price --x inc,dist_cbd \\
|
|
19
|
+
--coords lon,lat --knn 6 --method SEM
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from uspatial.data import describe_dataset, load_example
|
|
23
|
+
from uspatial.diagnostics import (
|
|
24
|
+
LisaResult,
|
|
25
|
+
LMTestResult,
|
|
26
|
+
MoranResult,
|
|
27
|
+
gearys_c,
|
|
28
|
+
lm_diagnostics,
|
|
29
|
+
local_moran,
|
|
30
|
+
morans_i,
|
|
31
|
+
residual_moran,
|
|
32
|
+
spatial_chow_test,
|
|
33
|
+
variance_inflation_factor,
|
|
34
|
+
)
|
|
35
|
+
from uspatial.models import (
|
|
36
|
+
SelectionResult,
|
|
37
|
+
SpatialModel,
|
|
38
|
+
SpatialResults,
|
|
39
|
+
compare_models,
|
|
40
|
+
impacts_decomposition,
|
|
41
|
+
select_model,
|
|
42
|
+
)
|
|
43
|
+
from uspatial.reporting import (
|
|
44
|
+
coefficient_table,
|
|
45
|
+
latex_table,
|
|
46
|
+
markdown_table,
|
|
47
|
+
model_comparison_table,
|
|
48
|
+
)
|
|
49
|
+
from uspatial.weights import build_weights, weights_summary
|
|
50
|
+
|
|
51
|
+
__version__ = "0.2.0"
|
|
52
|
+
__author__ = "Dongsoo Jung"
|
|
53
|
+
__email__ = "jds068888@gmail.com"
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
# models
|
|
57
|
+
"SpatialModel",
|
|
58
|
+
"SpatialResults",
|
|
59
|
+
"SelectionResult",
|
|
60
|
+
"select_model",
|
|
61
|
+
"compare_models",
|
|
62
|
+
"impacts_decomposition",
|
|
63
|
+
# diagnostics
|
|
64
|
+
"morans_i",
|
|
65
|
+
"gearys_c",
|
|
66
|
+
"local_moran",
|
|
67
|
+
"lm_diagnostics",
|
|
68
|
+
"variance_inflation_factor",
|
|
69
|
+
"spatial_chow_test",
|
|
70
|
+
"residual_moran",
|
|
71
|
+
"MoranResult",
|
|
72
|
+
"LisaResult",
|
|
73
|
+
"LMTestResult",
|
|
74
|
+
# weights & data
|
|
75
|
+
"build_weights",
|
|
76
|
+
"weights_summary",
|
|
77
|
+
"load_example",
|
|
78
|
+
"describe_dataset",
|
|
79
|
+
# reporting
|
|
80
|
+
"coefficient_table",
|
|
81
|
+
"markdown_table",
|
|
82
|
+
"latex_table",
|
|
83
|
+
"model_comparison_table",
|
|
84
|
+
]
|