library(rworldmap)
## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type :   vignette('rworldmap')
library(rworldxtra)
library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(spdep)
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(ape)
## Registered S3 method overwritten by 'ape':
##   method   from 
##   plot.mst spdep
library(sp)
library(MVA)
## Loading required package: HSAUR2
## Loading required package: tools
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following object is masked from 'package:ape':
## 
##     zoom
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(corrplot)
## corrplot 0.90 loaded
library(psych)
## 
## Attaching package: 'psych'
## The following object is masked from 'package:Hmisc':
## 
##     describe
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(crayon)
## 
## Attaching package: 'crayon'
## The following object is masked from 'package:psych':
## 
##     %+%
## The following object is masked from 'package:ggplot2':
## 
##     %+%
library(pastecs)
library(readxl)
library(clhs)
library(spatialreg)
## Loading required package: Matrix
## Registered S3 methods overwritten by 'spatialreg':
##   method                   from 
##   residuals.stsls          spdep
##   deviance.stsls           spdep
##   coef.stsls               spdep
##   print.stsls              spdep
##   summary.stsls            spdep
##   print.summary.stsls      spdep
##   residuals.gmsar          spdep
##   deviance.gmsar           spdep
##   coef.gmsar               spdep
##   fitted.gmsar             spdep
##   print.gmsar              spdep
##   summary.gmsar            spdep
##   print.summary.gmsar      spdep
##   print.lagmess            spdep
##   summary.lagmess          spdep
##   print.summary.lagmess    spdep
##   residuals.lagmess        spdep
##   deviance.lagmess         spdep
##   coef.lagmess             spdep
##   fitted.lagmess           spdep
##   logLik.lagmess           spdep
##   fitted.SFResult          spdep
##   print.SFResult           spdep
##   fitted.ME_res            spdep
##   print.ME_res             spdep
##   print.lagImpact          spdep
##   plot.lagImpact           spdep
##   summary.lagImpact        spdep
##   HPDinterval.lagImpact    spdep
##   print.summary.lagImpact  spdep
##   print.sarlm              spdep
##   summary.sarlm            spdep
##   residuals.sarlm          spdep
##   deviance.sarlm           spdep
##   coef.sarlm               spdep
##   vcov.sarlm               spdep
##   fitted.sarlm             spdep
##   logLik.sarlm             spdep
##   anova.sarlm              spdep
##   predict.sarlm            spdep
##   print.summary.sarlm      spdep
##   print.sarlm.pred         spdep
##   as.data.frame.sarlm.pred spdep
##   residuals.spautolm       spdep
##   deviance.spautolm        spdep
##   coef.spautolm            spdep
##   fitted.spautolm          spdep
##   print.spautolm           spdep
##   summary.spautolm         spdep
##   logLik.spautolm          spdep
##   print.summary.spautolm   spdep
##   print.WXImpact           spdep
##   summary.WXImpact         spdep
##   print.summary.WXImpact   spdep
##   predict.SLX              spdep
## 
## Attaching package: 'spatialreg'
## The following objects are masked from 'package:spdep':
## 
##     anova.sarlm, as_dgRMatrix_listw, as_dsCMatrix_I, as_dsCMatrix_IrW,
##     as_dsTMatrix_listw, as.spam.listw, bptest.sarlm, can.be.simmed,
##     cheb_setup, coef.gmsar, coef.sarlm, coef.spautolm, coef.stsls,
##     create_WX, deviance.gmsar, deviance.sarlm, deviance.spautolm,
##     deviance.stsls, do_ldet, eigen_pre_setup, eigen_setup, eigenw,
##     errorsarlm, fitted.gmsar, fitted.ME_res, fitted.sarlm,
##     fitted.SFResult, fitted.spautolm, get.ClusterOption,
##     get.coresOption, get.mcOption, get.VerboseOption,
##     get.ZeroPolicyOption, GMargminImage, GMerrorsar, griffith_sone,
##     gstsls, Hausman.test, HPDinterval.lagImpact, impacts, intImpacts,
##     Jacobian_W, jacobianSetup, l_max, lagmess, lagsarlm, lextrB,
##     lextrS, lextrW, lmSLX, logLik.sarlm, logLik.spautolm, LR.sarlm,
##     LR1.sarlm, LR1.spautolm, LU_prepermutate_setup, LU_setup,
##     Matrix_J_setup, Matrix_setup, mcdet_setup, MCMCsamp, ME, mom_calc,
##     mom_calc_int2, moments_setup, powerWeights, predict.sarlm,
##     predict.SLX, print.gmsar, print.ME_res, print.sarlm,
##     print.sarlm.pred, print.SFResult, print.spautolm, print.stsls,
##     print.summary.gmsar, print.summary.sarlm, print.summary.spautolm,
##     print.summary.stsls, residuals.gmsar, residuals.sarlm,
##     residuals.spautolm, residuals.stsls, sacsarlm, SE_classic_setup,
##     SE_interp_setup, SE_whichMin_setup, set.ClusterOption,
##     set.coresOption, set.mcOption, set.VerboseOption,
##     set.ZeroPolicyOption, similar.listw, spam_setup, spam_update_setup,
##     SpatialFiltering, spautolm, spBreg_err, spBreg_lag, spBreg_sac,
##     stsls, subgraph_eigenw, summary.gmsar, summary.sarlm,
##     summary.spautolm, summary.stsls, trW, vcov.sarlm, Wald1.sarlm
datos<-read_excel("/Users/sindyluh/Downloads/datos.xlsx")
dfCaMg=data.frame(datos)

#Muestreo espacial
n=0.80*403
n=round(n,0)
df_sindy=data.frame(x=dfCaMg$Long,
                    y=dfCaMg$Lat,
                    Mg=dfCaMg$relacion
                    )
res<-clhs(df_sindy, size=n, iter=100, progress=FALSE,simple=TRUE)
df_sindy=dfCaMg[res,]

dfCaMg=df_sindy

#matriz de peso
X=as.matrix(data.frame(dfCaMg[,4:14 ]))#Tabla con todo menos coordenadas
XY=as.matrix(data.frame(dfCaMg[,2:3 ]))#Tabla con coordenadas
CaMg.d=as.matrix(dist(XY, diag=T, upper=T))#Matriz
CaMg.d.inv<-as.matrix(1/CaMg.d)#Inversa de la matriz
diag(CaMg.d.inv)<-0#Asignando 0 a la diag
W=as.matrix(CaMg.d.inv)#matriz de peso basado en distancia
SUMAS=apply(W,1,sum)
We=W/SUMAS#Matriz estandarizada

#Creando matriz de peso con funciones de librerĆ­a
contnb=dnearneigh(coordinates(XY),0,380000,longlat=F)
dlist<-nbdists(contnb,XY)
dlist<-lapply(dlist,function(x)1/x)
Wve=nb2listw(contnb,glist = dlist,style="W")

Modelo 1: SAR

modelo.arp=spautolm(relacion~1,data=dfCaMg,listw=Wve)
summary(modelo.arp)
## 
## Call: spautolm(formula = relacion ~ 1, data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.92331 -1.09072 -0.17582  0.60749 24.19328 
## 
## Coefficients: 
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)   5.4427     1.9945  2.7289 0.006355
## 
## Lambda: 0.9393 LR test value: 33.26 p-value: 8.0635e-09 
## Numerical Hessian standard error of lambda: 0.059534 
## 
## Log likelihood: -709.4037 
## ML residual variance (sigma squared): 4.7196, (sigma: 2.1725)
## Number of observations: 322 
## Number of parameters estimated: 3 
## AIC: 1424.8
library(spatialreg)
res1=modelo.arp$fit$residuals #Tomar residuales de la salida de modelo.arp
Moran.I(res1,CaMg.d.inv)
## $observed
## [1] 0.03622526
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004914095
## 
## $p.value
## [1] 1.110223e-15
#Interpretación: dependencia 

Modelo 2: SARAR

mod2 = sacsarlm(relacion~1,data=dfCaMg,listw=Wve)
## Warning in sacsarlm(relacion ~ 1, data = dfCaMg, listw = Wve): inversion of asymptotic covariance matrix failed for tol.solve = 2.22044604925031e-16 
##   reciprocal condition number = 3.76734e-18 - using numerical Hessian.
summary(mod2)
## 
## Call:sacsarlm(formula = relacion ~ 1, data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.19293 -0.99100 -0.18593  0.62419 24.49762 
## 
## Type: sac 
## Coefficients: (numerical Hessian approximate standard errors) 
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)   0.9194     1.1608  0.7921   0.4283
## 
## Rho: 0.87651
## Approximate (numerical Hessian) standard error: 0.11779
##     z-value: 7.4415, p-value: 9.9476e-14
## Lambda: 0.87651
## Approximate (numerical Hessian) standard error: 0.11791
##     z-value: 7.4337, p-value: 1.0569e-13
## 
## LR test value: 47.492, p-value: 4.8675e-11
## 
## Log likelihood: -702.2877 for sac model
## ML residual variance (sigma squared): 4.4836, (sigma: 2.1174)
## Number of observations: 322 
## Number of parameters estimated: 4 
## AIC: 1412.6, (AIC for lm: 1456.1)
res2=mod2$residuals #Tomar residuales de la salida de modelo.arp
Moran.I(res2, CaMg.d.inv)
## $observed
## [1] 0.01838899
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004843046
## 
## $p.value
## [1] 8.986129e-06
#Interpretación:dependencia

Modelo 3:SLM

mser1=errorsarlm(formula=relacion~z+cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve)
summary(mser1)
## 
## Call:errorsarlm(formula = relacion ~ z + cos + K + Na + CICE + Cu + 
##     Zn, data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4.99031 -0.82857 -0.17828  0.66042 24.11000 
## 
## Type: error 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  3.7767357  1.2375011  3.0519 0.0022739
## z            0.0068642  0.0110232  0.6227 0.5334765
## cos          1.6362573  0.3246694  5.0398 4.661e-07
## K           -4.6523455  0.9797090 -4.7487 2.047e-06
## Na          -3.1730846  0.9212574 -3.4443 0.0005725
## CICE         0.1678637  0.0329975  5.0872 3.635e-07
## Cu          -0.1780271  0.0830709 -2.1431 0.0321072
## Zn          -0.1274413  0.0652698 -1.9525 0.0508753
## 
## Lambda: 0.86799, LR test value: 11.584, p-value: 0.00066515
## Asymptotic standard error: 0.090642
##     z-value: 9.5759, p-value: < 2.22e-16
## Wald statistic: 91.699, p-value: < 2.22e-16
## 
## Log likelihood: -666.8816 for error model
## ML residual variance (sigma squared): 3.6431, (sigma: 1.9087)
## Number of observations: 322 
## Number of parameters estimated: 10 
## AIC: 1353.8, (AIC for lm: 1363.3)
res3=mser1$residuals
Moran.I(res3, CaMg.d.inv)
## $observed
## [1] 0.01506639
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004630904
## 
## $p.value
## [1] 8.631406e-05
#Interpretación:  dependencia,lambda sign

#Sin z
mser2=errorsarlm(formula=relacion~cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve)
summary(mser2)
## 
## Call:errorsarlm(formula = relacion ~ cos + K + Na + CICE + Cu + Zn, 
##     data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4.95790 -0.85811 -0.10341  0.65327 24.06531 
## 
## Type: error 
## Coefficients: (asymptotic standard errors) 
##              Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  4.282984   0.832249  5.1463 2.657e-07
## cos          1.641851   0.324728  5.0561 4.280e-07
## K           -4.687857   0.979335 -4.7868 1.695e-06
## Na          -3.172288   0.921910 -3.4410 0.0005796
## CICE         0.168385   0.033007  5.1015 3.371e-07
## Cu          -0.184216   0.082679 -2.2281 0.0258744
## Zn          -0.125480   0.065267 -1.9226 0.0545331
## 
## Lambda: 0.85515, LR test value: 11.336, p-value: 0.00076022
## Asymptotic standard error: 0.099009
##     z-value: 8.6371, p-value: < 2.22e-16
## Wald statistic: 74.599, p-value: < 2.22e-16
## 
## Log likelihood: -667.0707 for error model
## ML residual variance (sigma squared): 3.6497, (sigma: 1.9104)
## Number of observations: 322 
## Number of parameters estimated: 9 
## AIC: 1352.1, (AIC for lm: 1361.5)
res4=mser2$residuals
Moran.I(res4, CaMg.d.inv)
## $observed
## [1] 0.01418225
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004639448
## 
## $p.value
## [1] 0.000192732
#Interpretación:  dependencia,lambda sign

#Sin Zn
mser3=errorsarlm(formula=relacion~cos+K+Na+CICE+Cu,data=dfCaMg,listw=Wve)
summary(mser3)
## 
## Call:errorsarlm(formula = relacion ~ cos + K + Na + CICE + Cu, data = dfCaMg, 
##     listw = Wve)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -5.602866 -0.932206 -0.095637  0.645459 24.401218 
## 
## Type: error 
## Coefficients: (asymptotic standard errors) 
##              Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  4.135755   0.749276  5.5197 3.396e-08
## cos          1.582547   0.324812  4.8722 1.104e-06
## K           -4.805469   0.983390 -4.8866 1.026e-06
## Na          -3.070768   0.925687 -3.3173  0.000909
## CICE         0.183152   0.032338  5.6637 1.481e-08
## Cu          -0.284781   0.064830 -4.3927 1.119e-05
## 
## Lambda: 0.83243, LR test value: 9.6288, p-value: 0.0019155
## Asymptotic standard error: 0.11356
##     z-value: 7.3305, p-value: 2.2937e-13
## Wald statistic: 53.736, p-value: 2.2926e-13
## 
## Log likelihood: -668.8967 for error model
## ML residual variance (sigma squared): 3.695, (sigma: 1.9222)
## Number of observations: 322 
## Number of parameters estimated: 8 
## AIC: 1353.8, (AIC for lm: 1361.4)
res5=mser3$residuals
Moran.I(res5, CaMg.d.inv)
## $observed
## [1] 0.01220543
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004616178
## 
## $p.value
## [1] 0.0009036837
#Interpretación:  A pesar de eliminar variables no explicativas el modelo sigue presentando dependencia

Modelo 4: SEM

mod4=lagsarlm(formula=relacion~z+cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve)
summary(mod4) 
## 
## Call:lagsarlm(formula = relacion ~ z + cos + K + Na + CICE + Cu + 
##     Zn, data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -5.10030 -0.85025 -0.13572  0.55634 24.17533 
## 
## Type: lag 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  0.3752954  0.7722050  0.4860 0.6269637
## z           -0.0021859  0.0067749 -0.3227 0.7469587
## cos          1.5568370  0.3103088  5.0171 5.247e-07
## K           -4.7249558  0.9473681 -4.9875 6.118e-07
## Na          -3.0946650  0.8976060 -3.4477 0.0005654
## CICE         0.1705593  0.0312384  5.4599 4.763e-08
## Cu          -0.1840667  0.0788317 -2.3349 0.0195469
## Zn          -0.1147232  0.0632165 -1.8148 0.0695599
## 
## Rho: 0.87759, LR test value: 18.425, p-value: 1.7671e-05
## Asymptotic standard error: 0.083207
##     z-value: 10.547, p-value: < 2.22e-16
## Wald statistic: 111.24, p-value: < 2.22e-16
## 
## Log likelihood: -663.4612 for lag model
## ML residual variance (sigma squared): 3.5647, (sigma: 1.888)
## Number of observations: 322 
## Number of parameters estimated: 10 
## AIC: 1346.9, (AIC for lm: 1363.3)
## LM test for residual autocorrelation
## test value: 3.0266, p-value: 0.081911
res6 = mod4$residuals
Moran.I(res6, CaMg.d.inv)
## $observed
## [1] 0.007684906
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004589105
## 
## $p.value
## [1] 0.01860074
#Interpretación: dependencia ,Rho sign

#Eliminando z
mod4.1=lagsarlm(formula=relacion~cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve)
summary(mod4.1) 
## 
## Call:lagsarlm(formula = relacion ~ cos + K + Na + CICE + Cu + Zn, 
##     data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -5.11911 -0.82777 -0.14894  0.56363 24.20793 
## 
## Type: lag 
## Coefficients: (asymptotic standard errors) 
##              Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  0.195351   0.556621  0.3510 0.7256190
## cos          1.546726   0.308918  5.0069 5.531e-07
## K           -4.708386   0.946238 -4.9759 6.494e-07
## Na          -3.096701   0.897712 -3.4495 0.0005615
## CICE         0.171088   0.031194  5.4847 4.141e-08
## Cu          -0.179643   0.077708 -2.3118 0.0207903
## Zn          -0.115452   0.063188 -1.8271 0.0676812
## 
## Rho: 0.87778, LR test value: 18.451, p-value: 1.7436e-05
## Asymptotic standard error: 0.082888
##     z-value: 10.59, p-value: < 2.22e-16
## Wald statistic: 112.15, p-value: < 2.22e-16
## 
## Log likelihood: -663.5134 for lag model
## ML residual variance (sigma squared): 3.5658, (sigma: 1.8883)
## Number of observations: 322 
## Number of parameters estimated: 9 
## AIC: 1345, (AIC for lm: 1361.5)
## LM test for residual autocorrelation
## test value: 3.6976, p-value: 0.054491
res7 = mod4.1$residuals
Moran.I(res7, CaMg.d.inv)
## $observed
## [1] 0.008525429
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004585149
## 
## $p.value
## [1] 0.01112391
#Interpretación: modelo empeoró

Modelo 5:SAC

mod5=sacsarlm(formula=relacion~z+cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve)
summary(mod5)
## 
## Call:sacsarlm(formula = relacion ~ z + cos + K + Na + CICE + Cu + 
##     Zn, data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -5.09339 -0.81223 -0.13995  0.54761 24.26443 
## 
## Type: sac 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  0.3764893  1.9433438  0.1937 0.8463852
## z            0.0019981  0.0096171  0.2078 0.8354144
## cos          1.5448265  0.3213807  4.8068 1.533e-06
## K           -4.4782471  0.9631628 -4.6495 3.327e-06
## Na          -3.0909127  0.9057053 -3.4127 0.0006432
## CICE         0.1595294  0.0323275  4.9348 8.024e-07
## Cu          -0.1544459  0.0817391 -1.8895 0.0588250
## Zn          -0.1305425  0.0639727 -2.0406 0.0412907
## 
## Rho: 0.81222
## Asymptotic standard error: 0.41701
##     z-value: 1.9477, p-value: 0.051447
## Lambda: 0.59697
## Asymptotic standard error: 0.81771
##     z-value: 0.73005, p-value: 0.46536
## 
## LR test value: 20.179, p-value: 4.152e-05
## 
## Log likelihood: -662.5844 for sac model
## ML residual variance (sigma squared): 3.5423, (sigma: 1.8821)
## Number of observations: 322 
## Number of parameters estimated: 11 
## AIC: 1347.2, (AIC for lm: 1363.3)
res8 = mod5$residuals
Moran.I(res8, CaMg.d.inv)
## $observed
## [1] 0.004269304
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004566282
## 
## $p.value
## [1] 0.1058362
#Interpretación: no dependencia espacial, Rho sig y lambda no sig

#Sin z
mod5.1=sacsarlm(formula=relacion~cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve)
summary(mod5.1)
## 
## Call:sacsarlm(formula = relacion ~ cos + K + Na + CICE + Cu + Zn, 
##     data = dfCaMg, listw = Wve)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -5.08319 -0.83397 -0.15160  0.55662 24.24496 
## 
## Type: sac 
## Coefficients: (asymptotic standard errors) 
##              Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  0.510445   1.810850  0.2819   0.77803
## cos          1.548284   0.320347  4.8331 1.344e-06
## K           -4.497118   0.962032 -4.6746 2.945e-06
## Na          -3.090580   0.905251 -3.4141   0.00064
## CICE         0.159884   0.032252  4.9573 7.149e-07
## Cu          -0.157604   0.081221 -1.9404   0.05233
## Zn          -0.129450   0.063911 -2.0255   0.04282
## 
## Rho: 0.81666
## Asymptotic standard error: 0.37091
##     z-value: 2.2018, p-value: 0.027682
## Lambda: 0.57185
## Asymptotic standard error: 0.78132
##     z-value: 0.73189, p-value: 0.46423
## 
## LR test value: 20.266, p-value: 3.974e-05
## 
## Log likelihood: -662.6056 for sac model
## ML residual variance (sigma squared): 3.5436, (sigma: 1.8824)
## Number of observations: 322 
## Number of parameters estimated: 10 
## AIC: 1345.2, (AIC for lm: 1361.5)
res9 = mod5.1$residuals
Moran.I(res9, CaMg.d.inv)
## $observed
## [1] 0.004001731
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004569621
## 
## $p.value
## [1] 0.1193616
#Interpretación: no dependencia espacial, Rho sig y lambda no sig. Aunque lambda disminuyó un poco

#Sin Zn
mod5.2=sacsarlm(formula=relacion~cos+K+Na+CICE+Cu,data=dfCaMg,listw=Wve)
summary(mod5.2)
## 
## Call:sacsarlm(formula = relacion ~ cos + K + Na + CICE + Cu, data = dfCaMg, 
##     listw = Wve)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -5.741320 -0.850639 -0.079101  0.540419 24.582036 
## 
## Type: sac 
## Coefficients: (asymptotic standard errors) 
##              Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  0.397308   1.543982  0.2573  0.796927
## cos          1.491022   0.318985  4.6743 2.950e-06
## K           -4.633935   0.964956 -4.8022 1.569e-06
## Na          -2.987160   0.908377 -3.2885  0.001007
## CICE         0.175995   0.031501  5.5869 2.312e-08
## Cu          -0.261861   0.063615 -4.1163 3.849e-05
## 
## Rho: 0.81292
## Asymptotic standard error: 0.31692
##     z-value: 2.5651, p-value: 0.010315
## Lambda: 0.48074
## Asymptotic standard error: 0.75972
##     z-value: 0.63278, p-value: 0.52688
## 
## LR test value: 18.202, p-value: 0.00011157
## 
## Log likelihood: -664.6103 for sac model
## ML residual variance (sigma squared): 3.5926, (sigma: 1.8954)
## Number of observations: 322 
## Number of parameters estimated: 9 
## AIC: 1347.2, (AIC for lm: 1361.4)
res10 = mod5.2$residuals
Moran.I(res10, CaMg.d.inv)
## $observed
## [1] 0.002954723
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004546264
## 
## $p.value
## [1] 0.1818241
#Interpretación: no dependencia espacial(mejoro), Rho sig y lambda no sig. (empeoró)

Modelo 6:GNS

mod6=sacsarlm(formula=relacion~z+cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve,type="mixed")
summary(mod6)
## 
## Call:sacsarlm(formula = relacion ~ z + cos + K + Na + CICE + Cu + 
##     Zn, data = dfCaMg, listw = Wve, type = "mixed")
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4.82474 -0.81637 -0.13113  0.52154 23.90671 
## 
## Type: sacmixed 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)   8.759321   9.608682  0.9116 0.3619767
## z             0.019108   0.021691  0.8809 0.3783581
## cos           1.481342   0.329212  4.4997 6.806e-06
## K            -4.158869   0.973561 -4.2718 1.939e-05
## Na           -3.052916   0.904044 -3.3770 0.0007329
## CICE          0.139139   0.034015  4.0905 4.304e-05
## Cu           -0.102227   0.084915 -1.2039 0.2286373
## Zn           -0.156419   0.065444 -2.3901 0.0168423
## lag.z        -0.078898   0.055518 -1.4211 0.1552761
## lag.cos       1.789815   4.290850  0.4171 0.6765879
## lag.K       -20.657963  17.933601 -1.1519 0.2493565
## lag.Na        3.987532  13.051514  0.3055 0.7599682
## lag.CICE      0.857792   0.614776  1.3953 0.1629276
## lag.Cu       -2.256618   1.314093 -1.7172 0.0859346
## lag.Zn        0.904086   0.920630  0.9820 0.3260851
## 
## Rho: -0.067288
## Asymptotic standard error: 1.213
##     z-value: -0.055472, p-value: 0.95576
## Lambda: 0.42936
## Asymptotic standard error: 0.85617
##     z-value: 0.5015, p-value: 0.61602
## 
## LR test value: 35.974, p-value: 4.0071e-05
## 
## Log likelihood: -654.6868 for sacmixed model
## ML residual variance (sigma squared): 3.4101, (sigma: 1.8466)
## Number of observations: 322 
## Number of parameters estimated: 18 
## AIC: 1345.4, (AIC for lm: 1363.3)
res11 = mod6$residuals
Moran.I(res11, CaMg.d.inv)
## $observed
## [1] 0.002594858
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004553396
## 
## $p.value
## [1] 0.2098289
#Interpretación:no dep,Rho y lambda no sig

Modelo 7:SDE

mod7=lagsarlm(formula=relacion~z+cos+K+Na+CICE+Cu+Zn,data=dfCaMg,listw=Wve,type="mixed")
summary(mod7)
## 
## Call:lagsarlm(formula = relacion ~ z + cos + K + Na + CICE + Cu + 
##     Zn, data = dfCaMg, listw = Wve, type = "mixed")
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4.74911 -0.81162 -0.12396  0.55325 23.87339 
## 
## Type: mixed 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value  Pr(>|z|)
## (Intercept)   6.293905   6.170242  1.0200 0.3077086
## z             0.016601   0.021431  0.7746 0.4385610
## cos           1.494790   0.331000  4.5160 6.302e-06
## K            -4.170348   0.974160 -4.2810 1.861e-05
## Na           -3.105862   0.908196 -3.4198 0.0006266
## CICE          0.139410   0.034082  4.0904 4.306e-05
## Cu           -0.102126   0.084900 -1.2029 0.2290180
## Zn           -0.158448   0.065526 -2.4181 0.0156028
## lag.z        -0.067546   0.047477 -1.4227 0.1548160
## lag.cos       1.353409   3.455807  0.3916 0.6953293
## lag.K       -16.880843  11.787933 -1.4320 0.1521311
## lag.Na        7.172463  11.748477  0.6105 0.5415296
## lag.CICE      0.751621   0.335928  2.2374 0.0252573
## lag.Cu       -2.223693   0.900195 -2.4702 0.0135025
## lag.Zn        1.046389   0.866881  1.2071 0.2274038
## 
## Rho: 0.13527, LR test value: 0.069628, p-value: 0.79188
## Asymptotic standard error: 0.39696
##     z-value: 0.34077, p-value: 0.73327
## Wald statistic: 0.11613, p-value: 0.73327
## 
## Log likelihood: -654.9767 for mixed model
## ML residual variance (sigma squared): 3.4218, (sigma: 1.8498)
## Number of observations: 322 
## Number of parameters estimated: 17 
## AIC: 1344, (AIC for lm: 1342)
## LM test for residual autocorrelation
## test value: 3.8214, p-value: 0.050603
res12 = mod7$residuals
Moran.I(res12, CaMg.d.inv)
## $observed
## [1] 0.003721987
## 
## $expected
## [1] -0.003115265
## 
## $sd
## [1] 0.004563814
## 
## $p.value
## [1] 0.1340958
#Interpretación: hay dependencia, Rho no significativo
#Conclusión: al parecer ningún modelo ajusta para los datos

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.