我有一个模型,定义如下:
import statsmodels.formula.api as smf
model = smf.glm(formula="A ~ B + C + D", data=data, family=sm.families.Poisson()).fit()
Run Code Online (Sandbox Code Playgroud)
该模型具有如下所示的系数:
Intercept 0.319813
C[T.foo] -1.058058
C[T.bar] -0.749859
D[T.foo] 0.217136
D[T.bar] 0.404791
B 0.262614
Run Code Online (Sandbox Code Playgroud)
我可以抓住的值Intercept,并B通过做model.params.Intercept和model.params.B,但我不能让每个值C和D.
我试过model.params.C[T.foo],例如,我得到了错误.
我如何从模型中获取特定值?
我尝试将add_constant()函数与数据集数组一起使用.在索引59处它起作用(列被创建)但在索引60处它不被创建.最初,testmat [59]的形状为(24,54),testmat [60]的形状为(9,54).以下是我运行add_constant函数时得到的结果:
In: Xnew = sm.add_constant(testmat[59])
Out:
const TRYSIL_PO TRYSIL_TA
6142 1 985.7 271.65
6143 1 984.9 271.85
Run Code Online (Sandbox Code Playgroud)
In: Xnew = sm.add_constant(testmat[60]):
Out:
TRYSIL_PO TRYSIL_TA
6166 983.6 272.75
6167 983.1 272.85
Run Code Online (Sandbox Code Playgroud)
我已经检查了其他来源,但似乎这个没有得到解决.你有解释吗?
我正在使用statsmodels.tsa.SARIMAX()来训练具有外生变量的模型.当使用外生变量训练模型时,是否存在等效的get_prediction(),以便返回的对象包含预测的平均值和置信区间而不仅仅是一组预测的平均值结果?predict()和forecast()方法采用外生变量,但只返回预测的平均值.
SARIMA_model = sm.tsa.SARIMAX(endog=y_train.astype('float64'),
exog=ExogenousFeature_train.values.astype('float64'),
order=(1,0,0),
seasonal_order=(2,1,0,7),
simple_differencing=False)
model_results = SARIMA_model.fit()
pred = model_results.predict(start=train_end_date,
end=test_end_date,
exog=ExogenousFeature_test.values.astype('float64').reshape(343,1),
dynamic=False)
Run Code Online (Sandbox Code Playgroud)
这里的pred是一个预测值数组,而不是一个包含预测平均值和置信区间的对象,如果你运行get_predict(),你会得到它们.注意,get_predict()不接受外生变量.
我的statsmodels版本是0.8
python time-series confidence-interval forecasting statsmodels
.summary()使用 pandas statsmodels的函数时,OLS 回归结果包括以下字段。
coef std err t P>|t| [0.025 0.975]
Run Code Online (Sandbox Code Playgroud)
如何获得标准化系数(不包括截距),类似于 SPSS 中可实现的?
我是 Python ARIMA 实现的新手。我有几个月的数据,频率为 15 分钟。在我尝试遵循 Box-Jenkins 方法来拟合时间序列模型时。我在最后遇到了一个问题。给出了时间序列 (ts) 和差异序列 (ts_diff)的ACF-PACF 图。我使用了 ARIMA (5,1,2),最后我绘制了拟合值(绿色)和原始值(蓝色)。从图中可以看出,值有明显的变化(一个)。我究竟做错了什么?
预测不好吗?任何见解都会有所帮助。
有没有办法像 statsmodels 一样为 scikit 逻辑回归模型提供类似的、不错的输出?有了所有的 p 值,标准。一张表中的错误等?
我尝试使用holt-winters model如下所示的预测,但我不断得到与我期望的预测不一致的预测.我还展示了情节的可视化
Train = Airline[:130]
Test = Airline[129:]
from statsmodels.tsa.holtwinters import Holt
y_hat_avg = Test.copy()
fit1 = Holt(np.asarray(Train['Passengers'])).fit()
y_hat_avg['Holt_Winter'] = fit1.predict(start=1,end=15)
plt.figure(figsize=(16,8))
plt.plot(Train.index, Train['Passengers'], label='Train')
plt.plot(Test.index,Test['Passengers'], label='Test')
plt.plot(y_hat_avg.index,y_hat_avg['Holt_Winter'], label='Holt_Winter')
plt.legend(loc='best')
plt.savefig('Holt_Winters.jpg')
Run Code Online (Sandbox Code Playgroud)
我不确定我在这里缺少什么.

预测似乎适用于训练数据的早期部分
我想使用statsmodelsOLS 类来创建多元回归模型。考虑以下数据集:
import statsmodels.api as sm
import pandas as pd
import numpy as np
dict = {'industry': ['mining', 'transportation', 'hospitality', 'finance', 'entertainment'],
'debt_ratio':np.random.randn(5), 'cash_flow':np.random.randn(5) + 90}
df = pd.DataFrame.from_dict(dict)
x = data[['debt_ratio', 'industry']]
y = data['cash_flow']
def reg_sm(x, y):
x = np.array(x).T
x = sm.add_constant(x)
results = sm.OLS(endog = y, exog = x).fit()
return results
Run Code Online (Sandbox Code Playgroud)
当我运行以下代码时:
reg_sm(x, y)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
TypeError: '>=' not supported between instances of 'float' and 'str'
Run Code Online (Sandbox Code Playgroud)
我已经尝试将industry变量转换为分类变量,但仍然出现错误。我别无选择。
我正在使用带有 Patsy 包的 R 风格公式来处理 statsmodels,并收到一个我无法理解的错误,任何提示或技巧将不胜感激。
PatsyError:数据参数和 C('Industry_Banking&CapitalMarkets') 之间的行数不匹配(8137 与 1)
DataFrame 有 8137 行并且没有丢失数据
完整代码如下
mixed = smf.mixedlm("""count_SoldServiceName ~ date_int + AzureActiveEngagementCount + AzureEngagementPartnerCount
+ DCount_learning_path_name + Industry_Automotive + C('Industry_Banking&CapitalMarkets') + C('Industry_Chemicals&Agrochemicals') + Industry_CivilianGovernment
+ Industry_ConsumerGoods + C('Industry_Defense&Intelligence') + Industry_DiscreteManufacturing + Industry_Energy + Industry_Gaming
+ Industry_HealthPayor + Industry_HealthProvider + Industry_HigherEducation + Industry_Insurance + C('Industry_Media&Entertainment') + Industry_Nonprofit
+ Industry_PartnerProfessionalServices + Industry_Pharmaceuticals + C('Industry_Primary&SecondaryEdu/K-12') + Industry_ProfessionalServices
+ C('Industry_PublicSafety&Justice') + Industry_Retailers + Industry_SmartSpaces + Industry_Telecommunications + C('Industry_Travel,Transport&Hospitality')
+ Industry_other + InvestmentArea_AA + …Run Code Online (Sandbox Code Playgroud) 我想通过statsmodels STL方法进行分解
我的时间序列数据如下所示:
success.rate
Date
2020-09-11 24.735701
2020-09-14 24.616301
2020-09-15 24.695900
2020-09-16 24.467051
2020-09-17 24.118799
Run Code Online (Sandbox Code Playgroud)
当我把它放入STL中时
STL(sdf, seasonal=20, robust=True)
Run Code Online (Sandbox Code Playgroud)
我总是收到这样的错误:
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/mnt/d/mywork/test
STL(sdf,seasonal=20, robust=True)
----> 1 STL(sdf, seasonal=20, robust=True)
statsmodels/tsa/_stl.pyx in statsmodels.tsa._stl.STL.__init__()
ValueError: Unable to determine period from endog
Run Code Online (Sandbox Code Playgroud) statsmodels ×10
python ×8
pandas ×4
forecasting ×2
time-series ×2
coefficients ×1
holtwinters ×1
patsy ×1
python-3.x ×1
regression ×1
scikit-learn ×1
statistics ×1