dispersion_test
import os
os.chdir('/Users/toshan/dev/pysal/pysal/contrib/spint')
from count_model import CountModel
import numpy as np
import pandas as pd
from gravity import Gravity, Production, Attraction, Doubly, BaseGravity
import statsmodels.formula.api as smf
from statsmodels.api import families
os.chdir('/Users/toshan/dev/pysal/pysal/contrib/glm')
from glm import GLM
from family import Poisson, QuasiPoisson
import pysal
import os
os.chdir('/Users/toshan/dev/pysal/pysal/contrib/spint')
from dispersion import alpha_disp, phi_disp
rec = pd.read_csv('/Users/toshan/Documents/RecreationDemand.csv')
rec.head()
y = rec['trips'].values.reshape((-1,1))
X = rec[['quality', 'income', 'costC', 'costS', 'costH']].values
test = CountModel(y, X, constant=False)
glm_results = test.fit(framework='glm')
phi_disp(glm_results)
alpha_disp(glm_results)
alpha_disp(glm_results, lambda x: x**2)
#Prepare some test data - columbus example
db = pysal.open(pysal.examples.get_path('columbus.dbf'),'r')
y = np.array(db.by_col("HOVAL"))
y = np.reshape(y, (49,1))
X = []
#X.append(np.ones(len(y)))
X.append(db.by_col("INC"))
X.append(db.by_col("CRIME"))
X = np.array(X).T
poisson_y = np.round(y).astype(int)
test = CountModel(poisson_y, X)
glm_results = test.fit(framework='glm')
phi_disp(glm_results)
alpha_disp(glm_results)
alpha_disp(glm_results, lambda x:x**2)
model1 = GLM(y, X, constant=False, family=Poisson()).fit()
model2 = GLM(y, X, constant=False, family=QuasiPoisson()).fit()
print model1.scale
print model2.scale