Với một số dữ liệu được tạo ra một cách ngẫu nhiên vớiPoisson Regression trong statsmodels và R
- 2 cột,
- 50 hàng và
- dãy số nguyên giữa 0-100
Với R , thì có thể đạt được âm mưu tiêu chuẩn và ma trận chẩn đoán như sau:
> col=2
> row=50
> range=0:100
> df <- data.frame(replicate(col,sample(range,row,rep=TRUE)))
> model <- glm(X2 ~ X1, data = df, family = poisson)
> glm.diag.plots(model)
Trong Python, điều này sẽ cho tôi dòng dự đoán vs dư âm mưu:
import numpy as np
import pandas as pd
import statsmodels.formula.api
from statsmodels.genmod.families import Poisson
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randint(100, size=(50,2)))
df.rename(columns={0:'X1', 1:'X2'}, inplace=True)
glm = statsmodels.formula.api.gee
model = glm("X2 ~ X1", groups=None, data=df, family=Poisson())
results = model.fit()
Và âm mưu chẩn đoán bằng Python:
model_fitted_y = results.fittedvalues # fitted values (need a constant term for intercept)
model_residuals = results.resid # model residuals
model_abs_resid = np.abs(model_residuals) # absolute residuals
plot_lm_1 = plt.figure(1)
plot_lm_1.set_figheight(8)
plot_lm_1.set_figwidth(12)
plot_lm_1.axes[0] = sns.residplot(model_fitted_y, 'X2', data=df, lowess=True, scatter_kws={'alpha': 0.5}, line_kws={'color': 'red', 'lw': 1, 'alpha': 0.8})
plot_lm_1.axes[0].set_xlabel('Line Predictor')
plot_lm_1.axes[0].set_ylabel('Residuals')
plt.show()
Nhưng khi tôi cố gắng để có được số liệu thống kê nấu ăn,
# cook's distance, from statsmodels internals
model_cooks = results.get_influence().cooks_distance[0]
nó ném một lỗi nói:
AttributeError Traceback (most recent call last)
<ipython-input-66-0f2bedfa1741> in <module>()
4 model_residuals = results.resid
5 # normalized residuals
----> 6 model_norm_residuals = results.get_influence().resid_studentized_internal
7 # absolute squared normalized residuals
8 model_norm_residuals_abs_sqrt = np.sqrt(np.abs(model_norm_residuals))
/opt/conda/lib/python3.6/site-packages/statsmodels/base/wrapper.py in __getattribute__(self, attr)
33 pass
34
---> 35 obj = getattr(results, attr)
36 data = results.model.data
37 how = self._wrap_attrs.get(attr)
AttributeError: 'GEEResults' object has no attribute 'get_influence'
Có cách nào để vẽ ra tất cả 4 lô chẩn đoán bằng Python như trong R?
Làm cách nào để truy lục thống kê đầu bếp của mô hình được trang bị kết quả bằng Python sử dụng statsmodels
?
các biện pháp ngoại lệ và ảnh hưởng chỉ có sẵn cho OLS và có thể WLS. (Nó có thể không khó khăn để sử dụng một số dư GLM, nhưng nó sẽ cần xét nghiệm đơn vị chống lại R hoặc Stata. GEE có thể khó khăn hơn.) – user333700
Đối với một số mục đích, R thực sự là vua. Trong khi Python có mã tối thiểu và ngắn hơn R, rất nhiều công việc được thực hiện chỉ trong một số ít các lệnh trong ngôn ngữ thứ hai. Tôi nhớ các lệnh của R;) –