12345678910111213141516171819202122232425 |
- import pandas as pd
- import numpy as np
- from statsmodels.stats.outliers_influence import variance_inflation_factor
- # 实际数据
- df = pd.read_csv("paper_data.csv")
- df = df.drop(columns=["before_last_NO2", "before_last_O3", "last_year_pm10",
- "before_last_pm1", "before_last_pm2.5","last_year_pm1","last_year_OM","last_year_BC",
- 'before_last_pm10', 'last_year_NO3', 'last_year_NH4', 'before_last_SO4',
- 'before_last_NO3', 'before_last_NH4', 'before_last_OM', 'before_last_BC', 'before_last_nl'])
- # 计算每个变量的 VIF方差膨胀因子
- def calculate_vif(df):
- vif_data = pd.DataFrame()
- vif_data["Variable"] = df.columns
- vif_data["VIF"] = [
- variance_inflation_factor(df.values, i) for i in range(df.shape[1])
- ]
- return vif_data
- # 调用函数计算 VIF
- vif_result = calculate_vif(df)
- # 输出 VIF 结果
- print(vif_result)
|