Guest User

Untitled

a guest
May 27th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #load packages
  2. require(spatial)
  3. require(MASS)
  4. library(gstat)
  5. require(rgdal)
  6. require(sp)
  7. #grid
  8. xy <- expand.grid(1:100, 1:100)
  9. names(xy) <- c("x","y")
  10.  
  11. # create spatially autocorrelated data
  12. g <- gstat(formula=z~1, locations=~x+y, dummy=T, beta=1,
  13. model=vgm(psill=0.1, model="Exp", range=10), nmax=10)
  14.  
  15. # simulate
  16. yy <- predict(g, newdata=xy, nsim=1)
  17. coordinates(yy)=~x+y
  18. spplot(yy, col.regions = rainbow(7)) #plot
  19.  
  20. #derive autocorrelation structure
  21. a<-data.frame(yy)
  22. colnames(a)<-c("x","y","z")
  23. a <- surf.ls(2, a) #fits a trend surface by least-squares
  24. cc<-correlogram(a, 100, xlim=c(0,100), ylab="Autocorr",
  25. xlab="Distance") #plot correlogram
  26. cc #details
  27.  
  28. library(sp)
  29. library(spdep)
  30. data(meuse)
  31. coordinates(meuse) <- ~x+y
  32.  
  33. k.nn <- spdep::knn2nb(spdep::knearneigh(coordinates(meuse)))
  34. bw <- max(unlist(spdep::nbdists(k.nn, coordinates(meuse))))
  35. ( AutoCov <- spdep::autocov_dist(meuse@data[,"copper"], xy = coordinates(meuse),
  36. nbs = bw, style = "W", type = "inverse") )
  37.  
  38. nbd <- nbdists(k.nn, coordinates(meuse))
  39. gl <- lapply(nbd, function(x) 1/x)
  40. lw <- nb2listw(k.nn, glist=gl)
  41. lags <- lag(lw, meuse$copper)
  42. all.equal(lags, AutoCov)
Add Comment
Please, Sign In to add comment