def covariates(X): """ This 1. checks if the lower-level covariate contains a constant 2. adds a constant to the lower-level covariate if it has no constant """ if constant_check(X): Warn("X array should not contain a constant vector;" " constant will be added automatically") else: X = sphstack(np.ones((X.shape[0], 1)), X) return X
def __init__(self, flows, cost, cost_func='pow', o_vars=None, d_vars=None, origins=None, destinations=None, constant=True, framework='GLM', SF=None, CD=None, Lag=None, Quasi=False): n = User.check_arrays(flows, cost) #User.check_y(flows, n) self.n = n self.f = flows self.c = cost self.ov = o_vars self.dv = d_vars if type(cost_func) == str: if cost_func.lower() == 'pow': self.cf = np.log if (self.c==0).any(): raise ValueError("Zero values detected: cost function 'pow'" "requires the logarithm of the cost variable which" "is undefined at 0") elif cost_func.lower() == 'exp': self.cf = lambda x: x*1.0 elif (type(cost_func) == FunctionType) | (type(cost_func) == np.ufunc): self.cf = cost_func else: raise ValueError("cost_func must be 'exp', 'pow' or a valid " " function that has a scalar as a input and output") y = np.reshape(self.f, (-1,1)) if isinstance(self, Gravity): X = np.empty((self.n, 0)) else: X = sp.csr_matrix((self.n, 1)) if isinstance(self, Production) | isinstance(self, Doubly): o_dummies = spcategorical(origins.flatten()) if constant: o_dummies = o_dummies[:,1:] X = sphstack(X, o_dummies, array_out=False) if isinstance(self, Attraction) | isinstance(self, Doubly): d_dummies = spcategorical(destinations.flatten()) if constant | isinstance(self, Doubly): d_dummies = d_dummies[:,1:] X = sphstack(X, d_dummies, array_out=False) if self.ov is not None: if isinstance(self, Gravity): for each in range(self.ov.shape[1]): if (self.ov[:,each] == 0).any(): raise ValueError("Zero values detected in column %s " "of origin variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) X = np.hstack((X, np.log(np.reshape(self.ov[:,each], (-1,1))))) else: for each in range(self.ov.shape[1]): if (self.ov[:,each] == 0).any(): raise ValueError("Zero values detected in column %s " "of origin variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) ov = sp.csr_matrix(np.log(np.reshape(self.ov[:,each], ((-1,1))))) X = sphstack(X, ov, array_out=False) if self.dv is not None: if isinstance(self, Gravity): for each in range(self.dv.shape[1]): if (self.dv[:,each] == 0).any(): raise ValueError("Zero values detected in column %s " "of destination variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) X = np.hstack((X, np.log(np.reshape(self.dv[:,each], (-1,1))))) else: for each in range(self.dv.shape[1]): if (self.dv[:,each] == 0).any(): raise ValueError("Zero values detected in column %s " "of destination variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) dv = sp.csr_matrix(np.log(np.reshape(self.dv[:,each], ((-1,1))))) X = sphstack(X, dv, array_out=False) if isinstance(self, Gravity): X = np.hstack((X, self.cf(np.reshape(self.c, (-1,1))))) else: c = sp.csr_matrix(self.cf(np.reshape(self.c, (-1,1)))) X = sphstack(X, c, array_out=False) X = X[:,1:]#because empty array instantiated with extra column if not isinstance(self, (Gravity, Production, Attraction, Doubly)): X = self.cf(np.reshape(self.c, (-1,1))) if SF: raise NotImplementedError("Spatial filter model not yet implemented") if CD: raise NotImplementedError("Competing destination model not yet implemented") if Lag: raise NotImplementedError("Spatial Lag autoregressive model not yet implemented") CountModel.__init__(self, y, X, constant=constant) if (framework.lower() == 'glm'): if not Quasi: results = self.fit(framework='glm') else: results = self.fit(framework='glm', Quasi=True) else: raise NotImplementedError('Only GLM is currently implemented') self.params = results.params self.yhat = results.yhat self.cov_params = results.cov_params self.std_err = results.std_err self.pvalues = results.pvalues self.tvalues = results.tvalues self.deviance = results.deviance self.resid_dev = results.resid_dev self.llf = results.llf self.llnull = results.llnull self.AIC = results.AIC self.k = results.k self.D2 = results.D2 self.adj_D2 = results.adj_D2 self.pseudoR2 = results.pseudoR2 self.adj_pseudoR2 = results.adj_pseudoR2 self.results = results self._cache = {}
def __init__(self, flows, cost, cost_func='pow', o_vars=None, d_vars=None, origins=None, destinations=None, constant=True, framework='GLM', SF=None, CD=None, Lag=None, Quasi=False): n = User.check_arrays(flows, cost) #User.check_y(flows, n) self.n = n self.f = flows self.c = cost self.ov = o_vars self.dv = d_vars if type(cost_func) == str: if cost_func.lower() == 'pow': self.cf = np.log if (self.c == 0).any(): raise ValueError( "Zero values detected: cost function 'pow'" "requires the logarithm of the cost variable which" "is undefined at 0") elif cost_func.lower() == 'exp': self.cf = lambda x: x * 1.0 elif (type(cost_func) == FunctionType) | (type(cost_func) == np.ufunc): self.cf = cost_func else: raise ValueError( "cost_func must be 'exp', 'pow' or a valid " " function that has a scalar as a input and output") y = np.reshape(self.f, (-1, 1)) if isinstance(self, Gravity): X = np.empty((self.n, 0)) else: X = sp.csr_matrix((self.n, 1)) if isinstance(self, Production) | isinstance(self, Doubly): o_dummies = spcategorical(origins.flatten()) if constant: o_dummies = o_dummies[:, 1:] X = sphstack(X, o_dummies, array_out=False) if isinstance(self, Attraction) | isinstance(self, Doubly): d_dummies = spcategorical(destinations.flatten()) if constant | isinstance(self, Doubly): d_dummies = d_dummies[:, 1:] X = sphstack(X, d_dummies, array_out=False) if self.ov is not None: if isinstance(self, Gravity): for each in range(self.ov.shape[1]): if (self.ov[:, each] == 0).any(): raise ValueError( "Zero values detected in column %s " "of origin variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) X = np.hstack( (X, np.log(np.reshape(self.ov[:, each], (-1, 1))))) else: for each in range(self.ov.shape[1]): if (self.ov[:, each] == 0).any(): raise ValueError( "Zero values detected in column %s " "of origin variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) ov = sp.csr_matrix( np.log(np.reshape(self.ov[:, each], ((-1, 1))))) X = sphstack(X, ov, array_out=False) if self.dv is not None: if isinstance(self, Gravity): for each in range(self.dv.shape[1]): if (self.dv[:, each] == 0).any(): raise ValueError( "Zero values detected in column %s " "of destination variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) X = np.hstack( (X, np.log(np.reshape(self.dv[:, each], (-1, 1))))) else: for each in range(self.dv.shape[1]): if (self.dv[:, each] == 0).any(): raise ValueError( "Zero values detected in column %s " "of destination variables, which are undefined for " "Poisson log-linear spatial interaction models" % each) dv = sp.csr_matrix( np.log(np.reshape(self.dv[:, each], ((-1, 1))))) X = sphstack(X, dv, array_out=False) if isinstance(self, Gravity): X = np.hstack((X, self.cf(np.reshape(self.c, (-1, 1))))) else: c = sp.csr_matrix(self.cf(np.reshape(self.c, (-1, 1)))) X = sphstack(X, c, array_out=False) X = X[:, 1:] #because empty array instantiated with extra column if not isinstance(self, (Gravity, Production, Attraction, Doubly)): X = self.cf(np.reshape(self.c, (-1, 1))) if SF: raise NotImplementedError( "Spatial filter model not yet implemented") if CD: raise NotImplementedError( "Competing destination model not yet implemented") if Lag: raise NotImplementedError( "Spatial Lag autoregressive model not yet implemented") CountModel.__init__(self, y, X, constant=constant) if (framework.lower() == 'glm'): if not Quasi: results = self.fit(framework='glm') else: results = self.fit(framework='glm', Quasi=True) else: raise NotImplementedError('Only GLM is currently implemented') self.params = results.params self.yhat = results.yhat self.cov_params = results.cov_params self.std_err = results.std_err self.pvalues = results.pvalues self.tvalues = results.tvalues self.deviance = results.deviance self.resid_dev = results.resid_dev self.llf = results.llf self.llnull = results.llnull self.AIC = results.AIC self.k = results.k self.D2 = results.D2 self.adj_D2 = results.adj_D2 self.pseudoR2 = results.pseudoR2 self.adj_pseudoR2 = results.adj_pseudoR2 self.results = results self._cache = {}