In [1]:
# Load Biospytial modules and etc.
%matplotlib inline
import sys
sys.path.append('/apps')
import django
django.setup()
import pandas as pd
import numpy as np
In [2]:
from external_plugins.spystats import tools
%run ../HEC_runs/fit_fia_logbiomass_logspp_GLS.py
In [3]:
from external_plugins.spystats import tools
hx = np.linspace(0,800000,100)
The object new_data has been reprojected to Alberts and a linear model have been fitted with residuals stored as residuals
In [4]:
new_data.residuals[:10]
Out[4]:
In [5]:
gvg.plot(refresh=False,legend=False,percentage_trunked=20)
plt.title("Semivariogram of residuals $log(Biomass) ~ log(SppR)$")
## HERE we can cast a model (Whittle) and fit it inside the global variogram
whittle_model = tools.WhittleVariogram(sill=0.345,range_a=100000,nugget=0.33,alpha=1.0)
tt = gvg.fitVariogramModel(whittle_model)
plt.plot(hx,gvg.model.f(hx),'--',lw=4,c='black')
print(whittle_model)
In [6]:
## This section is an example for calculating GLS. Using a small section because of computing intensity
In [7]:
minx = -85
maxx = -80
miny = 30
maxy = 35
section = tools._subselectDataFrameByCoordinates(new_data,'LON','LAT',minx,maxx,miny,maxy)
secvg = tools.Variogram(section,'logBiomass',model=whittle_model)
MMdist = secvg.distance_coordinates.flatten()
CovMat = secvg.model.corr_f(MMdist).reshape(len(section),len(section))
plt.imshow(CovMat)
Out[7]:
In [26]:
import statsmodels.regression.linear_model as lm
import statsmodels.api as sm
model1 = lm.GLS.from_formula(formula='logBiomass ~ logSppN',data=section,sigma=CovMat)
results = model1.fit()
resum = results.summary()
In [32]:
k = resum.as_csv()
In [ ]:
In [25]:
## Without spatial structure
Id = np.identity(len(section))
model2 = lm.GLS.from_formula(formula='logBiomass ~ logSppN',data=section,sigma=Id)
results = model2.fit()
smm =results.summary()
In [10]:
## Without spatial structure
import statsmodels.formula.api as smf
model3 = smf.ols(formula='logBiomass ~ logSppN',data=section)
results = model3.fit()
results.summary()
Out[10]:
In [11]:
from scipy.stats import multivariate_normal as mvn
from scipy.spatial import distance_matrix
In [12]:
n = 50
nx = np.linspace(0,100,n)
xx, yy = np.meshgrid(nx,nx)
points = np.vstack([ xx.ravel(), yy.ravel()]).transpose()## Generate dist matrix
Mdist = distance_matrix(points,points)
In [13]:
plt.imshow(Mdist)
Out[13]:
In [15]:
Mdist.shape
covmat = secvg.model.corr_f(Mdist.flatten()).reshape(Mdist.shape)
In [ ]:
In [16]:
plt.imshow(covmat)
Out[16]:
In [18]:
meanx = np.zeros(n*n)
sim1 = mvn.rvs(mean=meanx,cov=covmat)
In [24]:
plt.imshow(sim1.reshape(n,n),interpolation=None)
Out[24]:
In [23]:
%time sim2 = mvn.rvs(mean=meanx,cov=covmat)
plt.imshow(sim2.reshape(n,n))
Out[23]:
In [ ]:
matm = tools.MaternVariogram(sill=0.34,range_a=100000,nugget=0.33,kappa=0.5)
expmm = tools.ExponentialVariogram(sill=0.34,range_a=100000,nugget=0.33)
gausms = tools.GaussianVariogram(sill=0.34,range_a=100000,nugget=0.33)
sphmm = tools.SphericalVariogram(sill=0.34,range_a=100000,nugget=0.33)
wm = tools.WhittleVariogram(sill=0.34,range_a=100000,nugget=0.33,alpha=1)
map(lambda l : l.fit(gvg), [matm,expmm,gausms,sphmm,wm])
print(matm)
print(expmm)
print(gausms)
print(sphmm)
print(wm)
In [ ]: