require(prospectr)
data(NIRsoil)
Yr <- NIRsoil$Nt[as.logical(NIRsoil$train)]
Xr <- NIRsoil$spc[as.logical(NIRsoil$train),]
# Example 1
# Compute a principal components distance
pca.d <- orthoDiss(Xr = Xr, pcSelection = list("cumvar", 0.999),
method = "pca",
local = FALSE,
center = TRUE, scaled = TRUE)
# The final number of pcs used for computing the distance
# matrix of objects in Xr
pca.d$n.components
# The final distance matrix
ds <- pca.d$dissimilarity
# Example 1.1
# Evaluate the distance matrix on the baisis of the
# side information (Yr) associated with Xr
se <- simEval(d = ds, sideInf = Yr)
# The final evaluation results
se$eval
# The final values of the side information (Yr) and the values of
# the side information corresponding to the first nearest neighbours
# found by using the distance matrix
se$firstNN
# Example 1.2
# Evaluate the distance matrix on the baisis of two side
# information (Yr and Yr2)
# variables associated with Xr
Yr2 <- NIRsoil$CEC[as.logical(NIRsoil$train)]
se2 <- simEval(d = ds, sideInf = cbind(Yr, Yr2))
# The final evaluation results
se2$eval
# The final values of the side information variables and the values
# of the side information variables corresponding to the first
# nearest neighbours found by using the distance matrix
se2$firstNN
###
# Example 2
# Evaluate the distances produced by retaining different number of
# principal components (this is the same principle used in the
# optimized principal components approach ("opc"))
# first project the data
pca <- orthoProjection(Xr = Xr, method = "pca",
pcSelection = list("manual", 30),
center = TRUE, scaled = TRUE)
# standardize the scores
scores.s <- sweep(pca$scores, MARGIN = 2,
STATS = pca$sc.sdv, FUN = "/")
rslt <- matrix(NA, ncol(scores.s), 3)
colnames(rslt) <- c("pcs", "rmsd", "r")
rslt[,1] <- 1:ncol(scores.s)
for(i in 1:ncol(scores.s))
{
sc.ipcs <- scores.s[ ,1:i, drop = FALSE]
di <- fDiss(Xr = sc.ipcs, method = "euclid",
center = FALSE, scaled = FALSE)
se <- simEval(d = di, sideInf = Yr)
rslt[i,2:3] <- unlist(se$eval)
}
plot(rslt)
###
# Example 3
# Example 3.1
# Evaluate a dissimilarity matrix computed using a moving window
# correlation method
mwcd <- mcorDiss(Xr = Xr, ws = 35, center = FALSE, scaled = FALSE)
se.mw <- simEval(d = mwcd, sideInf = Yr)
se.mw$eval
# Example 3.2
# Evaluate a dissimilarity matrix computed using the correlation
# method
cd <- corDiss(Xr = Xr, center = FALSE, scaled = FALSE)
se.nc <- simEval(d = cd, sideInf = Yr)
se.nc$eval
Run the code above in your browser using DataLab