ALS Spacial Cross-Validation

Description

Fit Adaptive Least Squares with k-fold cross-validation

Usage

1
Hals.fastcv.snow(j, rm.ndx, Z, Hs, Ht, Hst.ls, GP.mx)

Arguments

j

Index used by snowfall. A scalar integer. Which row of GP.mx to use for the ALS hyperparameters, GP.

rm.ndx

A list of vectors of indices to remove for k-fold cross-validation.

Z

Data. A τ x n numeric matrix.

Hs

Spacial covariates. An n x p_s numeric matrix.

Ht

Temporal covariates. An τ x p_t numeric matrix.

Hst.ls

Space-time covariates. A list of length τ, each element containing a n x p_st numeric matrix.

GP.mx

Hyperparameters. A k.glob x 2 non-negative matrix. See MSS.snow.

Value

A τ x n numeric matrix. The ALS cross-validated predictions of Z.

See Also

Hals.snow, MSS.snow.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
set.seed(99999)

library(SSsimple)

tau <- 70
n.all <- 14

Hs.all <- matrix(rnorm(n.all), nrow=n.all)
Ht <- matrix(rnorm(tau*2), nrow=tau)
Hst.ls.all <- list()
for(i in 1:tau) { Hst.ls.all[[i]] <- matrix(rnorm(n.all*2), nrow=n.all) }

Hst.combined <- list()
for(i in 1:tau) { 
    Hst.combined[[i]] <- cbind( Hs.all, matrix(Ht[i, ], nrow=n.all, 
    ncol=ncol(Ht), byrow=TRUE), Hst.ls.all[[i]] ) 
}

######## use SSsimple to simulate
sssim.obj <- SS.sim.tv( 0.999, Hst.combined, 0.01, diag(1, n.all), tau )



Z.all <- sssim.obj$Z
Z <- Z.all
n <- n.all

Hst.ls <- Hst.ls.all

Hs <- Hs.all

xrho <- 1/10
xreg <- 1/10

GP.mx <- matrix(c(xrho, xreg), nrow=1)

rm.ndx <- create.rm.ndx.ls(n, 10)

Zcv <- Hals.fastcv.snow(j=1, rm.ndx, Z, Hs, Ht, Hst.ls, GP.mx) 



test.rng <- 20:tau

errs.sq <- (Z - Zcv)^2
sqrt( mean(errs.sq[test.rng, ]) )




## The function is currently defined as
function (j, rm.ndx, Z, Hs, Ht, Hst.ls, GP.mx) 
{
    n <- ncol(Z)
    tau <- nrow(Z)
    rho <- GP.mx[j, 1]
    reg <- GP.mx[j, 2]
    Z.hat <- matrix(NA, tau, n)
    for (drop.ndx in rm.ndx) {
        if (!is.null(Hst.ls)) {
            red.Hst.ls <- list()
            Hst0.ls <- list()
            for (i in 1:tau) {
                red.Hst.ls[[i]] <- Hst.ls[[i]][-drop.ndx, , drop = FALSE]
                Hst0.ls[[i]] <- Hst.ls[[i]][drop.ndx, , drop = FALSE]
            }
        }
        else {
            red.Hst.ls <- NULL
            Hst0.ls <- NULL
        }
        if (!is.null(Hs)) {
            red.Hs <- Hs[-drop.ndx, , drop = FALSE]
            Hs0 <- Hs[drop.ndx, , drop = FALSE]
        }
        else {
            red.Hs <- NULL
            Hs0 <- NULL
        }
        Z.hat[, drop.ndx] <- H.als.b(Z[, -drop.ndx, drop = FALSE], 
            Hs = red.Hs, Ht = Ht, Hst.ls = red.Hst.ls, rho, reg, 
            b.lag = 0, Hs0 = Hs0, Ht0 = Ht, Hst0.ls = Hst0.ls)$Z0.hat
    }
    return(Z.hat)
  }