在预测碳排放量时,我使用ols模型,结果出现很多nan,inf
代码
import numpy as np
import pandas as pd
#载入数据
excel_path = r"Testingdata_shiyou.xlsx"
data_df = pd.read_excel(excel_path)
# 给列名重新命名,每一列就是一个自(因)变量的观测值
data_df.columns = ['year', 'I', 'P', 'A', 'Te', 'Ts', 'Td', 'Tc']
# 量纲恢复
# 数据标准化
def normalize(data):
"""对因变量作标准化变换,标准化因子是标准差"""
return (data - data.mean()) / data.std()
#%%
import statsmodels.api as sm
X = np.log(data_df.iloc[:, 2:])
def mask_nan(X,value):
'''
用value值来代替nan 或inf
'''
X_values= tf.add(tf.zeros_like(X),value)
mask = tf.math.is_finite(X)
y = tf.where(mask,X,X_values)
return y
y = np.log(data_df.iloc[:, 1])
X = sm.add_constant(X)
ols_model = sm.OLS(y,X).fit()
print(ols_model.summary())
结果
OLS Regression Results
==============================================================================
Dep. Variable: I R-squared: 1.000
Model: OLS Adj. R-squared: nan
Method: Least Squares F-statistic: nan
Date: Wed, 13 Apr 2022 Prob (F-statistic): nan
Time: 16:29:49 Log-Likelihood: 148.10
No. Observations: 5 AIC: -286.2
Df Residuals: 0 BIC: -288.2
Df Model: 4
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const -0.0678 inf -0 nan nan nan
P 0.9989 inf 0 nan nan nan
A 0.9892 inf 0 nan nan nan
Te 0.9672 inf 0 nan nan nan
Ts -0.2405 inf -0 nan nan nan
Td 0.0003 inf 0 nan nan nan
Tc 0.9808 inf 0 nan nan nan
==============================================================================
Omnibus: nan Durbin-Watson: 0.035
Prob(Omnibus): nan Jarque-Bera (JB): 0.362
Skew: -0.201 Prob(JB): 0.834
Kurtosis: 1.745 Cond. No. 631.
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The input rank is higher than the number of observations.
想请各位帮忙看看如何解决结果里的nan和inf