Branch - gsco19
import sys
#append path here to point to your folder
sys.path.append("C:/Users/msachde1/Downloads/Research/Development/mgwr")
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
from mgwr.gwr import GWR
from spglm.family import Gaussian, Binomial, Poisson
from mgwr.gwr import MGWR
from mgwr.sel_bw import Sel_BW
import multiprocessing as mp
pool = mp.Pool()
from scipy import linalg
import numpy.linalg as la
from scipy import sparse as sp
from scipy.sparse import linalg as spla
from spreg.utils import spdot, spmultiply
from scipy import special
import libpysal as ps
import seaborn as sns
import matplotlib.pyplot as plt
from copy import deepcopy
import copy
from collections import namedtuple
import spglm
data_p = ps.io.open(ps.examples.get_path('Tokyomortality.csv'))
coords = list(zip(data_p.by_col('X_CENTROID'),data_p.by_col('Y_CENTROID')))
off = np.array(data_p.by_col('eb2564')).reshape((-1,1))
y = np.array(data_p.by_col('db2564')).reshape((-1,1))
occ = np.array(data_p.by_col('OCC_TEC')).reshape((-1,1))
own = np.array(data_p.by_col('OWNH')).reshape((-1,1))
pop = np.array(data_p.by_col('POP65')).reshape((-1,1))
unemp = np.array(data_p.by_col('UNEMP')).reshape((-1,1))
#X set for multivariate example
X = np.hstack([occ,own,pop,unemp])
#x set for univariate example
x = occ
X_std = (X-X.mean(axis=0))/X.std(axis=0)
x_std = (x-x.mean(axis=0))/x.std(axis=0)
y_std = (y-y.mean(axis=0))/y.std(axis=0)
off_std = (off-off.mean(axis=0))/off.std(axis=0)
#checking distribution of y
sns.distplot(y)
bw=Sel_BW(coords,y,x_std,family=Poisson(),offset=off,constant=False)
bw=bw.search()
gwr_model=GWR(coords,y,x_std,bw,family=Poisson(),offset=off,constant=False).fit()
bw
selector=Sel_BW(coords,y,x_std,multi=True,family=Poisson(),offset=off,constant=False)
selector.search(verbose=True)
mgwr_model=MGWR(coords,y,x_std,selector,family=Poisson(),offset=off,constant=False).fit()
selector.search(verbose=True,init_multi=100)
y.shape[0]
np.sum(((gwr_model.params-mgwr_model.params)==0.0)==True)
gwr_model.aic,mgwr_model.aic
np.sum((gwr_model.predy-mgwr_model.predy==0)==True)
bw=Sel_BW(coords,y,X_std,family=Poisson(),offset=off)
bw=bw.search()
gwr_model=GWR(coords,y,X_std,bw,family=Poisson(),offset=off).fit()
bw
selector=Sel_BW(coords,y,X_std,multi=True,family=Poisson(),offset=off)
selector.search(verbose=True)
selector.search(verbose=True, init_multi=50)
mgwr_model=MGWR(coords,y,X_std,selector,family=Poisson(),offset=off).fit()
mgwr_model.aicc,gwr_model.aicc
mgwr_model.aic,gwr_model.aic
mgwr_model.bic,gwr_model.bic