python statsmodels:输出差异 "formula.api" 与“"regression.quantile_regression"
python statsmodels: Difference in output "formula.api" vs. ""regression.quantile_regression"
对于使用 python
的模块 statsmodels
,我想知道使用 statsmodels.formula.api
与 statsmodels.regression.quantile_regression
调用相同过程的差异是如何产生的。
特别是,我获得了参数估计的差异。
附上一个最小工作示例。
#%% Moduls;
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
#%% smf-Version;
model1 = smf.quantreg(formula='foodexp ~ income', data=data, missing="drop")
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% QuantReg-Version;
model2 = QuantReg \
(
data['foodexp'].values,
exog = sm.tools.tools.add_constant(data['income']).values,
missing = 'drop'
)
result2 = model2.fit \
(
q = 0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06
)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
编辑:
我需要编辑我的问题;我仍然非常感谢下面提出的解决方法,但在应用设置中不起作用;原因:我没有只有 1 个回归量。
请查找附件中的修改版本。
#%% Moduls;
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
data['income2'] = data['income']**2
#%% smf-Version;
model1 = smf.quantreg(formula='foodexp ~ income + income2', data=data, missing="drop")
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% QuantReg-Version;
model2 = QuantReg \
(
data['foodexp'].values,
exog = sm.tools.tools.add_constant(data[['income', 'income2']].values),
missing = 'drop'
)
result2 = model2.fit \
(
q = 0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06
)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
您需要对代码稍作改动。这有很大的不同
#%% QuantReg-Version;
model2 = QuantReg ( data['foodexp'].values, exog = sm.tools.tools.add_constant(data['income'].values), missing = 'drop')
当你把它放在外面时,在内部实现上有很大的不同。
最终实施
#%% Moduls;
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
#%% smf-Version;
model1 = smf.quantreg(formula='foodexp ~ income', data=data, missing="drop")
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather',
max_iter=1000, p_tol=1e-06)
#%% QuantReg-Version;
model2 = QuantReg \
(
data['foodexp'].values,
exog = sm.tools.tools.add_constant(data['income'].values),
missing = "drop"
)
result2 = model2.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
除了我上面的代码。我已将 exog 从模型 2 复制到模型 1
#%% Moduls;
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
data['income2'] = data['income']**2
model1 = smf.quantreg(formula='foodexp ~ income + income2', data=data, missing="drop")
model2 = QuantReg (data['foodexp'].values, exog = sm.tools.tools.add_constant(data[['income', 'income2']].values), missing = 'drop')
model1.exog = model2.exog
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
result2 = model2.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
第二种方法:- 我已将 exog 从模型 1 复制到模型 2
#%% Moduls;
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
data['income2'] = data['income']**2
model1 = smf.quantreg(formula='foodexp ~ income + income2', data=data, missing="drop")
model2 = QuantReg (data['foodexp'].values, exog = sm.tools.tools.add_constant(data[['income', 'income2']].values), missing = 'drop')
model2.exog = model1.exog
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
result2 = model2.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
如果我将两个 exog 保持为相同的值,则答案是相等的。因此,我之前提到的数据转换的实现存在明显差异。
对于使用 python
的模块 statsmodels
,我想知道使用 statsmodels.formula.api
与 statsmodels.regression.quantile_regression
调用相同过程的差异是如何产生的。
特别是,我获得了参数估计的差异。
附上一个最小工作示例。
#%% Moduls;
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
#%% smf-Version;
model1 = smf.quantreg(formula='foodexp ~ income', data=data, missing="drop")
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% QuantReg-Version;
model2 = QuantReg \
(
data['foodexp'].values,
exog = sm.tools.tools.add_constant(data['income']).values,
missing = 'drop'
)
result2 = model2.fit \
(
q = 0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06
)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
编辑:
我需要编辑我的问题;我仍然非常感谢下面提出的解决方法,但在应用设置中不起作用;原因:我没有只有 1 个回归量。 请查找附件中的修改版本。
#%% Moduls;
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
data['income2'] = data['income']**2
#%% smf-Version;
model1 = smf.quantreg(formula='foodexp ~ income + income2', data=data, missing="drop")
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% QuantReg-Version;
model2 = QuantReg \
(
data['foodexp'].values,
exog = sm.tools.tools.add_constant(data[['income', 'income2']].values),
missing = 'drop'
)
result2 = model2.fit \
(
q = 0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06
)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
您需要对代码稍作改动。这有很大的不同
#%% QuantReg-Version;
model2 = QuantReg ( data['foodexp'].values, exog = sm.tools.tools.add_constant(data['income'].values), missing = 'drop')
当你把它放在外面时,在内部实现上有很大的不同。
最终实施
#%% Moduls;
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
#%% smf-Version;
model1 = smf.quantreg(formula='foodexp ~ income', data=data, missing="drop")
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather',
max_iter=1000, p_tol=1e-06)
#%% QuantReg-Version;
model2 = QuantReg \
(
data['foodexp'].values,
exog = sm.tools.tools.add_constant(data['income'].values),
missing = "drop"
)
result2 = model2.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
除了我上面的代码。我已将 exog 从模型 2 复制到模型 1
#%% Moduls;
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
data['income2'] = data['income']**2
model1 = smf.quantreg(formula='foodexp ~ income + income2', data=data, missing="drop")
model2 = QuantReg (data['foodexp'].values, exog = sm.tools.tools.add_constant(data[['income', 'income2']].values), missing = 'drop')
model1.exog = model2.exog
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
result2 = model2.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
第二种方法:- 我已将 exog 从模型 1 复制到模型 2
#%% Moduls;
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.regression.quantile_regression import QuantReg
#%% Load in sample data;
data = sm.datasets.engel.load_pandas().data
data['income2'] = data['income']**2
model1 = smf.quantreg(formula='foodexp ~ income + income2', data=data, missing="drop")
model2 = QuantReg (data['foodexp'].values, exog = sm.tools.tools.add_constant(data[['income', 'income2']].values), missing = 'drop')
model2.exog = model1.exog
result1 = model1.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
result2 = model2.fit(q=0.5, vcov='robust', kernel='epa', bandwidth='hsheather', max_iter=1000, p_tol=1e-06)
#%% Compare Results;
print(result1.params[0])
print(result2.params[0])
print('Difference times 10^9: ' + str(abs(10**9*(result1.params[0]-result2.params[0]))))
如果我将两个 exog 保持为相同的值,则答案是相等的。因此,我之前提到的数据转换的实现存在明显差异。