# Example code to promote familiarity with the functionality of package SpatialADAI #################################################################################################### # Preliminaries #################################################################################################### # Installation ############### library(devtools) # preceded by install.packages("devtools") if necessary install_url("http://www.lancaster.ac.uk/~wadswojl/SpatialADAI_0.1.0.tar.gz") #################################################################################################### # First loading ################ # After installation, load the library library(SpatialADAI) # For more info / to see a list of functions and their help files ?SpatialADAI #################################################################################################### # Notes ################ # Recommended or required packages are the following: library(mvtnorm) library(mvPot) library(fields) library(evd) #################################################################################################### # Example usage ################ # Simulate data from the model at a grid of locations and plot: par0 <- c(0.4,log(1),1) #the first parameter is delta (weight parameter), the two other are for the correlation function in W coord <- expand.grid(seq(0,2,len=20),seq(0,2,len=20)) # 400 stations sigma0 <- exp(-(as.matrix(dist(coord))/exp(par0[2]))^par0[3]) datU <- rC(1,par0[1],sigma0) # n=300 replicates filled.contour(seq(0,2,len=20),seq(0,2,len=20),matrix(datU,20,20)) # Simulate data from the model at 20 locations: coord <- matrix(runif(20*2),ncol=2) # D=20 stations sigma0 <- exp(-(as.matrix(dist(coord))/exp(par0[2]))^par0[3]) datU <- rC(300,par0[1],sigma0) # n=300 replicates thresh <- 0.95 #threshold (same threshold for each marginal) subset <- c(1:5) #subset of locations # single evaluation of the likelihood at a subset of locations (recommended to test this first to understand approximate optimization time) system.time(print(test<-fit.cop.Ddim.QMC(datU=datU[,subset], coord = coord[subset,], thresh = thresh,init.val = par0,optim=FALSE))) # Alternative using function which is comparable or faster in low dimensions, but slower in high dimensions system.time(print(test<-fit.cop.Ddim(datU=datU[,subset], coord = coord[subset,], thresh = thresh,init.val = par0,optim=FALSE))) subset <- c(1:10) #subset of locations # single evaluation of the likelihood at a subset of locations (recommended to test this first to understand approximate optimization time) system.time(print(test<-fit.cop.Ddim.QMC(datU=datU[,subset], coord = coord[subset,], thresh = thresh,init.val = par0,optim=FALSE))) system.time(print(test<-fit.cop.Ddim.QMC(datU=datU[,subset], coord = coord[subset,], thresh = thresh,init.val = par0,optim=FALSE,prime = 311))) # Alternative using function which is comparable in low dimensions, but slower in high dimensions system.time(print(test<-fit.cop.Ddim(datU=datU[,subset], coord = coord[subset,], thresh = thresh,init.val = par0,optim=FALSE))) #################################################################################################### # Plot empirical and model \chi_u datU <- rC(3000,par0[1],sigma0) # n=300 replicates chiu.est(x=datU, u=seq(0.8,0.99,len=20),nbs=100,blocklength = 1, ylab=expression(chi [u])) cm<-chiuD.QMC(u=seq(0.8,0.99,len=20), cov.par = c(exp(par0[2]),par0[3]),dist = rdist(coord),delta = par0[1]) lines(seq(0.8,0.99,len=20), cm, col=4) #################################################################################################### # Bivariate simulation and fitting ################################## delta<-0.53 theta<-0.8 datU <- rC2(1000,delta = delta,theta = theta,model = "IEVL") # W copula is inverted extreme value logistic plot(datU) fit<-fit.cop.2dim(datU = datU,thresh = thresh,init.val = c(delta,theta),cens.type = "max",model = "IEVL",optim = TRUE) fit