Ordered Factor Kernel R6 class
Ordered Factor Kernel R6 class
Usage
k_OrderedFactorKernel(
s2 = 1,
D,
nlevels,
xindex,
p_lower = 1e-08,
p_upper = 5,
p_est = TRUE,
s2_lower = 1e-08,
s2_upper = 1e+08,
s2_est = TRUE,
useC = TRUE,
offdiagequal = 1 - 1e-06
)Format
R6Class object.
Arguments
- s2
Initial variance
- D
Number of input dimensions of data
- nlevels
Number of levels for the factor
- xindex
Index of the factor (which column of X)
- p_lower
Lower bound for p
- p_upper
Upper bound for p
- p_est
Should p be estimated?
- s2_lower
Lower bound for s2
- s2_upper
Upper bound for s2
- s2_est
Should s2 be estimated?
- useC
Should C code used? Not implemented for FactorKernel yet.
- offdiagequal
What should offdiagonal values be set to when the indices are the same? Use to avoid decomposition errors, similar to adding a nugget.
Value
Object of R6Class with methods for fitting GP model.
Super class
GauPro::GauPro_kernel -> GauPro_kernel_OrderedFactorKernel
Public fields
pParameter for correlation
p_estShould p be estimated?
p_lowerLower bound of p
p_upperUpper bound of p
p_lengthlength of p
s2variance
s2_estIs s2 estimated?
logs2Log of s2
logs2_lowerLower bound of logs2
logs2_upperUpper bound of logs2
xindexIndex of the factor (which column of X)
nlevelsNumber of levels for the factor
offdiagequalWhat should offdiagonal values be set to when the indices are the same? Use to avoid decomposition errors, similar to adding a nugget.
Methods
Inherited methods
Method new()
Initialize kernel object
Usage
OrderedFactorKernel$new(
s2 = 1,
D = NULL,
nlevels,
xindex,
p_lower = 1e-08,
p_upper = 5,
p_est = TRUE,
s2_lower = 1e-08,
s2_upper = 1e+08,
s2_est = TRUE,
useC = TRUE,
offdiagequal = 1 - 1e-06
)Arguments
s2Initial variance
DNumber of input dimensions of data
nlevelsNumber of levels for the factor
xindexIndex of X to use the kernel on
p_lowerLower bound for p
p_upperUpper bound for p
p_estShould p be estimated?
s2_lowerLower bound for s2
s2_upperUpper bound for s2
s2_estShould s2 be estimated?
useCShould C code used? Much faster.
offdiagequalWhat should offdiagonal values be set to when the indices are the same? Use to avoid decomposition errors, similar to adding a nugget.
pVector of distances in latent space
Method param_optim_start()
Starting point for parameters for optimization
Method param_optim_start0()
Starting point for parameters for optimization
Method set_params_from_optim()
Set parameters from optimization output
Examples
kk <- OrderedFactorKernel$new(D=1, nlevels=5, xindex=1)
kk$p <- (1:10)/100
kmat <- outer(1:5, 1:5, Vectorize(kk$k))
kmat
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.0000000 0.9999000 0.9991004 0.9964065 0.9900498
#> [2,] 0.9999000 1.0000000 0.9996001 0.9975031 0.9919327
#> [3,] 0.9991004 0.9996001 1.0000000 0.9991004 0.9951120
#> [4,] 0.9964065 0.9975031 0.9991004 1.0000000 0.9984013
#> [5,] 0.9900498 0.9919327 0.9951120 0.9984013 1.0000000
if (requireNamespace("dplyr", quietly=TRUE)) {
library(dplyr)
n <- 20
X <- cbind(matrix(runif(n,2,6), ncol=1),
matrix(sample(1:2, size=n, replace=TRUE), ncol=1))
X <- rbind(X, c(3.3,3), c(3.7,3))
n <- nrow(X)
Z <- X[,1] - (4-X[,2])^2 + rnorm(n,0,.1)
plot(X[,1], Z, col=X[,2])
tibble(X=X, Z) %>% arrange(X,Z)
k2a <- IgnoreIndsKernel$new(k=Gaussian$new(D=1), ignoreinds = 2)
k2b <- OrderedFactorKernel$new(D=2, nlevels=3, xind=2)
k2 <- k2a * k2b
k2b$p_upper <- .65*k2b$p_upper
gp <- GauPro_kernel_model$new(X=X, Z=Z, kernel = k2, verbose = 5,
nug.min=1e-2, restarts=0)
gp$kernel$k1$kernel$beta
gp$kernel$k2$p
gp$kernel$k(x = gp$X)
tibble(X=X, Z=Z, pred=gp$predict(X)) %>% arrange(X, Z)
tibble(X=X[,2], Z) %>% group_by(X) %>% summarize(n=n(), mean(Z))
curve(gp$pred(cbind(matrix(x,ncol=1),1)),2,6, ylim=c(min(Z), max(Z)))
points(X[X[,2]==1,1], Z[X[,2]==1])
curve(gp$pred(cbind(matrix(x,ncol=1),2)), add=TRUE, col=2)
points(X[X[,2]==2,1], Z[X[,2]==2], col=2)
curve(gp$pred(cbind(matrix(x,ncol=1),3)), add=TRUE, col=3)
points(X[X[,2]==3,1], Z[X[,2]==3], col=3)
legend(legend=1:3, fill=1:3, x="topleft")
# See which points affect (5.5, 3 themost)
data.frame(X, cov=gp$kernel$k(X, c(5.5,3))) %>% arrange(-cov)
plot(k2b)
}
#> Optimizing
#> Initial values:
#> $par
#> [1] 0.03164225 -0.17664565 0.79857288 0.95367421 1.31722835 -1.88791534
#>
#> $value
#> [1] 59.5566
#>
#> Restart (parallel): starts pars = 0.03164225 -0.1766456 0.7985729 0.9536742 1.317228 -1.887915
#> start
#> 1 0.0316422532774051,-0.176645645824596,0.798572875095871,0.953674212763142,1.31722835044932,-1.88791533652693
#> 2 0.0316,-0.177,0.799,0.954,1.32,-1.89
#> end value func_evals grad_evals convergence
#> 1 NA 59.5566 1 NA NA
#> 2 -1.82,-1.61,0.609,0.759,0.554,-2 -21.83657 29 29 0
#> message
#> 1 NA
#> 2 CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH
#> * nug is at minimum value after optimizing. Check the fit to see it this caused a bad fit. Consider changing nug.min. This is probably fine for noiseless data.