def fit(self, framework='GLM', Quasi=False): """ Method that fits a particular count model usign the appropriate estimation technique. Models include Poisson GLM, Negative Binomial GLM, Quasi-Poisson - at the moment Poisson GLM is the only option. TODO: add zero inflated variants and hurdle variants. Parameters ---------- framework : string estimation framework; default is GLM "GLM" | "QUASI" | """ if (framework.lower() == 'glm'): if not Quasi: results = GLM(self.y, self.X, family=Poisson(), constant=self.constant).fit() else: results = GLM(self.y, self.X, family=QuasiPoisson(), constant=self.constant).fit() return CountModelResults(results) else: raise NotImplemented( 'Poisson GLM is the only count model currently implemented')
def test_BS_NN_Offset(self): est_Int = self.BS_NN_OFF.by_col(' est_Intercept') se_Int = self.BS_NN_OFF.by_col(' se_Intercept') t_Int = self.BS_NN_OFF.by_col(' t_Intercept') est_OCC = self.BS_NN_OFF.by_col(' est_OCC_TEC') se_OCC = self.BS_NN_OFF.by_col(' se_OCC_TEC') t_OCC = self.BS_NN_OFF.by_col(' t_OCC_TEC') est_OWN = self.BS_NN_OFF.by_col(' est_OWNH') se_OWN = self.BS_NN_OFF.by_col(' se_OWNH') t_OWN = self.BS_NN_OFF.by_col(' t_OWNH') est_POP = self.BS_NN_OFF.by_col(' est_POP65') se_POP = self.BS_NN_OFF.by_col(' se_POP65') t_POP = self.BS_NN_OFF.by_col(' t_POP65') est_UNEMP = self.BS_NN_OFF.by_col(' est_UNEMP') se_UNEMP = self.BS_NN_OFF.by_col(' se_UNEMP') t_UNEMP = self.BS_NN_OFF.by_col(' t_UNEMP') yhat = self.BS_NN_OFF.by_col(' yhat') pdev = np.array(self.BS_NN_OFF.by_col(' localpdev')).reshape((-1,1)) model = GWR(self.coords, self.y, self.X, bw=100, offset=self.off, family=Poisson(), kernel='bisquare', fixed=False) rslt = model.fit() AICc = get_AICc(rslt) AIC = get_AIC(rslt) BIC = get_BIC(rslt) self.assertAlmostEquals(np.floor(AICc), 367.0) self.assertAlmostEquals(np.floor(AIC), 361.0) self.assertAlmostEquals(np.floor(BIC), 451.0) np.testing.assert_allclose(est_Int, rslt.params[:,0], rtol=1e-02, atol=1e-02) np.testing.assert_allclose(se_Int, rslt.bse[:,0], rtol=1e-02, atol=1e-02) np.testing.assert_allclose(t_Int, rslt.tvalues[:,0], rtol=1e-01, atol=1e-02) np.testing.assert_allclose(est_OCC, rslt.params[:,1], rtol=1e-03, atol=1e-02) np.testing.assert_allclose(se_OCC, rslt.bse[:,1], rtol=1e-02, atol=1e-02) np.testing.assert_allclose(t_OCC, rslt.tvalues[:,1], rtol=1e-01, atol=1e-02) np.testing.assert_allclose(est_OWN, rslt.params[:,2], rtol=1e-04, atol=1e-02) np.testing.assert_allclose(se_OWN, rslt.bse[:,2], rtol=1e-02, atol=1e-02) np.testing.assert_allclose(t_OWN, rslt.tvalues[:,2], rtol=1e-01, atol=1e-02) np.testing.assert_allclose(est_POP, rslt.params[:,3], rtol=1e-03, atol=1e-02) np.testing.assert_allclose(se_POP, rslt.bse[:,3], rtol=1e-02, atol=1e-02) np.testing.assert_allclose(t_POP, rslt.tvalues[:,3], rtol=1e-01, atol=1e-02) np.testing.assert_allclose(est_UNEMP, rslt.params[:,4], rtol=1e-04, atol=1e-02) np.testing.assert_allclose(se_UNEMP, rslt.bse[:,4], rtol=1e-02, atol=1e-02) np.testing.assert_allclose(t_UNEMP, rslt.tvalues[:,4], rtol=1e-01, atol=1e-02) np.testing.assert_allclose(yhat, rslt.mu, rtol=1e-03, atol=1e-02) np.testing.assert_allclose(pdev, rslt.pDev, rtol=1e-04, atol=1e-02)
def test_golden_adapt_AICc_Poisson_wo_offset(self): bw1 = 51.0 bw2 = Sel_BW(self.coords, self.y, self.X, kernel='bisquare', family=Poisson(), fixed=False).search(criterion='AICc') assert_allclose(bw1, bw2)
def test_PoissonGLM(self): model = CountModel(self.y, self.X, family=Poisson()) results = model.fit('GLM') np.testing.assert_allclose(results.params, [3.92159085, 0.01183491, -0.01371397], atol=1.0e-8) self.assertIsInstance(results.family, Poisson) self.assertEqual(results.n, 49) self.assertEqual(results.k, 3) self.assertEqual(results.df_model, 2) self.assertEqual(results.df_resid, 46) np.testing.assert_allclose(results.yhat, [ 51.26831574, 50.15022766, 40.06142973, 34.13799739, 28.76119226, 42.6836241, 55.64593703, 34.08277997, 40.90389582, 37.19727958, 23.47459217, 26.12384057, 29.78303507, 25.96888223, 29.14073823, 26.04369592, 34.18996367, 32.28924005, 27.42284396, 72.69207879, 33.05316347, 36.52276972, 49.2551479, 35.33439632, 24.07252457, 31.67153709, 27.81699478, 25.38021219, 24.31759259, 23.13586161, 48.40724678, 48.57969818, 31.92596006, 43.3679231, 34.32925819, 51.78908089, 34.49778584, 27.56236198, 48.34273194, 57.50829097, 50.66038226, 54.68701352, 35.77103116, 43.21886784, 40.07615759, 49.98658004, 43.13352883, 40.28520774, 46.28910294 ]) np.testing.assert_allclose( results.cov_params, [[1.70280610e-02, -6.18628383e-04, -2.21386966e-04], [-6.18628383e-04, 2.61733917e-05, 6.77496445e-06], [-2.21386966e-04, 6.77496445e-06, 3.75463502e-06]]) np.testing.assert_allclose(results.std_err, [0.13049161, 0.00511599, 0.00193769], atol=1.0e-8) np.testing.assert_allclose( results.pvalues, [2.02901657e-198, 2.07052532e-002, 1.46788805e-012]) np.testing.assert_allclose(results.tvalues, [30.0524361, 2.31331634, -7.07748998]) np.testing.assert_allclose(results.resid, [ 28.73168426, -5.15022766, -14.06142973, -1.13799739, -5.76119226, -13.6836241, 19.35406297, 2.91722003, 12.09610418, 58.80272042, -3.47459217, -6.12384057, 12.21696493, 17.03111777, -11.14073823, -7.04369592, 7.81003633, 27.71075995, 3.57715604, 8.30792121, -13.05316347, -6.52276972, -1.2551479, 17.66560368, -6.07252457, -11.67153709, 6.18300522, -2.38021219, 7.68240741, -1.13586161, -16.40724678, -8.57969818, -7.92596006, -15.3679231, -7.32925819, -15.78908089, 8.50221416, -4.56236198, -8.34273194, 4.49170903, -8.66038226, -10.68701352, -9.77103116, -9.21886784, -12.07615759, 26.01341996, -1.13352883, -13.28520774, -10.28910294 ]) self.assertAlmostEqual(results.deviance, 230.46013824817649) self.assertAlmostEqual(results.llf, -247.42592089969378) self.assertAlmostEqual(results.AIC, 500.85184179938756) self.assertAlmostEqual(results.D2, 0.388656011675) self.assertAlmostEqual(results.adj_D2, 0.36207583826952761)
def test_Dispersion(self): model = CountModel(self.y, self.X, family=Poisson()) results = model.fit('GLM') phi = phi_disp(results) alpha1 = alpha_disp(results) alpha2 = alpha_disp(results, lambda x: x**2) np.testing.assert_allclose(phi, [5.39968689, 2.3230411, 0.01008847], atol=1.0e-8) np.testing.assert_allclose(alpha1, [4.39968689, 2.3230411, 0.01008847], atol=1.0e-8) np.testing.assert_allclose(alpha2, [0.10690133, 2.24709978, 0.01231683], atol=1.0e-8)
def test_GS_NN(self): est_Int = self.GS_NN.by_col(' est_Intercept') se_Int = self.GS_NN.by_col(' se_Intercept') t_Int = self.GS_NN.by_col(' t_Intercept') est_OCC = self.GS_NN.by_col(' est_OCC_TEC') se_OCC = self.GS_NN.by_col(' se_OCC_TEC') t_OCC = self.GS_NN.by_col(' t_OCC_TEC') est_OWN = self.GS_NN.by_col(' est_OWNH') se_OWN = self.GS_NN.by_col(' se_OWNH') t_OWN = self.GS_NN.by_col(' t_OWNH') est_POP = self.GS_NN.by_col(' est_POP65') se_POP = self.GS_NN.by_col(' se_POP65') t_POP = self.GS_NN.by_col(' t_POP65') est_UNEMP = self.GS_NN.by_col(' est_UNEMP') se_UNEMP = self.GS_NN.by_col(' se_UNEMP') t_UNEMP = self.GS_NN.by_col(' t_UNEMP') yhat = self.GS_NN.by_col(' yhat') pdev = np.array(self.GS_NN.by_col(' localpdev')).reshape((-1, 1)) model = GWR(self.coords, self.y, self.X, bw=50, family=Poisson(), kernel='gaussian', fixed=False) rslt = model.fit() AICc = get_AICc(rslt) AIC = get_AIC(rslt) BIC = get_BIC(rslt) self.assertAlmostEquals(np.floor(AICc), 21070.0) self.assertAlmostEquals(np.floor(AIC), 21069.0) self.assertAlmostEquals(np.floor(BIC), 21111.0) np.testing.assert_allclose(est_Int, rslt.params[:, 0], rtol=1e-04) np.testing.assert_allclose(se_Int, rslt.bse[:, 0], rtol=1e-02) np.testing.assert_allclose(t_Int, rslt.tvalues[:, 0], rtol=1e-02) np.testing.assert_allclose(est_OCC, rslt.params[:, 1], rtol=1e-03) np.testing.assert_allclose(se_OCC, rslt.bse[:, 1], rtol=1e-02) np.testing.assert_allclose(t_OCC, rslt.tvalues[:, 1], rtol=1e-02) np.testing.assert_allclose(est_OWN, rslt.params[:, 2], rtol=1e-04) np.testing.assert_allclose(se_OWN, rslt.bse[:, 2], rtol=1e-02) np.testing.assert_allclose(t_OWN, rslt.tvalues[:, 2], rtol=1e-02) np.testing.assert_allclose(est_POP, rslt.params[:, 3], rtol=1e-02) np.testing.assert_allclose(se_POP, rslt.bse[:, 3], rtol=1e-02) np.testing.assert_allclose(t_POP, rslt.tvalues[:, 3], rtol=1e-02) np.testing.assert_allclose(est_UNEMP, rslt.params[:, 4], rtol=1e-02) np.testing.assert_allclose(se_UNEMP, rslt.bse[:, 4], rtol=1e-02) np.testing.assert_allclose(t_UNEMP, rslt.tvalues[:, 4], rtol=1e-02) np.testing.assert_allclose(yhat, rslt.mu, rtol=1e-04) np.testing.assert_allclose(pdev, rslt.pDev, rtol=1e-05)
def __init__(self, y, X, family=Poisson(), constant=True): self.y = self._check_counts(y) self.X = X self.constant = constant