<jupyter_start><jupyter_text>Linear Model<jupyter_code>y = nu + np.random.normal(size=N)
model = GLM(family=Gaussian())
model.fit(X, y)
model.coef_
model.coef_covariance_matrix_
model.coef_standard_error_
model.p_values_
yhat = model.predict(X)
yhat[:10]
mod = sm.OLS(y, X)
res = mod.fit()
print(res.summary())
res.params
res.bse<jupyter_output><empty_output><jupyter_text>Linear Regression With Formula<jupyter_code>df = pd.DataFrame(
    np.concatenate([X[:, 1:], y.reshape(-1, 1)], axis=1),
    columns=['Moshi', 'SwimSwim', 'y'])
df.shape
model = GLM(family=Gaussian())
model.fit(df, formula='y ~ Moshi + SwimSwim')
model.coef_
model.summary()
yhat = model.predict(df)
yhat[:10]
model.formula<jupyter_output><empty_output><jupyter_text>Run some simulations off the linear model.<jupyter_code>s = Simulation(model)
s.sample(X)
models = s.parametric_bootstrap(X, n_sim=10)
for model in models:
    print(model.coef_)
models = s.non_parametric_bootstrap(X, y, n_sim=10)
for model in models:
    print(model.coef_)<jupyter_output>[ 1.08899747 -1.97909035  0.0242303 ]
[ 1.1438153  -2.0103238   0.03155373]
[  1.07893939e+00  -1.87856140e+00  -1.24740123e-03]
[ 1.06236628 -1.91918097 -0.01660036]
[ 1.05378187 -2.0633577  -0.07282795]
[ 1.07175649 -1.95840599  0.06544901]
[ 1.09868402 -2.00568189 -0.08815472]
[ 1.03488206 -2.00576657  0.05256831]
[ 1.12335559 -1.86088373 -0.0611884 ]
[ 1.16289827 -1.96426665  0.00614709]<jupyter_text>Linear Model with Sample Weights<jupyter_code>sample_weights = np.random.uniform(0, 2, size=N)
model = GLM(family=Gaussian())
model = model.fit(X, y, sample_weights=sample_weights)
model.coef_<jupyter_output><empty_output><jupyter_text>Logistic Model<jupyter_code>p = 1 / (1 + np.exp(-nu))
y_logistic = np.random.binomial(1, p=p, size=N)
model = GLM(family=Bernoulli())
model.fit(X, y_logistic)
model.coef_
model.dispersion_
model.coef_covariance_matrix_
model.coef_standard_error_
model.p_values_
mod = sm.Logit(y_logistic, X)
res = mod.fit()
print(res.summary())
s = Simulation(model)
s.sample(X, n_sim=10)
for model in s.parametric_bootstrap(X, n_sim=10):
    print(model.coef_)
for model in s.non_parametric_bootstrap(X, y_logistic, n_sim=10):
    print(model.coef_)<jupyter_output>[ 0.97689309 -1.97413283  1.10627714]
[ 0.95892487 -1.99413312  0.98665457]
[ 0.94129474 -1.94450942  1.00859525]
[ 0.93336539 -1.95264506  0.99312847]
[ 0.93940596 -1.91603988  0.99230598]
[ 0.95260326 -1.95432899  0.96347066]
[ 0.97404336 -1.95443378  1.07337741]
[ 0.93040456 -1.85128555  0.98612584]
[ 0.94250477 -1.9621798   1.02531186]
[ 0.9412844  -1.94538671  1.06160179]<jupyter_text>Poission Model<jupyter_code>mu = np.exp(nu)
y_poisson = np.random.poisson(lam=mu, size=N)
model = GLM(family=Poisson())
model.fit(X, y_poisson)
model.coef_
model.coef_covariance_matrix_
model.coef_standard_error_
mod = statsmodels.discrete.discrete_model.Poisson(y_poisson, X)
res = mod.fit()
print(res.summary())
s = Simulation(model)
s.sample(X, n_sim=10)
for model in s.parametric_bootstrap(X, n_sim=10):
    print(model.coef_)
for model in s.non_parametric_bootstrap(X, y_poisson, n_sim=10):
    print(model.coef_)<jupyter_output>[ 0.99944037 -1.95435021  0.97540803]
[ 1.03624533 -1.96567739  0.91936164]
[ 0.98336717 -1.96622823  1.00794745]
[ 1.02681072 -1.9644957   0.92060739]
[ 1.01231914 -1.94929026  0.96059324]
[ 1.01857949 -1.96132365  0.95859381]
[ 0.99512153 -1.97303933  0.99006156]
[ 1.01719772 -1.93460717  0.9529537 ]
[ 0.99912808 -1.95763946  0.97810085]
[ 1.00010555 -1.94437559  0.97117927]<jupyter_text>Poisson with Exposures<jupyter_code>mu = np.exp(nu)
expos = np.random.uniform(0, 10, size=N)
y_poisson = np.random.poisson(lam=(mu*expos), size=N)
model = GLM(family=Poisson())
model.fit(X, y_poisson, offset=np.log(expos))
model.coef_
model.coef_standard_error_<jupyter_output><empty_output><jupyter_text>Gamma Regression<jupyter_code>mu = np.exp(nu)
y_gamma = np.random.gamma(shape=2.0, scale=(mu / 2.0), size=N)
gamma_model = GLM(family=Gamma())
gamma_model.fit(X, y_gamma)
gamma_model.coef_
gamma_model.coef_standard_error_
gamma_model.dispersion_
gamma_model = sm.GLM(y_gamma, X, 
                     family=sm.families.Gamma(
                         link=statsmodels.genmod.families.links.log))
res = gamma_model.fit()
print(res.summary())<jupyter_output>Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                      y   No. Observations:                10000
Model:                            GLM   Df Residuals:                     9997
Model Family:                   Gamma   Df Model:                            2
Link Function:                    log   Scale:                  0.499038505471
Method:                          IRLS   Log-Likelihood:                -13910.
Date:                Sun, 17 Sep 2017   Deviance:                       5430.3
Time:                        14:47:52   Pearson chi2:                 4.99e+03
No. Iterations:                     5                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
----------------------------------------------------[...]<jupyter_text>Exponential Regression<jupyter_code>mu = np.exp(nu)
y_exponential = np.random.exponential(scale=mu, size=N)
exponential_model = GLM(family=Gamma())
exponential_model.fit(X, y_exponential)
exponential_model.coef_
exponential_model.coef_standard_error_
exponential_model.dispersion_
exponential_model = sm.GLM(y_exponential, X, 
                     family=sm.families.Gamma(
                         link=statsmodels.genmod.families.links.log))
res = exponential_model.fit()
print(res.summary())<jupyter_output>Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                      y   No. Observations:                10000
Model:                            GLM   Df Residuals:                     9997
Model Family:                   Gamma   Df Model:                            2
Link Function:                    log   Scale:                   0.98383654671
Method:                          IRLS   Log-Likelihood:                -15006.
Date:                Sun, 17 Sep 2017   Deviance:                       11279.
Time:                        14:47:52   Pearson chi2:                 9.84e+03
No. Iterations:                     6                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
----------------------------------------------------[...]<jupyter_text>Linear Model with Correlated Predictors<jupyter_code>N = 1000
X = np.empty(shape=(N, 3))
X[:, 0] = 1.0
X[:, 1] = np.random.uniform(size=N)
X[:, 2] = 0.5*X[:, 1] + np.random.uniform(-0.5, 0.5, size=N)
nu = 1 - 2*X[:, 1] + X[:, 2]
y = nu + np.random.normal(size=N)
model = GLM(family=Gaussian())
model.fit(X, y)
model.coef_
model.coef_covariance_matrix_
model.coef_standard_error_
mod = sm.OLS(y, X)
res = mod.fit()
print(res.summary())<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.228
Model:                            OLS   Adj. R-squared:                  0.226
Method:                 Least Squares   F-statistic:                     146.9
Date:                Sun, 17 Sep 2017   Prob (F-statistic):           1.23e-56
Time:                        14:47:52   Log-Likelihood:                -1398.6
No. Observations:                1000   AIC:                             2803.
Df Residuals:                     997   BIC:                             2818.
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|[...]
{'fraction_non_alphanumeric': 0.18103552807081363, 'fraction_numerical': 0.1252576694555596, 'mean_word_length': 2.320450885668277, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '397879', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3502, 'n_tokens_neox': 2836, 'n_words': 510, 'path': 'examples/glm-examples.ipynb', 'repo_name': 'cscherrer/py-glm', 'size': '8247'}