def fitGLSRobust(geodataframe, variogram_object, num_iterations=20, distance_threshold=1000000): """ Fits a GLS model iterating through the residuals of the previous GLS. After estimating the parameters the method uses the new residuals as input. Recalculates the empirical variogram, refits the theoretical variogram, recalculates de Covariance Matrix. This is done `num_iterations` times. note: the geodataframe needs to have a column called `residuals` """ lrsq = [] lparams = [] lpvals = [] lconf_int = [] for i in range(num_iterations): logger.info("Building Spatial Covariance Matrix") CovMat = buildSpatialStructure(geodataframe, variogram_object.model) logger.info("Calculating GLS estimators") results, resum = calculateGLS(geodataframe, CovMat) n_obs = results.nobs rsq = results.rsquared params = results.params pvals = results.pvalues conf_int = results.conf_int() logger.info("RESULTS::: n_obs: %s, r-squared: %s, {%s,%s,%s}" % (n_obs, rsq, params.to_json(), pvals.to_json(), conf_int.to_json())) lrsq.append(rsq) lparams.append(params) lpvals.append(pvals) lconf_int.append(conf_int) geodataframe.residuals = results.resid envelope = variogram_object.envelope variogram_object = tools.Variogram( geodataframe, 'residuals', using_distance_threshold=distance_threshold, model=variogram_object.model) variogram_object.envelope = envelope #variogram_object.empirical = empirical logger.info("Recalculating variogram") variogram_object.calculateEmpirical() logger.info("Refiting Theoretical Variogram") tt = variogram_object.fitVariogramModel(variogram_object.model) logger.info("Variogram parameters: range %s, sill %s, nugget %s" % (tt.range_a, tt.sill, tt.nugget)) resultspd = pd.DataFrame({ 'rsq': lrsq, 'params': lparams, 'pvals': lpvals, 'conf_int': lconf_int }) return (resum, variogram_object, resultspd, results)
def buildSpatialStructure(geodataframe, theoretical_model): """ wrapper function for calculating spatial covariance matrix """ secvg = tools.Variogram(geodataframe, 'logBiomass', model=theoretical_model) logger.info("Calculating Distance Matrix") CovMat = secvg.calculateCovarianceMatrix() return CovMat
def loadVariogramFromData(plotdata_path, geodataframe): """ Reads and instantiates a Variogram object using data stored in plotdata_path. """ ## Read the empirical variogram logger.info("Reading the empirical Variogram file") thrs_dist = 1000000 ## Change here with appropriate path for file empirical_semivariance_log_log = plotdata_path #### here put the hec calculated logger.info( "Instantiating a Variogram object with the values calculated before") emp_var_log_log = pd.read_csv(empirical_semivariance_log_log) gvg = tools.Variogram(geodataframe, 'logBiomass', using_distance_threshold=thrs_dist) gvg.envelope = emp_var_log_log gvg.empirical = emp_var_log_log.variogram gvg.lags = emp_var_log_log.lags logger.info("Dropping possible Nans") emp_var_log_log = emp_var_log_log.dropna() vdata = gvg.envelope.dropna() logger.info("Instantiating Model...") #matern_model = tools.MaternVariogram(sill=0.34,range_a=100000,nugget=0.33,kappa=0.5) whittle_model = tools.WhittleVariogram(sill=0.34, range_a=100000, nugget=0.0, alpha=3) logger.info("fitting %s Model with the empirical variogram" % whittle_model.name) gvg.model = whittle_model tt = gvg.fitVariogramModel(whittle_model) logger.info("Model fitted") return (gvg, tt)