Skip to main content

各向异性地理加权回归模型实现 (VGWR)

Project description

AGWR - 各向异性地理加权回归模型 (VGWR)

Python 3.8+ License: MIT PyPI version

一个用于实现各向异性地理加权回归(Variably Geographically Weighted Regression, VGWR)的Python包。该包基于研究论文中的数学理论,提供了稳定、高效的VGWR模型实现。

功能特点

  • 各向异性核函数: 支持方向性距离计算和各向异性带宽参数
  • CA算法: 使用条件数算法选择代表性位置点
  • 模型拟合: 使用最大似然估计和L-BFGS-B优化算法
  • 空间变系数: 支持部分变量具有空间变化的系数
  • 模型诊断: 提供AIC、R²、残差分析等诊断工具
  • 易于使用: 简洁的API设计,类似scikit-learn的使用方式

安装

从PyPI安装(推荐)

pip install agwr

从源码安装

git clone https://github.com/agwr-team/agwr.git
cd agwr
pip install -e .

依赖要求

  • Python >= 3.8
  • numpy >= 1.20.0
  • pandas >= 1.3.0
  • scipy >= 1.7.0

快速开始

基本用法

import numpy as np
from agwr import VGWR

# 准备数据
n = 100
u = np.random.rand(n) * 10  # 经度坐标
v = np.random.rand(n) * 10   # 纬度坐标

# 生成示例数据
z1 = np.random.randn(n)
z2 = np.random.randn(n)
z3 = np.random.randn(n)
z4 = np.random.randn(n)
z5 = np.random.randn(n)
z6 = np.random.randn(n)

# 生成因变量(示例)
y = (1.0 + 0.5*z1 + 0.3*z2 + 0.2*z3 + 
     0.1*z4 + 0.15*z5 + 0.25*z6 + 
     np.random.randn(n) * 0.1)

# 创建并拟合模型
model = VGWR(m=20, max_iter=100)
model.fit(y, z1, z2, z3, z4, z5, z6, u, v)

# 查看结果
model.summary()

# 获取局部系数
coefficients = model.get_local_coefficients()
print(f"beta2 平均值: {np.mean(coefficients['beta2']):.4f}")
print(f"beta4 平均值: {np.mean(coefficients['beta4']):.4f}")
print(f"beta6 平均值: {np.mean(coefficients['beta6']):.4f}")

预测新数据

# 准备新数据
n_new = 50
u_new = np.random.rand(n_new) * 10
v_new = np.random.rand(n_new) * 10
z1_new = np.random.randn(n_new)
z2_new = np.random.randn(n_new)
z3_new = np.random.randn(n_new)
z4_new = np.random.randn(n_new)
z5_new = np.random.randn(n_new)
z6_new = np.random.randn(n_new)

# 进行预测
y_pred = model.predict(z1_new, z2_new, z3_new, z4_new, 
                       z5_new, z6_new, u_new, v_new)
print(f"预测值范围: [{y_pred.min():.4f}, {y_pred.max():.4f}]")

可视化空间变系数

from agwr import plot_spatial_coefficients

# 绘制 beta2 的空间分布
plot_spatial_coefficients(model, u, v, variable_name='beta2')

# 绘制 beta4 的空间分布
plot_spatial_coefficients(model, u, v, variable_name='beta4')

# 绘制 beta6 的空间分布
plot_spatial_coefficients(model, u, v, variable_name='beta6')

模型理论

VGWR模型基于以下数学公式:

各向异性核函数

R(h, θ) = exp(-Σ(h²/θ))

其中:

  • h 是方向性距离向量 [h_u, h_v]
  • θ 是各向异性带宽向量 [θ_u, θ_v]

空间变系数表示

模型使用重构参数化方法来表示空间变化的系数:

β(x) = ∑ᵢ γᵢ bᵢ(x)

其中 bᵢ(x) 是基函数,γᵢ 是节点系数。

模型形式

VGWR模型的形式为:

y = β₀ + β₁z₁ + β₂(u,v)z₂ + β₃z₃ + β₄(u,v)z₄ + β₅z₅ + β₆(u,v)z₆ + ε

其中:

  • β₀, β₁, β₃, β₅ 是全局常数系数
  • β₂(u,v), β₄(u,v), β₆(u,v) 是空间变化的系数

负对数似然函数

L = (n/2) log(2πσ²) + RSS/(2σ²)

其中 RSS 是残差平方和。

API参考

VGWR类

初始化参数

  • m (int): 代表点数量,默认为20
  • max_iter (int): 优化最大迭代次数,默认为100
  • random_state (int, optional): 随机种子

主要方法

  • fit(y, z1, z2, z3, z4, z5, z6, u, v, initial_theta=None, verbose=True): 拟合模型
  • predict(z1_new, z2_new, z3_new, z4_new, z5_new, z6_new, u_new, v_new): 进行预测
  • summary(): 打印模型摘要
  • get_local_coefficients(): 获取所有位置的局部系数

属性

  • sig: 误差标准差
  • theta2, theta4, theta6: 各向异性带宽参数
  • beta0, beta1, beta3, beta5: 全局系数
  • betv2, betv4, betv6: 空间变系数
  • AIC: Akaike信息准则
  • R_squared: 决定系数
  • fitted_values: 拟合值
  • residuals: 残差

工具函数

  • RV(theta, h): 计算各向异性核函数值
  • CA_algorithm(x, m, max_iter): 使用条件数算法选择代表点
  • plot_spatial_coefficients(model, u, v, variable_name): 绘制空间变系数的分布图

测试

运行测试套件:

cd agwr_package
python -m pytest tests/

或者运行单个测试:

python tests/test_model.py

贡献

欢迎贡献代码!请遵循以下步骤:

  1. Fork本仓库
  2. 创建功能分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 创建Pull Request

许可证

本项目采用MIT许可证 - 查看 LICENSE 文件了解详情。

引用

如果您在研究中使用了AGWR包,请引用相关论文:

@article{agwr2024,
  title={各向异性地理加权回归模型研究},
  author={AGWR开发团队},
  journal={空间统计学报},
  year={2024}
}

联系方式

更新日志

v2.0.0 (2024-01-01)

  • 重构为VGWR模型实现
  • 实现各向异性核函数
  • 支持空间变系数估计
  • 提供CA算法选择代表点
  • 包含完整的测试套件

v1.0.0 (2024-01-01)

  • 初始版本发布
  • 实现基本的AGWR模型
  • 支持各向异性核函数
  • 提供坐标投影功能
  • 包含完整的测试套件

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agwr-2.0.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agwr-2.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file agwr-2.0.0.tar.gz.

File metadata

  • Download URL: agwr-2.0.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for agwr-2.0.0.tar.gz
Algorithm Hash digest
SHA256 ad9c20ad1e03b17756e627dd622aad8f45d2871f916dc7174a24cef31e00fa27
MD5 45c610ef4ebe878a5449a97ef5b576d8
BLAKE2b-256 2c47fa02ae93602c20d48f6f252bb8f3a65d6eed34cb94ab18277341305bf24f

See more details on using hashes here.

File details

Details for the file agwr-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: agwr-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for agwr-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fd825fea0aa7a6530ec0986c713d6ef0924643b557fc0374a22aa4b7c29b62d
MD5 01ad506251a51e8ce237e011a4d9a2e6
BLAKE2b-256 dcb827fbbc73613834ec4ce37596b0de28d9a977d4c4f7a495f097b35d583a9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page