Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kPCA reproducibility #3

Closed
topepo opened this issue Jan 26, 2017 · 4 comments
Closed

kPCA reproducibility #3

topepo opened this issue Jan 26, 2017 · 4 comments

Comments

@topepo
Copy link
Contributor

topepo commented Jan 26, 2017

I'm not able to reproduce the kernel PCA results in comparison to the underlying function. Here's an example:

library(kernlab)
library(dimRed)

set.seed(131)
tr_dat <- matrix(rnorm(100*6), ncol = 6)
te_dat <- matrix(rnorm(20*6), ncol = 6)
colnames(tr_dat) <- paste0("X", 1:6)
colnames(te_dat) <- paste0("X", 1:6)

k_name <- "rbfdot"
k_par <- list(sigma = .2)

## test values

kpca_obj <- kPCA(stdpars = list(ndim = 3, kernel = k_name, kpar = k_par))
kpca_obj <- kpca_obj@fun(dimRedData(tr_dat), kpca_obj@stdpars)
kpca_pred <- kpca_obj@apply(te_dat)@data

## expected values

kpca_obj_exp <- kpca(tr_dat, 
                     kernel = k_name,
                     kpar = k_par)
kpca_pred_exp <- predict(kpca_obj_exp, tr_dat)[, 1:3]
colnames(kpca_pred_exp) <- paste0("kPCA", 1:3)

I get

> head(kpca_pred)
          kPCA1      kPCA2       kPCA3
[1,] -0.1754955 -2.8205993  0.51416167
[2,]  1.1112348  1.7925091 -0.02363246
[3,]  1.9973353 -0.9198911  0.14218226
[4,]  3.0105551  1.4249128 -2.79424169
[5,] -3.2053340 -2.0046749 -0.79662181
[6,]  1.5522026  3.6696689 -2.54760691
> head(kpca_pred_exp)
         kPCA1      kPCA2      kPCA3
[1,]  2.614505  2.9551241  2.1230302
[2,] -1.827209  2.4680460 -2.3203690
[3,]  2.956935 -1.2295952 -2.9909752
[4,] -3.740879 -0.8210545 -4.0988922
[5,] -1.015746 -1.7453619 -0.5225218
[6,] -2.357748  2.1721046 -2.2195350
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.3

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dimRed_0.0.3.9001 DRR_0.0.2         CVST_0.2-1        Matrix_1.2-7.1    kernlab_0.9-25   

loaded via a namespace (and not attached):
[1] tools_3.3.2     grid_3.3.2      lattice_0.20-34

BTW what's the best way to access the objects generated in the fun code from the base object? I'd like to get ahold of the PCA rotation matrix or the kPCA object res. That gets computed once on the first call?

Thanks,

Max

@gdkrmr
Copy link
Owner

gdkrmr commented Jan 27, 2017

you are using tr_dat instead of te_dat in the second line from below, try:

kpca_pred_exp <- predict(kpca_obj_exp, te_dat)[, 1:3]

To get the actual rotation matrix of a pca, you need to look in the environment of its apply function:

emb <- embed(loadDataSet("Iris"), "PCA")
environment(emb@apply)$rot

It even has nice column and row names, for a kPCA object you would have use environment(kpca_obj@apply)$res.

I will make some convenience functions for extracting interesting information from objects.

@gdkrmr
Copy link
Owner

gdkrmr commented Jan 27, 2017

@topepo maybe you can help with some proposals in #4

@topepo
Copy link
Contributor Author

topepo commented Jan 27, 2017

My apologies for that; I hate is when I get issues with bad code =[

@topepo topepo closed this as completed Jan 27, 2017
@gdkrmr
Copy link
Owner

gdkrmr commented Jan 27, 2017

don't worry, happens to me all the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants