tests/testthat/test_GetSmoothedCovarSurface.R

# devtools::load_all()
library(testthat)

# GMeanAndGCV

set.seed(1)
pts <- seq(0, 1, by=0.05)
regGrid <- seq(0, 1, by=0.1)
samp3 <- Wiener(50, pts, sparsify=length(pts))
mu3 <- rep(0, length(pts))

# without error
p0 <- SetOptions(samp3$Ly, samp3$Lt, list(dataType='Sparse', error=FALSE, kernel='epan'))
noErr <- GetSmoothedCovarSurface(samp3$Ly, samp3$Lt, mu3, pts, regGrid, p0, useBinnedCov=FALSE)

# with error
p1 <- SetOptions(samp3$Ly, samp3$Lt,list(dataType='Sparse', error=TRUE, kernel='epan'))
Err <- GetSmoothedCovarSurface(samp3$Ly, samp3$Lt, mu3, pts, regGrid, p1, useBinnedCov=FALSE)

# unit tests: test the interface.
rcov3 <- GetRawCov(samp3$Ly, samp3$Lt, pts, mu3, p1$dataType, p1$error)
system.time(tmp <- GCVLwls2DV2(pts, regGrid=regGrid, kern='epan', rcov=rcov3, t=samp3$Lt))
gcvBW3 <- sqrt(tmp$h * tmp$minBW)
sigma23 <- PC_CovE(pts, regGrid, gcvBW3, kernel='epan', rcov=rcov3, rotationCut=c(0.25, 0.75))$sigma2
test_that('Smooth Cov Surface interface is right', {
  expect_equal(rcov3, Err$rawCov)
  expect_equal(as.numeric(gcvBW3), Err$bwCov, tolerance = 0.01, scale = 1)
  # expect_equal(gcvBW3, Err$bwCov, tolerance = 0.05) // If someone gets time check why this doesn't work perfectly.
  # Entry (11,11) is different tha expected and I think this is a boundary issue but I am not sure.
  # This problem is only with the Epan kern, rect and gauss are fine.
  expect_equal(sigma23, Err$sigma2, tolerance = 0.1)
})

# GCV
p2 <- SetOptions(samp3$Ly, samp3$Lt, list(methodBwCov='GCV', dataType='Sparse', error=FALSE, kernel='epan'))
tmp2 <- GetSmoothedCovarSurface(samp3$Ly, samp3$Lt, mu3, pts, regGrid, p2, useBinnedCov=FALSE)
sum((diag(tmp2$smoothCov) - seq(0, 1, by=0.1))^2)

# CV
p3 <- SetOptions(samp3$Ly, samp3$Lt, list(methodBwCov='CV', dataType='Sparse', error=FALSE, kernel='epan'))
system.time(tmp3 <- GetSmoothedCovarSurface(samp3$Ly, samp3$Lt, mu3, pts, regGrid, p3, useBinnedCov=FALSE))
sum((diag(tmp3$smoothCov) - seq(0, 1, by=0.1))^2)

# truncation.
pTrunc <- SetOptions(samp3$Ly, samp3$Lt,list(dataType='Sparse', error=FALSE, kernel='epan', outPercent=c(0.01, 0.99)))
noErrTrunc <- GetSmoothedCovarSurface(samp3$Ly, samp3$Lt, mu3, pts, regGrid, pTrunc, useBinnedCov=FALSE)
test_that('Cov Surface truncation works', {
  expect_equal(noErr$smoothedCov[2:10, 2:10], noErrTrunc$smoothedCov)
  expect_equal(noErr$rawCov, noErrTrunc$rawCov)
  expect_equal(noErr$bwCov, noErrTrunc$bwCov)
  expect_equal(noErr$sigma2, noErrTrunc$sigma2)
})
functionaldata/tPACE documentation built on Aug. 16, 2022, 8:27 a.m.