This question pertains to Python 3 statsmodels and its general linear model class.
Whenever I have an array of values for my endogenous variable such that the values are more than an order of magnitude apart, the GLM does not converge and it throws an exception. Here is a coded example of what I mean.
import pandas as pd
import pyarrow.parquet as pq
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
import math
col = ["a", \
"b", \
"c", \
"d", \
"e", \
"f", \
"g", \
"h"]
df = pd.DataFrame(np.random.randint(low=1, high=100, size=(20, 8)), columns=col)
df["a"] = [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]
df2 = pd.DataFrame(np.random.randint(low=1, high=100, size=(20, 8)), columns=col)
df2["a"] = np.random.randint(low=10000, high=99999, size=(20, 1))
df3 = pd.DataFrame(np.random.randint(low=1, high=100, size=(20, 8)), columns=col)
df3["a"] = [0.01, \
0.01, \
0.01, \
0.01, \
0.01, \
0.01, \
np.random.randint(low=10000, high=99999), \
0.01, \
0.01, \
0.01, \
0.01, \
0.01, \
np.random.randint(low=10000, high=99999), \
0.01, \
0.01, \
0.01, \
0.01, \
0.01, \
0.01, \
0.01]
try:
actual = df[["a"]]
fml1 = "a ~ log(b) + c + d + e + f + g"
data1 = df[["b", "c", "d", "e", "f", "g"]]
model = sm.GLM(actual, data1, formula=fml1, family=sm.families.Tweedie(link_power=1.1)).fit()
model_pred = model.predict()
print("SUCCESS")
except:
print("FAILURE")
try:
actual = df2[["a"]]
fml1 = "a ~ log(b) + c + d + e + f + g"
data1 = df2[["b", "c", "d", "e", "f", "g"]]
model = sm.GLM(actual, data1, formula=fml1, family=sm.families.Tweedie(link_power=1.1)).fit()
model_pred = model.predict()
print("SUCCESS")
except:
print("FAILURE")
try:
actual = df3[["a"]]
fml1 = "a ~ log(b) + c + d + e + f + g"
data1 = df3[["b", "c", "d", "e", "f", "g"]]
model = sm.GLM(actual, data1, formula=fml1, family=sm.families.Tweedie(link_power=1.1)).fit()
model_pred = model.predict()
print("SUCCESS")
except:
print("FAILURE")
If you run this code, you should get an exception only on the last set of data. Why is this? How do I get the GLM to converge? Are there alternatives?


