def test_attribute_writable_resettable(): """ Regression test for mutables and class constructors. """ data = sm.datasets.longley.load() endog, exog = data.endog, data.exog glm_model = sm.GLM(endog, exog) assert_equal(glm_model.family.link.power, 1.0) glm_model.family.link.power = 2. assert_equal(glm_model.family.link.power, 2.0) glm_model2 = sm.GLM(endog, exog) assert_equal(glm_model2.family.link.power, 1.0)
def estimate(self, estimation_pt, sample, bandwidth): """ Performs a GWR estimation at a given point. estimation_pt is a list, tuple or numpy array containing the coordinates of the regression point. sample is a list or array with a sample to compute a prediction at the same time, by applying the regression parameters found by the regression to this value. bandwidth is a the kernel bandwidth. Returns a GWRResult object. """ # Compute the weights of all known samples using the Euclidean # distance between the points, and a Gaussian kernel. if self.kernel: w = self.kernel(self.locations, estimation_pt, bandwidth)[0] else: w = Gaussian.kernel(self.locations, estimation_pt, bandwidth)[0] # Perform the weighted regression using Maximum likelihood if self.family is None: # Gaussian GWR res = sm.WLS(self.targets, self.samples, w) fres = res.fit() else: # Poisson GWR res = sm.GLM(endog=self.targets, exog=self.samples, family=self.family) fres = res.fit(data_weights=w) # Pack everything into a GWRResult object # A GWRResult allows convenient inspection of the results. gwr_result = GWRResult(estimation_pt, sample, fres, self) del(fres) return gwr_result
def __init__(self): # generate artificial data np.random.seed(98765678) nobs = 200 rvs = np.random.randn(nobs, 6) data_exog = rvs data_exog = sm.add_constant(data_exog) xbeta = 1 + 0.1 * rvs.sum(1) data_endog = np.random.poisson(np.exp(xbeta)) #estimate discretemod.Poisson as benchmark self.res_discrete = Poisson(data_endog, data_exog).fit(disp=0) mod_glm = sm.GLM(data_endog, data_exog, family=sm.families.Poisson()) self.res_glm = mod_glm.fit() #estimate generic MLE #self.mod = PoissonGMLE(data_endog, data_exog) #res = self.mod.fit() offset = self.res_discrete.params[0] * data_exog[:, 0] #1d ??? #self.res = PoissonOffsetGMLE(data_endog, data_exog[:,1:], offset=offset).fit(start_params = np.ones(6)/2., method='nm') modo = PoissonOffsetGMLE(data_endog, data_exog[:, 1:], offset=offset) self.res = modo.fit(start_params=0.9 * self.res_discrete.params[1:], method='nm', disp=0)
def global_regression(self): """ Performs a global regression using the targets and samples only, by ignoring the locations. OLS is used for this regression. """ if self.family is None: return sm.OLS(self.targets, self.samples).fit() else: return sm.GLM(endog=self.targets, exog=self.samples, family=self.family).fit()
def __init__(self): # generate artificial data np.random.seed(98765678) nobs = 200 rvs = np.random.randn(nobs, 6) data_exog = rvs data_exog = sm.add_constant(data_exog) xbeta = 0.1 + 0.1 * rvs.sum(1) data_endog = np.random.poisson(np.exp(xbeta)) #estimate discretemod.Poisson as benchmark self.res_discrete = Poisson(data_endog, data_exog).fit(disp=0) mod_glm = sm.GLM(data_endog, data_exog, family=sm.families.Poisson()) self.res_glm = mod_glm.fit() #estimate generic MLE self.mod = PoissonGMLE(data_endog, data_exog) self.res = self.mod.fit(start_params=0.9 * self.res_discrete.params, method='nm', disp=0)
def setup(self): #fit for each test, because results will be changed by test x = self.exog np.random.seed(987689) y = x.sum(1) + np.random.randn(x.shape[0]) self.results = sm.GLM(y, self.exog).fit()
from pandas import DataFrame data = sm.datasets.longley.load() df = DataFrame(data.exog, columns=data.exog_name) y = data.endog # data.exog = sm.add_constant(data.exog) df['intercept'] = 1. olsresult = sm.OLS(y, df).fit() rlmresult = sm.RLM(y, df).fit() # olswrap = RegressionResultsWrapper(olsresult) # rlmwrap = RLMResultsWrapper(rlmresult) data = sm.datasets.wfs.load() # get offset offset = np.log(data.exog[:,-1]) exog = data.exog[:,:-1] # convert dur to dummy exog = sm.tools.categorical(exog, col=0, drop=True) # drop reference category # convert res to dummy exog = sm.tools.categorical(exog, col=0, drop=True) # convert edu to dummy exog = sm.tools.categorical(exog, col=0, drop=True) # drop reference categories and add intercept exog = sm.add_constant(exog[:,[1,2,3,4,5,7,8,10,11,12]]) endog = np.round(data.endog) mod = sm.GLM(endog, exog, family=sm.families.Poisson()).fit() # glmwrap = GLMResultsWrapper(mod)