Skip to contents

Gower factor Kernel R6 class

Gower factor Kernel R6 class

Usage

k_GowerFactorKernel(
  s2 = 1,
  D,
  nlevels,
  xindex,
  p_lower = 0,
  p_upper = 0.9,
  p_est = TRUE,
  s2_lower = 1e-08,
  s2_upper = 1e+08,
  s2_est = TRUE,
  p,
  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?

p

Vector of correlations

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.

Details

For a factor that has been converted to its indices. Each factor will need a separate kernel.

Super class

GauPro::GauPro_kernel -> GauPro_kernel_GowerFactorKernel

Public fields

p

Parameter for correlation

p_est

Should p be estimated?

p_lower

Lower bound of p

p_upper

Upper bound of p

s2

variance

s2_est

Is s2 estimated?

logs2

Log of s2

logs2_lower

Lower bound of logs2

logs2_upper

Upper bound of logs2

xindex

Index of the factor (which column of X)

nlevels

Number of levels for the factor

offdiagequal

What 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

GowerFactorKernel$new(
  s2 = 1,
  D,
  nlevels,
  xindex,
  p_lower = 0,
  p_upper = 0.9,
  p_est = TRUE,
  s2_lower = 1e-08,
  s2_upper = 1e+08,
  s2_est = TRUE,
  p,
  useC = TRUE,
  offdiagequal = 1 - 1e-06
)

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?

p

Vector of correlations

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.


Method k()

Calculate covariance between two points

Usage

GowerFactorKernel$k(x, y = NULL, p = self$p, s2 = self$s2, params = NULL)

Arguments

x

vector.

y

vector, optional. If excluded, find correlation of x with itself.

p

Correlation parameters.

s2

Variance parameter.

params

parameters to use instead of beta and s2.


Method kone()

Find covariance of two points

Usage

GowerFactorKernel$kone(
  x,
  y,
  p,
  s2,
  isdiag = TRUE,
  offdiagequal = self$offdiagequal
)

Arguments

x

vector

y

vector

p

correlation parameters on regular scale

s2

Variance parameter

isdiag

Is this on the diagonal of the covariance?

offdiagequal

What should offdiagonal values be set to when the indices are the same? Use to avoid decomposition errors, similar to adding a nugget.


Method dC_dparams()

Derivative of covariance with respect to parameters

Usage

GowerFactorKernel$dC_dparams(params = NULL, X, C_nonug, C, nug)

Arguments

params

Kernel parameters

X

matrix of points in rows

C_nonug

Covariance without nugget added to diagonal

C

Covariance with nugget

nug

Value of nugget


Method C_dC_dparams()

Calculate covariance matrix and its derivative with respect to parameters

Usage

GowerFactorKernel$C_dC_dparams(params = NULL, X, nug)

Arguments

params

Kernel parameters

X

matrix of points in rows

nug

Value of nugget


Method dC_dx()

Derivative of covariance with respect to X

Usage

GowerFactorKernel$dC_dx(XX, X, ...)

Arguments

XX

matrix of points

X

matrix of points to take derivative with respect to

...

Additional args, not used


Method param_optim_start()

Starting point for parameters for optimization

Usage

GowerFactorKernel$param_optim_start(
  jitter = F,
  y,
  p_est = self$p_est,
  s2_est = self$s2_est
)

Arguments

jitter

Should there be a jitter?

y

Output

p_est

Is p being estimated?

s2_est

Is s2 being estimated?

alpha_est

Is alpha being estimated?


Method param_optim_start0()

Starting point for parameters for optimization

Usage

GowerFactorKernel$param_optim_start0(
  jitter = F,
  y,
  p_est = self$p_est,
  s2_est = self$s2_est
)

Arguments

jitter

Should there be a jitter?

y

Output

p_est

Is p being estimated?

s2_est

Is s2 being estimated?

alpha_est

Is alpha being estimated?


Method param_optim_lower()

Lower bounds of parameters for optimization

Usage

GowerFactorKernel$param_optim_lower(p_est = self$p_est, s2_est = self$s2_est)

Arguments

p_est

Is p being estimated?

s2_est

Is s2 being estimated?

alpha_est

Is alpha being estimated?


Method param_optim_upper()

Upper bounds of parameters for optimization

Usage

GowerFactorKernel$param_optim_upper(p_est = self$p_est, s2_est = self$s2_est)

Arguments

p_est

Is p being estimated?

s2_est

Is s2 being estimated?

alpha_est

Is alpha being estimated?


Method set_params_from_optim()

Set parameters from optimization output

Usage

GowerFactorKernel$set_params_from_optim(
  optim_out,
  p_est = self$p_est,
  s2_est = self$s2_est
)

Arguments

optim_out

Output from optimization

p_est

Is p being estimated?

s2_est

Is s2 being estimated?

alpha_est

Is alpha being estimated?


Method s2_from_params()

Get s2 from params vector

Usage

GowerFactorKernel$s2_from_params(params, s2_est = self$s2_est)

Arguments

params

parameter vector

s2_est

Is s2 being estimated?


Method print()

Print this object

Usage

GowerFactorKernel$print()


Method clone()

The objects of this class are cloneable with this method.

Usage

GowerFactorKernel$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

kk <- GowerFactorKernel$new(D=1, nlevels=5, xindex=1, p=.2)
kmat <- outer(1:5, 1:5, Vectorize(kk$k))
kmat
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]  1.0  0.2  0.2  0.2  0.2
#> [2,]  0.2  1.0  0.2  0.2  0.2
#> [3,]  0.2  0.2  1.0  0.2  0.2
#> [4,]  0.2  0.2  0.2  1.0  0.2
#> [5,]  0.2  0.2  0.2  0.2  1.0
kk$plot()



# 2D, Gaussian on 1D, index on 2nd dim
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))
n <- nrow(X)
Z <- X[,1] - (X[,2]-1.8)^2 + rnorm(n,0,.1)
tibble(X=X, Z) %>% arrange(X,Z)
k2a <- IgnoreIndsKernel$new(k=Gaussian$new(D=1), ignoreinds = 2)
k2b <- GowerFactorKernel$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.2521832 -0.8657638  0.5825860  0.0000000 -1.8254123
#> 
#> $value
#> [1] 45.38652
#> 
#> 	Restart (parallel): starts pars = -0.2521832 -0.8657638 0.582586 0 -1.825412 
#>                                                                         start
#> 1 -0.252183161390947,-0.865763754844262,0.582585999886443,0,-1.82541226421978
#> 2                                                 -0.252,-0.866,0.583,0,-1.83
#>                          end     value func_evals grad_evals convergence
#> 1                         NA  45.38652          1         NA          NA
#> 2 3.43,-1.12,0.0124,0.585,-2 -45.21535         19         19           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.