Latent Factor Kernel R6 class
Latent Factor Kernel R6 class
Usage
k_LatentFactorKernel(
s2 = 1,
D,
nlevels,
xindex,
latentdim,
p_lower = 0,
p_upper = 1,
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 X to use the kernel on
- latentdim
Dimension of embedding space
- 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? Much faster.
- 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.
Details
Used for factor variables, a single dimension. Each level of the factor gets mapped into a latent space, then the distances in that space determine their correlations.
Super class
GauPro::GauPro_kernel -> GauPro_kernel_LatentFactorKernel
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
latentdimDimension of embedding space
pf_to_p_logLogical vector used to convert pf to p
p_to_pf_indsVector of indexes used to convert p to pf
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
LatentFactorKernel$new(
s2 = 1,
D,
nlevels,
xindex,
latentdim,
p_lower = 0,
p_upper = 1,
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
latentdimDimension of embedding space
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.
Method kone()
Find covariance of two points
Arguments
xvector
yvector
pfcorrelation parameters on regular scale, includes zeroes for first level.
s2Variance parameter
isdiagIs this on the diagonal of the covariance?
offdiagequalWhat should offdiagonal values be set to when the indices are the same? Use to avoid decomposition errors, similar to adding a nugget.
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
# Create a new kernel for a single factor with 5 levels,
# mapped into two latent dimensions.
kk <- LatentFactorKernel$new(D=1, nlevels=5, xindex=1, latentdim=2)
# Random initial parameter values
kk$p
#> [1] -1.10716482 1.54756693 -0.97683035 -0.10150345 0.04265025 -1.59671801
#> [7] 0.49096737
# Plots to understand
kk$plotLatent()
kk$plot()
# 5 levels, 1/4 are similar and 2/3/5 are similar
n <- 30
x <- matrix(sample(1:5, n, TRUE))
y <- c(ifelse(x == 1 | x == 4, 4, -3) + rnorm(n,0,.1))
plot(c(x), y)
m5 <- GauPro_kernel_model$new(
X=x, Z=y,
kernel=LatentFactorKernel$new(D=1, nlevels = 5, xindex = 1, latentdim = 2))
m5$kernel$p
#> [1] -3.3692956 1.0153404 -0.8341097 -4.8453431 -1.8087930 1.0058936 -0.8258971
# We should see 1/4 and 2/3/4 in separate clusters
m5$kernel$plotLatent()
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 <- LatentFactorKernel$new(D=2, nlevels=3, xind=2, latentdim=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=1)
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] -1.36611193 -0.42410226 0.23680366 -2.40779069 -0.79777210 -0.03470276
#> [7] 0.13227297
#>
#> $value
#> [1] 4858.984
#>
#> Restart (parallel): starts pars = -1.366112 -0.4241023 0.2368037 -2.407791 -0.7977721 -0.03470276 0.132273
#> Restart (parallel): starts pars = -0.9722268 -1.718136 0.8037575 -1.566855 -1.904968 1.149182 -0.572186
#> start
#> 1 -1.36611193132898,-0.424102260144812,0.236803663745558,-2.40779069401454,-0.79777210198522,-0.0347027620353388,0.132272967942691
#> 2 -1.37,-0.424,0.237,-2.41,-0.798,-0.0347,0.132
#> 3 -0.972,-1.72,0.804,-1.57,-1.9,1.15,-0.572
#> end value func_evals grad_evals
#> 1 NA 4858.984 1 NA
#> 2 -0.777,-1.49,0.57,-1.19,-3.42,-3.45,-2 -19.46035 38 38
#> 3 -1.18,-1.56,0.574,-0.894,-1.56,-1.8e-05,-2 -21.12552 37 37
#> convergence message
#> 1 NA NA
#> 2 0 CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH
#> 3 0 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.