Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. ## Goal of this document: Study how ASReml-R splits up the error variance between spatially correlated error variance and spatially uncorrelated error variance (i.e. "nugget variance" in geostatistics).
  2. ## Load packages
  3. library(RandomFields)
  4. library(LaplacesDemon)
  5. library(asreml)
  6.  
  7. ## Set random seeed
  8. RFoptions(seed=0)
  9.  
  10. ## Create dataset with 50 row and 100 columns
  11. data <- expand.grid(row=1:50,col=1:100)
  12. data$obs <- as.factor(1:nrow(data))
  13.  
  14. ## Define parameters
  15. intercept <- 3
  16. siguncorres <- sqrt(9)
  17. sigcorres <- sqrt(25)
  18. rho=0.5
  19.  
  20. ## Initialize AR1 simulation model
  21. model <- RMexp(scale=-1/log(rho), var=sigcorres^2)
  22.  
  23. ## Add spatially correlated and spatially uncorrelated error
  24. autocorrerror <- RFsimulate(model,x=data$row,y=data$col)@data[[1]]
  25. uncorrerror <- rnorm(nrow(data),0,siguncorres)
  26. data$y <- intercept+autocorrerror+uncorrerror
  27.  
  28. ## Convert as factor for asreml
  29. data$row <- as.factor(data$row)
  30. data$col <- as.factor(data$col)
  31.  
  32. ## Dataset should be with columns nested within rows
  33. data <- data[order(data$row),]
  34. m0 <- asreml(y ~ 1,rcov=~ar1(row):ar1(col),random=~obs,na.method.X="include",data=data,trace=F)
  35. m0 <- update(m0)
  36. ## Equivalent model with the exponential function
  37. #m1 <- asreml(y ~ 1,rcov=~exp(row):exp(col),random=~obs,na.method.X="include",data=data,trace=F)
  38.  
  39. m <- m0
  40. m$coefficients$fixed
  41. summary(m)$varcomp
  42. ## Check 95% confidence intervals
  43. c(summary(m)$varcomp[1,2]-qnorm(0.975)*summary(m)$varcomp[1,3],summary(m)$varcomp[1,2]+qnorm(0.975)*summary(m)$varcomp[1,3])
  44. c(summary(m)$varcomp[2,2]-qnorm(0.975)*summary(m)$varcomp[2,3],summary(m)$varcomp[2,2]+qnorm(0.975)*summary(m)$varcomp[2,3])
  45.  
  46. ## Check variogram
  47. plot.asrVariogram(variogram(m))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement