Capítulo 3 Geoestadística univariada con geoR

3.1 Parte descriptiva

3.1.1 Librerías

Lista de librerías con link a la documentación.

rm(list=ls())
library(fields)
library(geoR)
library(akima)

3.1.2 Lectura de datos

aquifer <- read.table("data_7_GeoestadisticaGEOSTAT/aquifer.txt", head = TRUE, dec = ",")

Encabezado de datos aquifer.txt

head(aquifer)
##        Este     Norte Profundidad
## 1  42.78275 127.62282        1464
## 2 -27.39691  90.78732        2553
## 3  -1.16289  84.89600        2158
## 4 -18.61823  76.45199        2455
## 5  96.46549  64.58058        1756
## 6 108.56243  82.92325        1702

Summary de los datos aquifer.txt

summary(aquifer)
##       Este             Norte          Profundidad  
##  Min.   :-145.24   Min.   :  9.414   Min.   :1024  
##  1st Qu.: -21.30   1st Qu.: 33.682   1st Qu.:1548  
##  Median :  11.66   Median : 59.158   Median :1797  
##  Mean   :  16.89   Mean   : 79.361   Mean   :2002  
##  3rd Qu.:  70.90   3rd Qu.:131.825   3rd Qu.:2540  
##  Max.   : 112.80   Max.   :184.766   Max.   :3571

3.2 GEO_Data

3.2.1 Convertir aquifer a un objeto geodata (geoR obj)

aquiferg <- as.geodata(aquifer)
summary(aquiferg)
## Number of data points: 85 
## 
## Coordinates summary
##          Este     Norte
## min -145.2365   9.41441
## max  112.8045 184.76636
## 
## Distance summary
##         min         max 
##   0.2211656 271.0615463 
## 
## Data summary
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 1024.000 1548.000 1797.000 2002.282 2540.000 3571.000

3.2.2 Gráfico de objeto geodata

Gráfico del objeto geodata

plot(aquiferg, qt.col = c("purple",
                         "pink",
                         "green",
                         "yellow"))

Gráfico con el parametro 3d

plot(aquiferg, scatter3d = T)

Gráfico removiendo la tendencia (trend )

plot(aquiferg, trend = "1st")

3.2.3 Gráficos descriptivos interpolación

par(mfrow = c(2, 2),
    mar = c(3, 3, 1, 1),
    mgp = c(2, 1, 0))
# Esta función agrupa los siguientes gráficos en
# una matrix 2x2

grillas <- interp(aquifer$Este,
                  aquifer$Norte,
                  aquifer$Profundidad)

persp(grillas$x,
      grillas$y,
      grillas$z,
      xlab = "Este",
      ylab = "Norte",
      zlab = "Nivel freatico",
      phi = 30,
      theta = 20,
      col = "lightblue",
      expand = .5,
      ticktype = "detailed")

drape.plot(grillas$x,
           grillas$y,
           grillas$z,
           xlab = "Este",
           ylab = "Norte",
           zlab = "z",
           theta = 45,
           col = topo.colors(64),
           expand = .5,
           ticktype = "detailed")


drape.plot(grillas$x,
           grillas$y,
           grillas$z,
           xlab = "Este",
           ylab = "Norte",
           zlab = "z",
           theta = -10,
           col = topo.colors(64),
           expand = .5,
           ticktype = "detailed")


drape.plot(grillas$x,
           grillas$y,
           grillas$z,
           xlab = "Este",
           ylab = "Norte",
           zlab = "z",
           theta = 60,
           col = topo.colors(64),
           expand = .5,
           ticktype = "detailed")

3.2.4 Gráficos de contorno

par(mfrow = c(2, 1),
    mar = c(1,1,1,1))

contour(grillas, nlevels = 10, main = "Contorno")
image(grillas$z, main =  "Grilla")

filled.contour(grillas, levels = seq(1000,
                                     5000,
                                     len = 10),
               col = heat.colors(10),
                main = "grilla niveles")

3.2.5 Funciones y gráficas a partir de la función outer

h <- seq(0, 1, len = 50)
u <- seq(0, 1, len = 50)

ejemplo1CH  <- function(h, u, sigma, a, b, c, d, delta) {
    (sigma^2/((a^2*u^2+c)^(d/2)))*exp(-(b^2*h^2)/(a^2*u^2+c))*exp(-delta*u^2)
    }
h <- seq(0, 1, len = 20)
u <- seq(1, 10, len = 20)
f <- outer(h, u, ejemplo1CH, sigma=3, a=1, b=3, c=1, d=2, delta=0)

par(mfrow = c(2, 2),
    mar = c(3, 3, 1, 1),
    mgp = c(2, 1, 0))

drape.plot(h,
           u,
           f,
           main = "Cressie-Huang; 1 (25,1,0.6)",
           xlab = "h",
           ylab = "u",
           zlab = "Covarianza",
           ltheta = 75,
           col = terrain.colors(64))

drape.plot(h,
           u,
           f,
           main = "Cressie-Huang; 1 (25,1,0.6)",
           xlab = "h",
           ylab = "u",
           zlab = "Covarianza",
           theta = -150,
           col = terrain.colors(64))
persp(h,
      u,
      f,
      main = "Cressie-Huang; 1 (25,1,0.6)",
      xlab = "h",
      ylab = "u",
      zlab = "Covarianza",
      ltheta = 75)

contour(h,
        u,
        f,
        col = topo.colors(10),
        xlim = c(0,0.6))

3.3 Modelando la media con regresión polinomial

3.3.1 Primer modelo

reg1 <- lm(Profundidad ~ Este + Norte, data = aquifer)
residuales1  <-  residuals(reg1)
summary(reg1)
## 
## Call:
## lm(formula = Profundidad ~ Este + Norte, data = aquifer)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -366.96 -161.53  -30.71  148.15  651.20 
## 
## Coefficients:
##              Estimate Std. Error t value            Pr(>|t|)    
## (Intercept) 2591.4302    38.9599   66.52 <0.0000000000000002 ***
## Este          -6.7514     0.3438  -19.64 <0.0000000000000002 ***
## Norte         -5.9872     0.4066  -14.73 <0.0000000000000002 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 203.3 on 82 degrees of freedom
## Multiple R-squared:  0.8921, Adjusted R-squared:  0.8894 
## F-statistic: 338.9 on 2 and 82 DF,  p-value: < 0.00000000000000022
anova(reg1)
## Analysis of Variance Table
## 
## Response: Profundidad
##           Df   Sum Sq  Mean Sq F value                Pr(>F)    
## Este       1 19045642 19045642  460.95 < 0.00000000000000022 ***
## Norte      1  8960172  8960172  216.86 < 0.00000000000000022 ***
## Residuals 82  3388069    41318                                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3.3.2 Segundo modelo

reg2 <- lm(Profundidad ~ Este + Norte +
           I(Este^2) + I(Norte^2) +
           I(Este * Norte),
           data = aquifer)
residuales2  <-  residuals(reg2)
summary(reg2)
## 
## Call:
## lm(formula = Profundidad ~ Este + Norte + I(Este^2) + I(Norte^2) + 
##     I(Este * Norte), data = aquifer)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -407.43 -138.76   -5.74  128.84  648.16 
## 
## Coefficients:
##                    Estimate  Std. Error t value             Pr(>|t|)    
## (Intercept)     2481.430109   68.127017  36.424 < 0.0000000000000002 ***
## Este              -8.373708    0.552451 -15.157 < 0.0000000000000002 ***
## Norte             -2.043419    1.763832  -1.159             0.250146    
## I(Este^2)          0.001417    0.004987   0.284             0.777096    
## I(Norte^2)        -0.024644    0.009298  -2.650             0.009708 ** 
## I(Este * Norte)    0.026801    0.007413   3.616             0.000526 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 185.9 on 79 degrees of freedom
## Multiple R-squared:  0.9131, Adjusted R-squared:  0.9076 
## F-statistic:   166 on 5 and 79 DF,  p-value: < 0.00000000000000022
anova(reg2)
## Analysis of Variance Table
## 
## Response: Profundidad
##                 Df   Sum Sq  Mean Sq  F value                Pr(>F)    
## Este             1 19045642 19045642 551.3469 < 0.00000000000000022 ***
## Norte            1  8960172  8960172 259.3855 < 0.00000000000000022 ***
## I(Este^2)        1    55368    55368   1.6028             0.2092235    
## I(Norte^2)       1   152170   152170   4.4051             0.0390253 *  
## I(Este * Norte)  1   451567   451567  13.0723             0.0005259 ***
## Residuals       79  2728964    34544                                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3.3.3 Tercer modelo

reg3 <- lm(Profundidad ~ Este * Norte,
           data = aquifer)
residuales3  <-  residuals(reg3)
summary(reg3)
## 
## Call:
## lm(formula = Profundidad ~ Este * Norte, data = aquifer)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -406.30 -138.88  -13.04  129.36  722.48 
## 
## Coefficients:
##                Estimate  Std. Error t value             Pr(>|t|)    
## (Intercept) 2627.069474   38.325720  68.546 < 0.0000000000000002 ***
## Este          -8.287218    0.565845 -14.646 < 0.0000000000000002 ***
## Norte         -6.648559    0.432667 -15.366 < 0.0000000000000002 ***
## Este:Norte     0.024524    0.007401   3.314              0.00138 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 191.9 on 81 degrees of freedom
## Multiple R-squared:  0.905,  Adjusted R-squared:  0.9014 
## F-statistic: 257.1 on 3 and 81 DF,  p-value: < 0.00000000000000022
anova(reg3)
## Analysis of Variance Table
## 
## Response: Profundidad
##            Df   Sum Sq  Mean Sq F value                Pr(>F)    
## Este        1 19045642 19045642  517.06 < 0.00000000000000022 ***
## Norte       1  8960172  8960172  243.25 < 0.00000000000000022 ***
## Este:Norte  1   404448   404448   10.98              0.001379 ** 
## Residuals  81  2983621    36835                                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3.4 Estimación del semivariograma empírico

vari2 <- variog(aquiferg, trend = "1st")
## variog: computing omnidirectional variogram
vari2Cloud <- variog(aquiferg, op = "cloud", trend = "1st")
## variog: computing omnidirectional variogram
vari2BinCloud <- variog(aquiferg,
                       max.dist = 200,
                       op = "cloud",
                       bin.cloud = TRUE)
## variog: computing omnidirectional variogram
vari2Sm <- variog(aquiferg,
                  trend = "1st",
                  op = "sm",
                  band=11)
## variog: computing omnidirectional variogram
par(mfrow = c(2, 2), mar = c(3, 3, 1, 1), mgp = c(2, 1, 0))
     plot(vari2$u, vari2$v, main = "binned variogram") # jocastroc: vari2 solo no da el plot se dejo vari2$u,vari2$v
     plot(vari2Cloud$u, vari2Cloud$v, main = "variogram cloud")
     plot(vari2BinCloud$u, vari2BinCloud$v, main = "clouds for binned variogram")
     plot(vari2Sm$u, vari2Sm$v, main = "smoothed variogram")

3.4.1 Explorando estimación clásica, removiendo tendencia

vari1 <- variog(aquiferg)
## variog: computing omnidirectional variogram
vari2 <- variog(aquiferg, trend = "1st")
## variog: computing omnidirectional variogram
vari3 <- variog(aquiferg, trend = "2nd")
## variog: computing omnidirectional variogram
plot(vari1$u,vari1$v, main = "Sin remover tendencia")

plot(vari2$u,vari2$v, main  = "Trend 1 ")

plot(vari3$u,vari3$v, main  = "Trend 2 ")

3.4.2 Explorando estimación resistente a datos atípicos y removiendo tendencia

vari1 <- variog(aquiferg, estimator.type = "modulus")
## variog: computing omnidirectional variogram
vari2 <- variog(aquiferg, trend = "1st", estimator.type = "modulus")
## variog: computing omnidirectional variogram
vari3 <- variog(aquiferg, trend = "2nd", estimator.type = "modulus")
## variog: computing omnidirectional variogram
plot(vari1$u,vari1$v, main = "Sin remover tendencia")

plot(vari2$u,vari2$v, main  = "Trend 1 ")

plot(vari3$u,vari3$v, main  = "Trend 2 ")

3.4.3 Explorando anisotropía

vari_0 <- variog(aquiferg,
                 trend = "1st",
                 max.dist = 200,
                 dir = 0)
## variog: computing variogram for direction = 0 degrees (0 radians)
##         tolerance angle = 22.5 degrees (0.393 radians)
vari_45 <- variog(aquiferg,
                  trend = "1st",
                  max.dist = 200,
                  dir = pi / 4)
## variog: computing variogram for direction = 45 degrees (0.785 radians)
##         tolerance angle = 22.5 degrees (0.393 radians)
vari_90 <- variog(aquiferg,
                  trend = "1st",
                  max.dist = 200,
                  dir = pi / 2)
## variog: computing variogram for direction = 90 degrees (1.571 radians)
##         tolerance angle = 22.5 degrees (0.393 radians)
vari_135 <- variog(aquiferg,
                   trend = "1st",
                   max.dist = 200,
                   dir = 3 * pi / 4)
## variog: computing variogram for direction = 135 degrees (2.356 radians)
##         tolerance angle = 22.5 degrees (0.393 radians)
par(mfrow = c(2, 2),
    mar = c(3, 3, 1, 1),
    mgp = c(2, 1, 0))

plot(vari_0$u,vari_0$v, main = "vari 0")
plot(vari_45$u,vari_45$v, main = "vari 45")
plot(vari_90$u,vari_90$v, main = "vari 90")
plot(vari_135$u,vari_135$v, main = "vari 195")

3.5 Estimación teórica del semivariograma

var1 <- variog(aquiferg,trend="1st",max.dist=200)
## variog: computing omnidirectional variogram
#ini1 <- eyefit(var1)
#cov.model  sigmasq phi   tausq kappa kappa2   practicalRange
#1      wave 30805.52  13 8984.94  <NA>   <NA> 38.8889336320589
ini1 <- c(30805.52, 13)
fitvar1 <- variofit(var1,
                    cov.model = "wave",
                    ini1,
                    fix.nugget = TRUE,
                    nugget = 8984.94,
                    wei = "equal")
## variofit: covariance model used is wave 
## variofit: weights used: equal 
## variofit: minimisation function used: optim
fitvar2 <- variofit(var1,
                    cov.model = "wave",
                    ini1,
                    fix.nugget = TRUE,
                    nugget = 8984.94,
                    wei = "npairs")
## variofit: covariance model used is wave 
## variofit: weights used: npairs 
## variofit: minimisation function used: optim
fitvar3 <- variofit(var1,
                    ini1,
                    fix.nugget = TRUE,
                    nugget = 8984.94,
                    wei = "cressie")
## variofit: covariance model used is matern 
## variofit: weights used: cressie 
## variofit: minimisation function used: optim
fitvar4 <- likfit(aquiferg,
                  coords = aquiferg$coords,
                  data = aquiferg$data,
                  trend = "1st",
                  ini.cov.pars = ini1,
                  fix.nugget = T,
                  nugget = 8984.94,
                  cov.model = "wave",
                  lik.method = "ML")
## kappa not used for the wave correlation function
## ---------------------------------------------------------------
## likfit: likelihood maximisation using the function optim.
## likfit: Use control() to pass additional
##          arguments for the maximisation function.
##         For further details see documentation for optim.
## likfit: It is highly advisable to run this function several
##         times with different initial values for the parameters.
## likfit: WARNING: This step can be time demanding!
## ---------------------------------------------------------------
## likfit: end of numerical maximisation.
fitvar5 <- likfit(aquiferg,
                  coords = aquiferg$coords,
                  data = aquiferg$data,
                  trend = "1st",
                  ini.cov.pars = ini1,
                  fix.nugget = T,
                  nugget = 8984.94,
                  cov.model = "wave",
                  lik.method = "REML")
## kappa not used for the wave correlation function
## ---------------------------------------------------------------
## likfit: likelihood maximisation using the function optim.
## likfit: Use control() to pass additional
##          arguments for the maximisation function.
##         For further details see documentation for optim.
## likfit: It is highly advisable to run this function several
##         times with different initial values for the parameters.
## likfit: WARNING: This step can be time demanding!
## ---------------------------------------------------------------
## likfit: end of numerical maximisation.
plot(var1$u,var1$v,
     xlab = "h",
     ylab = "semivarianza",
     cex.lab = 1.3,
     cex.axis = 1.2,
     main = "Estimación teórica del modelo de semivariograma",
     col.main = 4, cex.main =1.3)
lines(fitvar1, col = 1)
lines(fitvar2, col = 2)
lines(fitvar3, col = 3)
lines(fitvar4, col = 4)
lines(fitvar5, col = 5)
legend(130, 18000,
       c("MCO", "MCPnpairs", "MCPcressie", "ML", "REML"),
       lwd = 2,
       lty = 2:7,
       col = 2:7,
       box.col = 9,
       text.col = 2:7)

3.6 Resultados

summary(fitvar1)
## $pmethod
## [1] "OLS (ordinary least squares)"
## 
## $cov.model
## [1] "wave"
## 
## $spatial.component
##     sigmasq         phi 
## 31203.33666    11.93873 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
##   tausq 
## 8984.94 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 35.71419
## 
## $sum.of.squares
##     value 
## 159338722 
## 
## $estimated.pars
##     sigmasq         phi 
## 31203.33666    11.93873 
## 
## $weights
## [1] "equal"
## 
## $call
## variofit(vario = var1, ini.cov.pars = ini1, cov.model = "wave", 
##     fix.nugget = TRUE, nugget = 8984.94, weights = "equal")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(fitvar2)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "wave"
## 
## $spatial.component
##     sigmasq         phi 
## 31311.81332    12.05997 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
##   tausq 
## 8984.94 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 36.07688
## 
## $sum.of.squares
##       value 
## 32922890311 
## 
## $estimated.pars
##     sigmasq         phi 
## 31311.81332    12.05997 
## 
## $weights
## [1] "npairs"
## 
## $call
## variofit(vario = var1, ini.cov.pars = ini1, cov.model = "wave", 
##     fix.nugget = TRUE, nugget = 8984.94, weights = "npairs")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(fitvar3)
## $pmethod
## [1] "WLS (weighted least squares)"
## 
## $cov.model
## [1] "matern"
## 
## $spatial.component
##     sigmasq         phi 
## 32541.18204    22.69704 
## 
## $spatial.component.extra
## kappa 
##   0.5 
## 
## $nugget.component
##   tausq 
## 8984.94 
## 
## $fix.nugget
## [1] TRUE
## 
## $fix.kappa
## [1] TRUE
## 
## $practicalRange
## [1] 67.99426
## 
## $sum.of.squares
##    value 
## 26.97852 
## 
## $estimated.pars
##     sigmasq         phi 
## 32541.18204    22.69704 
## 
## $weights
## [1] "cressie"
## 
## $call
## variofit(vario = var1, ini.cov.pars = ini1, fix.nugget = TRUE, 
##     nugget = 8984.94, weights = "cressie")
## 
## attr(,"class")
## [1] "summary.variomodel"
summary(fitvar4)
## Summary of the parameter estimation
## -----------------------------------
## Estimation method: maximum likelihood 
## 
## Parameters of the mean component (trend):
##     beta0     beta1     beta2 
## 2723.1654   -7.1140   -6.9505 
## 
## Parameters of the spatial component:
##    correlation function: wave
##       (estimated) variance parameter sigmasq (partial sill) =  30806
##       (estimated) cor. fct. parameter phi (range parameter)  =  12.11
##    anisotropy parameters:
##       (fixed) anisotropy angle = 0  ( 0 degrees )
##       (fixed) anisotropy ratio = 1
## 
## Parameter of the error component:
##       (fixed) nugget = 8984.94
## 
## Transformation parameter:
##       (fixed) Box-Cox parameter = 1 (no transformation)
## 
## Practical Range with cor=0.05 for asymptotic range: 36.2227
## 
## Maximised Likelihood:
##    log.L n.params      AIC      BIC 
## "-572.8"      "5"   "1156"   "1168" 
## 
## non spatial model:
##    log.L n.params      AIC      BIC 
## "-570.8"      "4"   "1150"   "1159" 
## 
## Call:
## likfit(geodata = aquiferg, coords = aquiferg$coords, data = aquiferg$data, 
##     trend = "1st", ini.cov.pars = ini1, fix.nugget = T, nugget = 8984.94, 
##     cov.model = "wave", lik.method = "ML")
summary(fitvar5)
## Summary of the parameter estimation
## -----------------------------------
## Estimation method: restricted maximum likelihood 
## 
## Parameters of the mean component (trend):
##     beta0     beta1     beta2 
## 2723.0308   -7.1143   -6.9479 
## 
## Parameters of the spatial component:
##    correlation function: wave
##       (estimated) variance parameter sigmasq (partial sill) =  30806
##       (estimated) cor. fct. parameter phi (range parameter)  =  12.16
##    anisotropy parameters:
##       (fixed) anisotropy angle = 0  ( 0 degrees )
##       (fixed) anisotropy ratio = 1
## 
## Parameter of the error component:
##       (fixed) nugget = 8984.94
## 
## Transformation parameter:
##       (fixed) Box-Cox parameter = 1 (no transformation)
## 
## Practical Range with cor=0.05 for asymptotic range: 36.38468
## 
## Maximised Likelihood:
##    log.L n.params      AIC      BIC 
## "-553.2"      "5"   "1116"   "1129" 
## 
## non spatial model:
##    log.L n.params      AIC      BIC 
## "-552.1"      "4"   "1112"   "1122" 
## 
## Call:
## likfit(geodata = aquiferg, coords = aquiferg$coords, data = aquiferg$data, 
##     trend = "1st", ini.cov.pars = ini1, fix.nugget = T, nugget = 8984.94, 
##     cov.model = "wave", lik.method = "REML")

3.7 Pulimiento de medianas

Esta es una alternativa al modelamiento de la media cuando los modelos de regresión polinómicos usuales no logran el objetivo de eliminar la tendencia ya sea porque el tipo de tendencia corresponde mas a unas ventanas móviles o porque hay presentes datos atípicos.

3.7.1 Cargar librerias

Lista de librerías con link a la documentación.

rm(list=ls())
library(gstat)
library(sp)
library(mvtnorm)

3.7.2 Grilla de las ubicaciones espaciales.

n_x <- 4
n_y <- 6
x <- seq(0, 1, len = n_x)
y <- seq(0, 1, len = n_y)
coordenadas <- as.data.frame(expand.grid(x, y))
names(coordenadas) <- c("X", "Y")

Encabezado coordenadas

X Y
0.0000000 0.0
0.3333333 0.0
0.6666667 0.0
1.0000000 0.0
0.0000000 0.2
0.3333333 0.2

3.7.3 Definición de objeto VGM

Esto define un objeto vgm que es el tipo de objeto que usa el paquete gstat para los modelos teóricos de variograma. Con este objeto se pueden definir modelos anidados.

vario <- vgm(10, # Punto de silla
             "Exp", # Modelo, ver documentación
             0.5)  # Rango
print(vario)
##   model psill range
## 1   Exp    10   0.5

3.7.4 Matriz de varianza dadas coordenadas.

coordinates(coordenadas) <- ~X + Y
class(coordenadas) # Cambio de objedto dataframe a sp
## [1] "SpatialPoints"
## attr(,"package")
## [1] "sp"
cov_mat <- vgmArea(coordenadas, # Matriz de ubiaciones SP
        vgm = vario) # VGM object

print(dim(cov_mat))
## [1] 24 24

3.7.5 Simulación.

Simulación dada la media y la matriz de varianza

mu  <- rep(0, n_x * n_y) # Media del proceso
simu <- rmvnorm(1,
                mean = mu,
                sigma = cov_mat)
print(simu[1:5])
## [1]  2.1891916 -3.8833516  1.7460467  0.2713589 -5.1089803

3.7.6 Pulimiento de medianas

Unir las coordenadas con la columna de simulación

data <- as.data.frame(cbind(coordenadas@coords,
                            Simula = t(simu)))
names(data) <- c("X", "Y", "Var")
print(head(data))
##           X   Y        Var
## 1 0.0000000 0.0  2.1891916
## 2 0.3333333 0.0 -3.8833516
## 3 0.6666667 0.0  1.7460467
## 4 1.0000000 0.0  0.2713589
## 5 0.0000000 0.2 -5.1089803
## 6 0.3333333 0.2 -0.4416415

Reshape para matriz, esto transforma la tabla de datos en matriz

tabla <- reshape2::dcast(data,
                         X ~ Y,
                         value.var = "Var")
rownames(tabla) <- tabla[, 1]
tabla <- tabla[, c(-1)]
print(tabla)
##                            0         0.2        0.4       0.6       0.8          1
## 0                  2.1891916 -5.10898025 -2.6707419 -3.383100 -4.740408 -3.5000650
## 0.333333333333333 -3.8833516 -0.44164152  1.9610943 -3.174812 -3.538608 -1.9888859
## 0.666666666666667  1.7460467 -3.90262775 -0.7403839 -3.021353 -2.842467  2.1003900
## 1                  0.2713589 -0.09471264  0.9514740 -3.338079 -1.579355 -0.8825137

Pulimiento de medianas de la tabla

med <- medpolish(tabla)
## 1: 29.04747
## 2: 27.58165
## Final: 27.47662
geo_data <- reshape2::melt(med$residuals)
print(med)
## 
## Median Polish Results (Dataset: "tabla")
## 
## Overall: -2.097145
## 
## Row Effects:
##                 0 0.333333333333333 0.666666666666667                 1 
##        -1.6907089        -0.1343331         0.1343331         1.1985933 
## 
## Column Effects:
##          0        0.2        0.4        0.6        0.8          1 
##  2.4393849 -0.2586434  1.5362271 -1.0009371 -0.9161044  0.2651908 
## 
## Residuals:
##                         0     0.2      0.4       0.6       0.8         1
## 0                  3.5377 -1.0625 -0.41911  1.405691 -0.036449  0.022598
## 0.333333333333333 -4.0913  2.0485  2.65635  0.057603 -0.391025 -0.022598
## 0.666666666666667  1.2695 -1.6812 -0.31380 -0.057603  0.036449  3.798011
## 1                 -1.2695  1.0625  0.31380 -1.438590  0.235302 -0.249153

Reshape de los datos, con efecto de la fila y la columna

tabla_residuales <- as.data.frame(med$residuals)
names(tabla_residuales) <- med$col
rownames(tabla_residuales) <- med$row
geo_data <- reshape2::melt(as.matrix(tabla_residuales))

geo_data <- cbind(data,
                  geo_data,
                  med$overall)
names(geo_data) <- c("X",
                     "Y",
                     "Var",
                     "Efecto fila",
                     "Efecto columa",
                     "Residual",
                     "Efecto Global")
print(geo_data)
##            X   Y         Var Efecto fila Efecto columa    Residual Efecto Global
## 1  0.0000000 0.0  2.18919156  -1.6907089     2.4393849  3.53766088     -2.097145
## 2  0.3333333 0.0 -3.88335158  -0.1343331     2.4393849 -4.09125810     -2.097145
## 3  0.6666667 0.0  1.74604671   0.1343331     2.4393849  1.26947407     -2.097145
## 4  1.0000000 0.0  0.27135885   1.1985933     2.4393849 -1.26947407     -2.097145
## 5  0.0000000 0.2 -5.10898025  -1.6907089    -0.2586434 -1.06248268     -2.097145
## 6  0.3333333 0.2 -0.44164152  -0.1343331    -0.2586434  2.04848020     -2.097145
## 7  0.6666667 0.2 -3.90262775   0.1343331    -0.2586434 -1.68117214     -2.097145
## 8  1.0000000 0.2 -0.09471264   1.1985933    -0.2586434  1.06248268     -2.097145
## 9  0.0000000 0.4 -2.67074194  -1.6907089     1.5362271 -0.41911488     -2.097145
## 10 0.3333333 0.4  1.96109434  -0.1343331     1.5362271  2.65634555     -2.097145
## 11 0.6666667 0.4 -0.74038392   0.1343331     1.5362271 -0.31379882     -2.097145
## 12 1.0000000 0.4  0.95147400   1.1985933     1.5362271  0.31379882     -2.097145
## 13 0.0000000 0.6 -3.38310030  -1.6907089    -1.0009371  1.40569100     -2.097145
## 14 0.3333333 0.6 -3.17481203  -0.1343331    -1.0009371  0.05760342     -2.097145
## 15 0.6666667 0.6 -3.02135276   0.1343331    -1.0009371 -0.05760342     -2.097145
## 16 1.0000000 0.6 -3.33807864   1.1985933    -1.0009371 -1.43858959     -2.097145
## 17 0.0000000 0.8 -4.74040787  -1.6907089    -0.9161044 -0.03644924     -2.097145
## 18 0.3333333 0.8 -3.53860798  -0.1343331    -0.9161044 -0.39102519     -2.097145
## 19 0.6666667 0.8 -2.84246743   0.1343331    -0.9161044  0.03644924     -2.097145
## 20 1.0000000 0.8 -1.57935464   1.1985933    -0.9161044  0.23530174     -2.097145
## 21 0.0000000 1.0 -3.50006497  -1.6907089     0.2651908  0.02259841     -2.097145
## 22 0.3333333 1.0 -1.98888595  -0.1343331     0.2651908 -0.02259841     -2.097145
## 23 0.6666667 1.0  2.10039000   0.1343331     0.2651908  3.79801142     -2.097145
## 24 1.0000000 1.0 -0.88251371   1.1985933     0.2651908 -0.24915257     -2.097145

Validación de la descomposición

valida <- cbind(geo_data$Var,
                geo_data[["Efecto fila"]] +
                geo_data[["Efecto columa"]] +
                geo_data[["Residual"]] +
                geo_data[["Efecto Global"]])
valida <- as.data.frame(valida)
names(valida) <- c("datos", "suma")
print(valida)
##          datos        suma
## 1   2.18919156  2.18919156
## 2  -3.88335158 -3.88335158
## 3   1.74604671  1.74604671
## 4   0.27135885  0.27135885
## 5  -5.10898025 -5.10898025
## 6  -0.44164152 -0.44164152
## 7  -3.90262775 -3.90262775
## 8  -0.09471264 -0.09471264
## 9  -2.67074194 -2.67074194
## 10  1.96109434  1.96109434
## 11 -0.74038392 -0.74038392
## 12  0.95147400  0.95147400
## 13 -3.38310030 -3.38310030
## 14 -3.17481203 -3.17481203
## 15 -3.02135276 -3.02135276
## 16 -3.33807864 -3.33807864
## 17 -4.74040787 -4.74040787
## 18 -3.53860798 -3.53860798
## 19 -2.84246743 -2.84246743
## 20 -1.57935464 -1.57935464
## 21 -3.50006497 -3.50006497
## 22 -1.98888595 -1.98888595
## 23  2.10039000  2.10039000
## 24 -0.88251371 -0.88251371