SecDim
Package for The Second
Dimension of Spatial Association
SecDim
packageInstall and use the package SecDim
## install and library the pacakge
install.packages("SecDim")
library("SecDim")
Here is an example of the spatial prediction using SDA models.
# spatial data of response variables
data("obs")
# spatial data of optional SDA explanatory variables:
# b = 1, 3, 5, 7, and 9 km
# tau = seq(0, 1, 0.1)
# eight variables
data("sample_vars_sda")
$y <- obs$Cr_ppm
obshist(obs$y)
$y <- log(obs$y)
obshist(obs$y)
<- rmvoutlier(obs$y)
krm <- obs$y[-krm]
y <- lapply(sample_vars_sda, function(x) x[-krm,]) x
system.time({ # ~2s
<- selectvarsda(y, xlist = x)
sx })
<- cbind(y, sx)
data.sda <- lm(y ~., data.sda)
sda.lm summary(sda.lm)
data("sample_vars_fda")
<- data.frame(y, sample_vars_fda[-krm,])
data.fda <- lm(y ~., data.fda)
fda.lm summary(fda.lm)
## install and library the pacakge
install.packages("SecDim")
library("SecDim")
<- function(o, p) 1 - sum((o-p)^2)/sum((o-mean(o))^2)
R2
## Example
# spatial data of response variables
data("obs")
# spatial data of optional SDA explanatory variables:
# b = 1, 3, 5, 7, and 9 km
# tau = seq(0, 1, 0.1)
# eight variables
data("sample_vars_sda")
# data pre-processing: logarithm transformation
$y <- obs$Cr_ppm
obshist(obs$y)
$y <- log(obs$y)
obshist(obs$y)
################################################
## SDA cross validation
################################################
# cross validation: 70% training and 30% testing
set.seed(100)
<- sample(nrow(obs), 0.7*nrow(obs), replace = FALSE)
train
<- obs[train,]
trainy <- obs[-train,]
testy <- lapply(sample_vars_sda, function(x) x[train,])
trainx <- lapply(sample_vars_sda, function(x) x[-train,])
testx
# removing outliers for training data
<- rmvoutlier(trainy$y)
krm <- trainy$y[-krm]
trainy <- lapply(trainx, function(x) x[-krm,])
trainx
# generating explanatory variables for testing data
<- sdapredvars(testx)
sdaxv
# selecting the second dimension variables for SDA models
system.time({ # ~1.8s
<- selectvarsda(y = trainy, xlist = trainx)
sx
})
# SDA modeling and prediction
<- cbind("y" = trainy, sx)
data.sda <- lm(y ~., data.sda)
sda.lm <- predict(sda.lm, newdata = sdaxv)
sda.lm.pred R2(testy$y, sda.lm.pred)
plot(testy$y, sda.lm.pred, xlim = c(3, 7), ylim = c(3, 7))
################################################
## FDA cross validation
################################################
data("sample_vars_fda")
# cross validation: 70% training and 30% testing
set.seed(100)
<- sample(nrow(obs), 0.7*nrow(obs), replace = FALSE)
train
<- obs[train,]
trainy <- obs[-train,]
testy <- sample_vars_fda[train,]
trainx <- sample_vars_fda[-train,]
testx
# removing outliers for training data
<- rmvoutlier(trainy$y)
krm <- trainy$y[-krm]
trainy <- trainx[-krm,]
trainx
# FDA modeling and prediction
<- data.frame("y" = trainy, trainx)
data.fda <- lm(y ~., data.fda)
fda.lm <- predict(fda.lm, newdata = data.frame(testx))
fda.lm.pred R2(testy$y, fda.lm.pred)
plot(testy$y, fda.lm.pred, xlim = c(3, 7), ylim = c(3, 7))