aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
97 stars 6 forks source link

quantile regression with method 'rqss' failure #66

Closed aphalo closed 4 months ago

aphalo commented 4 months ago

The problem in 'ggpmisc' affects three statistics and one in 'ggplot2'. I see also the problem in 'quantreg'. I doubt this is a general problem as errors triggered in the documnetation of a 'quantreg' function would not pass CRAN checks... The reprex here is for 'quantreg', these are the examples copied from help(rqss).

The problem seems to be in 'quantreg' or 'SparseM', but maybe only under Windows.

library(quantreg)
#> Loading required package: SparseM
#> 
#> Attaching package: 'SparseM'
#> The following object is masked from 'package:base':
#> 
#>     backsolve

n <- 200
x <- sort(rchisq(n,4))
z <- x + rnorm(n)
y <- log(x)+ .1*(log(x))^2 + log(x)*rnorm(n)/4 + z
plot(x, y-z)


f.N  <- rqss(y ~ qss(x, constraint= "N") + z)
#> Error in validObject(.Object): invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class

f.I  <- rqss(y ~ qss(x, constraint= "I") + z)
#> Error in validObject(.Object): invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class

f.CI <- rqss(y ~ qss(x, constraint= "CI") + z)
#> Error in validObject(.Object): invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class

lines(x[-1], f.N $coef[1] + f.N $coef[-(1:2)])
#> Error in eval(expr, envir, enclos): object 'f.N' not found

lines(x[-1], f.I $coef[1] + f.I $coef[-(1:2)], col="blue")
#> Error in eval(expr, envir, enclos): object 'f.I' not found

lines(x[-1], f.CI$coef[1] + f.CI$coef[-(1:2)], col="red")
#> Error in eval(expr, envir, enclos): object 'f.CI' not found

## A bivariate example
if(requireNamespace("interp")){
  if(requireNamespace("interp")){
    data(CobarOre)
    fCO <- rqss(z ~ qss(cbind(x,y), lambda= .08), data=CobarOre)
    plot(fCO)
  }}
#> Loading required namespace: interp
#> Error in validObject(.Object): invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class

sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_Finland.utf8  LC_CTYPE=English_Finland.utf8   
#> [3] LC_MONETARY=English_Finland.utf8 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Finland.utf8    
#> 
#> time zone: Europe/Helsinki
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] quantreg_5.98 SparseM_1.83 
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5        cli_3.6.2          knitr_1.47         rlang_1.1.3       
#>  [5] xfun_0.44          purrr_1.0.2        styler_1.10.3      MatrixModels_0.5-3
#>  [9] glue_1.7.0         htmltools_0.5.8.1  rmarkdown_2.27     R.cache_0.16.0    
#> [13] grid_4.3.3         evaluate_0.23      MASS_7.3-60.0.1    fastmap_1.2.0     
#> [17] interp_1.1-6       yaml_2.3.8         lifecycle_1.0.4    compiler_4.3.3    
#> [21] fs_1.6.4           Rcpp_1.0.12        rstudioapi_0.16.0  R.oo_1.26.0       
#> [25] R.utils_2.12.3     lattice_0.22-5     digest_0.6.35      reprex_2.1.0      
#> [29] splines_4.3.3      magrittr_2.0.3     R.methodsS3_1.8.2  Matrix_1.6-5      
#> [33] tools_4.3.3        withr_3.0.0        deldir_2.0-4       survival_3.6-4

Created on 2024-06-01 with reprex v2.1.0

aphalo commented 4 months ago

This how I see the problem in 'ggpmisc'. The errors are converted into warning but no output is generated by the layer function.

ibrary(ggplot2)
#> Error in ibrary(ggplot2): could not find function "ibrary"
library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

p <-
  ggplot(mpg, aes(displ, 1 / hwy)) +
  geom_point()
p + stat_quant_line()


p + stat_quant_line(method = "rqss")
#> Smoothing formula not specified. Using: y ~ qss(x)
#> Warning: Computation failed in `stat_quant_line()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


p + stat_quant_line(method = "rqss", lambda = 0.1)
#> Smoothing formula not specified. Using: y ~ qss(x)
#> Warning: Computation failed in `stat_quant_line()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


p + stat_quant_band()


p + stat_quant_band(method = "rqss")
#> Warning: Computation failed in `stat_quant_band()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


p + stat_quant_band(method = "rqss", lambda = 0.1)
#> Warning: Computation failed in `stat_quant_band()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


p + stat_quant_eq()
#> Warning in rq.fit.br(x, y, tau = tau, ci = TRUE, ...): Solution may be
#> nonunique


p + stat_quant_eq(method = "rqss")
#> Warning in (function (formula, tau = 0.5, data = parent.frame(), weights, :
#> multiple taus not supported, using first element
#> Warning: Computation failed in `stat_quant_eq()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


p + stat_quant_eq(method = "rqss", lambda = 0.1)
#> Warning in stat_quant_eq(method = "rqss", lambda = 0.1): Ignoring unknown
#> parameters: `lambda`
#> Warning in (function (formula, tau = 0.5, data = parent.frame(), weights, :
#> multiple taus not supported, using first element
#> Warning: Computation failed in `stat_quant_eq()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_Finland.utf8  LC_CTYPE=English_Finland.utf8   
#> [3] LC_MONETARY=English_Finland.utf8 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Finland.utf8    
#> 
#> time zone: Europe/Helsinki
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggpmisc_0.5.6.9003 ggpp_0.5.7         ggplot2_3.5.1     
#> 
#> loaded via a namespace (and not attached):
#>  [1] Matrix_1.6-5       gtable_0.3.5       dplyr_1.1.4        compiler_4.3.3    
#>  [5] reprex_2.1.0       tidyselect_1.2.1   MatrixModels_0.5-3 splines_4.3.3     
#>  [9] scales_1.3.0       yaml_2.3.8         fastmap_1.2.0      lattice_0.22-5    
#> [13] R6_2.5.1           labeling_0.4.3     generics_0.1.3     knitr_1.47        
#> [17] MASS_7.3-60.0.1    tibble_3.2.1       munsell_0.5.1      R.cache_0.16.0    
#> [21] pillar_1.9.0       R.utils_2.12.3     rlang_1.1.3        utf8_1.2.4        
#> [25] xfun_0.44          fs_1.6.4           polynom_1.4-1      cli_3.6.2         
#> [29] withr_3.0.0        magrittr_2.0.3     digest_0.6.35      grid_4.3.3        
#> [33] rstudioapi_0.16.0  quantreg_5.98      lifecycle_1.0.4    R.methodsS3_1.8.2 
#> [37] R.oo_1.26.0        vctrs_0.6.5        SparseM_1.83       evaluate_0.23     
#> [41] glue_1.7.0         farver_2.1.2       styler_1.10.3      survival_3.6-4    
#> [45] fansi_1.0.6        colorspace_2.1-0   rmarkdown_2.27     purrr_1.0.2       
#> [49] tools_4.3.3        pkgconfig_2.0.3    htmltools_0.5.8.1

Created on 2024-06-01 with reprex v2.1.0

aphalo commented 4 months ago

And this is how I see it in 'ggplot2'. Using examples taken from help(stat_quantile). The errors are converted into warning but no output is generated by the layer function.

library(ggplot2)

m <-
  ggplot(mpg, aes(displ, 1 / hwy)) +
  geom_point()
m + stat_quantile()
#> Smoothing formula not specified. Using: y ~ x


m + stat_quantile(method = "rqss")
#> Smoothing formula not specified. Using: y ~ qss(x, lambda = 1)
#> Warning: Computation failed in `stat_quantile()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


m + stat_quantile(method = "rqss", lambda = 0.1)
#> Smoothing formula not specified. Using: y ~ qss(x, lambda = 0.1)
#> Warning: Computation failed in `stat_quantile()`.
#> Caused by error in `validObject()`:
#> ! invalid class "dsparseModelMatrix" object: superclass "xMatrix" not defined in the environment of the object's class


sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_Finland.utf8  LC_CTYPE=English_Finland.utf8   
#> [3] LC_MONETARY=English_Finland.utf8 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Finland.utf8    
#> 
#> time zone: Europe/Helsinki
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.5.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] Matrix_1.6-5       gtable_0.3.5       dplyr_1.1.4        compiler_4.3.3    
#>  [5] reprex_2.1.0       tidyselect_1.2.1   MatrixModels_0.5-3 splines_4.3.3     
#>  [9] scales_1.3.0       yaml_2.3.8         fastmap_1.2.0      lattice_0.22-5    
#> [13] R6_2.5.1           labeling_0.4.3     generics_0.1.3     knitr_1.47        
#> [17] MASS_7.3-60.0.1    tibble_3.2.1       munsell_0.5.1      R.cache_0.16.0    
#> [21] pillar_1.9.0       R.utils_2.12.3     rlang_1.1.3        utf8_1.2.4        
#> [25] xfun_0.44          fs_1.6.4           cli_3.6.2          withr_3.0.0       
#> [29] magrittr_2.0.3     digest_0.6.35      grid_4.3.3         rstudioapi_0.16.0 
#> [33] quantreg_5.98      lifecycle_1.0.4    R.methodsS3_1.8.2  R.oo_1.26.0       
#> [37] vctrs_0.6.5        SparseM_1.83       evaluate_0.23      glue_1.7.0        
#> [41] farver_2.1.2       styler_1.10.3      survival_3.6-4     fansi_1.0.6       
#> [45] colorspace_2.1-0   rmarkdown_2.27     purrr_1.0.2        tools_4.3.3       
#> [49] pkgconfig_2.0.3    htmltools_0.5.8.1

Created on 2024-06-01 with reprex v2.1.0

aphalo commented 4 months ago

This is not a bug in any of the packages. 'MatrixModels’ needs to be reinstalled even if the current version is already installed.