Skip to content

Error message when rotation matrix missing #7

Open

Description

@skiaphrene

Dear ggbiplot devs,

Currently, when performing a PCA with stats::prcomp() via a call to recipes::step_pca() (code snippet below), the following call to ggbiplot (v0.6.5) fails with an obscure error:

Error in sqrt(n) : non-numeric argument to mathematical function
3.sweep(pcobj$x, 2, 1/(D * sqrt(n)), FUN = "*")
2.get_SVD(pcobj)
1.ggbiplot(prcomp_obj, groups = iris$Species, choices = c(1, 2),
alpha = 0.8, varname.size = 3, varname.color = "black", var.factor = 0.5)

This is resolved by requesting retx=TRUE.

I would assume that this is also the case via a standard prcomp() call.

It would be more helpful if ggbiplot picked up on the missing rotation matrix and threw a more informative error.

Thank you for your time!

Best regards,

-- Alex

Snippet:

data(iris)
library(tidymodels)
library(ggbiplot)


## test 1
iris_recipe = recipe( iris , Species ~ . ) %>%
    step_normalize( all_numeric_predictors() ) %>%
    step_pca( all_numeric_predictors() , options = list(retx = FALSE) ) %>%
    prep()

iris_pca = iris_recipe %>% bake( iris )

prcomp_obj = iris_recipe$steps[[2]]$res

ggbiplot(prcomp_obj, 
         groups = iris$Species,
         choices = c(1,2),
         alpha = 0.8,
         varname.size = 3,
         varname.color = 'black',
         var.factor = .5
         ) # => fails


## test 2
iris_recipe = recipe( iris , Species ~ . ) %>%
    step_normalize( all_numeric_predictors() ) %>%
    step_pca( all_numeric_predictors() , options = list(retx = TRUE) ) %>%
    prep()

iris_pca = iris_recipe %>% bake( iris )

prcomp_obj = iris_recipe$steps[[2]]$res

ggbiplot(prcomp_obj, 
         groups = iris$Species,
         choices = c(1,2),
         alpha = 0.8,
         varname.size = 3,
         varname.color = 'black',
         var.factor = .5
         ) # => success

sessionInfo()
R version 4.6.2 (2026-10-29)
Platform: x86_64-apple-darwin20
Running under: macOS Tahoe 26.6.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.6-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1

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

time zone: Europe/Zurich
tzcode source: internal

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

other attached packages:
[1] ggbiplot_0.6.5 yardstick_1.4.0 workflowsets_1.1.1 workflows_1.3.0 tune_2.1.0 tidyr_1.3.2 tailor_0.1.0
[8] rsample_1.3.2 recipes_1.4.0 purrr_1.2.2 parsnip_1.6.1 modeldata_1.6.0 infer_1.1.0 ggplot2_4.0.3
[15] dplyr_1.2.1 dials_1.4.4 scales_1.4.0 broom_1.0.13 tidymodels_1.5.0

loaded via a namespace (and not attached):
[1] gtable_0.3.6 xfun_0.62 lattice_0.23-1 vctrs_0.7.3 tools_4.6.2 generics_0.1.4
[7] parallel_4.6.2 tibble_3.3.1 pkgconfig_2.0.3 Matrix_1.7-6 data.table_1.18.6.1 RColorBrewer_1.1-3
[13] S7_0.2.2 lifecycle_1.0.5 compiler_4.6.2 farver_2.1.2 codetools_0.2-20 DiceDesign_1.10
[19] class_7.3-24 prodlim_2026.03.11 pillar_1.11.1 furrr_0.4.0 MASS_7.3-66 gower_1.0.2
[25] rpart_4.1.27 parallelly_1.50.0 lava_1.10 tidyselect_1.2.1 future_1.82.0 listenv_1.1.0
[31] splines_4.6.2 grid_4.6.2 cli_3.6.6 magrittr_2.0.5 survival_3.8-12 future.apply_1.21.0
[37] withr_3.0.3 backports_1.5.1 lubridate_1.9.5 timechange_0.4.0 globals_0.20.0 otel_0.2.0
[43] nnet_7.3-21 timeDate_4052.112 evaluate_1.0.5 knitr_1.52 hardhat_1.4.3 rlang_1.3.0
[49] Rcpp_1.1.2 glue_1.8.1 ipred_0.9-16 rstudioapi_0.19.0 R6_2.6.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions