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

stat_fit_glance() for quantreg:rq() #1

Closed
aphalo opened this issue Jan 15, 2021 · 10 comments
Closed

stat_fit_glance() for quantreg:rq() #1

aphalo opened this issue Jan 15, 2021 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@aphalo
Copy link
Owner

aphalo commented Jan 15, 2021

https://stackoverflow.com/questions/65695409/is-there-a-neat-approach-to-label-a-ggplot-plot-with-the-equation-and-other-stat

This would be usable with ggplot2::geom_quantile()

@aphalo aphalo added the enhancement New feature or request label Jan 15, 2021
@aphalo aphalo self-assigned this Jan 15, 2021
@aphalo aphalo changed the title stat_fit_glance() for quantreg:rq() stat_fit_glance() for quantreg:rq() Jan 15, 2021
@markbneal
Copy link

Even if 'stat_fit_glance' was only compatible with one quantile being calculated (e.g. 'quantiles=0.50'), that would be helpful. I'm guessing that 'rq' outputting multiple quantile results could be difficult to handle neatly, so in a golly call, doing x seperate 'stat_fit_glance' calls for your desired x different quantiles would be a pretty good work around.

@aphalo
Copy link
Owner Author

aphalo commented Jan 27, 2021

For single quantiles this is solved in version 0.3.8. Be aware that for parameter estimates one needs to use stat_fit_tidy() instead of stat_fit_glance(). I leave this issue open as it is surely possible to write a stat_rq_eq() with support for a vector of quantiles. I had not used quantile regression before your message in stackoverflow, but it seems very useful indeed. I added to the vignette a quick and dirty way of defining stat_rq_eq(), but this supports only a single quantile per group.

Please look at the version at this link as I found a bug in this vignette in the version submitted to CRAN.
https://docs.r4photobiology.info/ggpmisc/

@aphalo
Copy link
Owner Author

aphalo commented May 24, 2021

stat_quant_eq() is now implemented with support for a vector of quantiles and most of the same features as stat_poly_eq(). It is not yet merged into the master branch.

@markbneal
Copy link

Hi Pedro,

I tried installing as per your suggestion on StackOverflow.
I had to manually uninstall "vctrs" package as it wouldn't update - probably only related to my computer, but easy to fix.
After most things are updated, I had this failure (below). I'll see if I can resolve, updating my R version to 4.05 looks like it might fix it, I'm currently running 4.03. (I'm not super keen to go to 4.1 just yet). SessionInfo at bottom.

> remotes::install_github("aphalo/ggpp")
Using github PAT from envvar GITHUB_PAT
Downloading GitHub repo aphalo/ggpp@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

1: All                            
2: CRAN packages only             
3: None                           
4: fansi   (0.4.2 -> 0.5.0) [CRAN]
5: stringi (1.5.3 -> 1.6.2) [CRAN]
6: tibble  (3.1.1 -> 3.1.2) [CRAN]

Enter one or more numbers, or an empty line to skip updates:
√  checking for file 'C:\Users\nealm\AppData\Local\Temp\RtmpQhCwin\remotes397c41522fa7\aphalo-ggpp-e2c39e0/DESCRIPTION' (505ms)
-  preparing 'ggpp': (2.2s)
√  checking DESCRIPTION meta-information ... 
-  checking for LF line-endings in source and make files and shell scripts (482ms)
-  checking for empty or unneeded directories
-  building 'ggpp_0.4.0.tar.gz'
   
Installing package into ‘C:/Rpackages’
(as ‘lib’ is unspecified)
* installing *source* package 'ggpp' ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
Error: (converted from warning) package 'ggplot2' was built under R version 4.0.5
Execution halted
ERROR: lazy loading failed for package 'ggpp'
* removing 'C:/Rpackages/ggpp'
Error: Failed to install 'ggpp' from GitHub:
  (converted from warning) installation of package ‘C:/Users/nealm/AppData/Local/Temp/RtmpQhCwin/file397c63dc9cd/ggpp_0.4.0.tar.gz’ had non-zero exit status
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252   
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C                        
[5] LC_TIME=English_New Zealand.1252    

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

loaded via a namespace (and not attached):
 [1] processx_3.4.5    compiler_4.0.3    R6_2.5.0          rprojroot_2.0.2   cli_2.5.0        
 [6] prettyunits_1.1.1 tools_4.0.3       withr_2.4.2       rstudioapi_0.13   curl_4.3         
[11] crayon_1.4.1      remotes_2.2.0     callr_3.5.1       pkgbuild_1.1.0    ps_1.4.0         

@markbneal
Copy link

Here are the resulting messages from install - some Rd warnings (?) specific to my computer?

remotes::install_github("aphalo/ggpp")
Using github PAT from envvar GITHUB_PAT
Downloading GitHub repo aphalo/ggpp@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

1: All                            
2: CRAN packages only             
3: None                           
4: fansi   (0.4.2 -> 0.5.0) [CRAN]
5: stringi (1.5.3 -> 1.6.2) [CRAN]
6: tibble  (3.1.1 -> 3.1.2) [CRAN]

Enter one or more numbers, or an empty line to skip updates:
√  checking for file 'C:\Users\nealm\AppData\Local\Temp\Rtmp4cDMe8\remotes4e38614275a\aphalo-ggpp-e2c39e0/DESCRIPTION' (973ms)
-  preparing 'ggpp': (1.8s)
√  checking DESCRIPTION meta-information ... 
-  checking for LF line-endings in source and make files and shell scripts (344ms)
-  checking for empty or unneeded directories
-  building 'ggpp_0.4.0.tar.gz'
   
Installing package into ‘C:/Rpackages’
(as ‘lib’ is unspecified)
* installing *source* package 'ggpp' ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package 'ggpp'
    finding HTML links ... done
    annotate                                html  
    compute_npcx                            html  
    geom_grob                               html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoTv5c/R.INSTALL3140692f13e/ggpp/man/geom_grob.Rd:62: file link 'geom_label' in package 'ggplot2' does not exist and so has been treated as a topic
    geom_plot                               html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoTv5c/R.INSTALL3140692f13e/ggpp/man/geom_plot.Rd:63: file link 'geom_label' in package 'ggplot2' does not exist and so has been treated as a topic
    geom_quadrant_lines                     html  
    geom_table                              html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoTv5c/R.INSTALL3140692f13e/ggpp/man/geom_table.Rd:83: file link 'geom_label' in package 'ggplot2' does not exist and so has been treated as a topic
    geom_text_linked                        html  
    geom_text_npc                           html  
    geom_x_margin_arrow                     html  
    geom_x_margin_grob                      html  
    geom_x_margin_point                     html  
    ggplot                                  html  
    ggpp-ggproto                            html  
    ggpp-package                            html  
    position_nudge_center                   html  
    position_nudge_line                     html  
    position_nudge_to                       html  
    scale_continuous_npc                    html  
    stat_apply                              html  
    stat_dens1d_filter                      html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoTv5c/R.INSTALL3140692f13e/ggpp/man/stat_dens1d_filter.Rd:81: file link 'bw.nrd' in package 'stats' does not exist and so has been treated as a topic
    stat_dens1d_labels                      html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoTv5c/R.INSTALL3140692f13e/ggpp/man/stat_dens1d_labels.Rd:61: file link 'bw.nrd' in package 'stats' does not exist and so has been treated as a topic
    stat_dens2d_filter                      html  
    stat_dens2d_labels                      html  
    stat_fmt_tb                             html  
    stat_quadrant_counts                    html  
    try_data_frame                          html  
    ttheme_gtdefault                        html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoTv5c/R.INSTALL3140692f13e/ggpp/man/ttheme_gtdefault.Rd:116: file link 'grid.table' in package 'gridExtra' does not exist and so has been treated as a topic
    ttheme_set                              html  
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
*** arch - i386
*** arch - x64
** testing if installed package can be loaded from final location
*** arch - i386
*** arch - x64
** testing if installed package keeps a record of temporary installation path
* DONE (ggpp)

Installing ggpmisc and selecting update all caused an issue with "fansi" not installing (similar to "vctrs" problem above). Resolved with deletion of "fansi", restart R and repeat.

package ‘fansi’ successfully unpacked and MD5 sums checked
Error: Failed to install 'ggpmisc' from GitHub:
  (converted from warning) cannot remove prior installation of package ‘fansi’
> remove.packages("fansi")
Removing package from ‘C:/Rpackages’
(as ‘lib’ is unspecified)
Error in remove.packages : there is no package called ‘fansi’

On install, an Rd warning here too - may be of no importance.

Restarting R session...

> remotes::install_github("aphalo/ggpmisc")
Using github PAT from envvar GITHUB_PAT
Downloading GitHub repo aphalo/ggpmisc@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

 1: All                                           
 2: CRAN packages only                            
 3: None                                          
 4: RcppArmad... (0.10.1.2.0 -> 0.10.5.0.0) [CRAN]
 5: matrixStats  (0.57.0     -> 0.58.0    ) [CRAN]
 6: stringi      (1.5.3      -> 1.6.2     ) [CRAN]
 7: tibble       (3.1.1      -> 3.1.2     ) [CRAN]
 8: MatrixModels (0.4-1      -> 0.5-0     ) [CRAN]
 9: SparseM      (1.78       -> 1.81      ) [CRAN]
10: splus2R      (1.2-2      -> 1.3-3     ) [CRAN]
11: quantreg     (5.75       -> 5.85      ) [CRAN]

Enter one or more numbers, or an empty line to skip updates:1
fansi        (NA         -> 0.5.0     ) [CRAN]
RcppArmad... (0.10.1.2.0 -> 0.10.5.0.0) [CRAN]
matrixStats  (0.57.0     -> 0.58.0    ) [CRAN]
stringi      (1.5.3      -> 1.6.2     ) [CRAN]
tibble       (3.1.1      -> 3.1.2     ) [CRAN]
MatrixModels (0.4-1      -> 0.5-0     ) [CRAN]
SparseM      (1.78       -> 1.81      ) [CRAN]
splus2R      (1.2-2      -> 1.3-3     ) [CRAN]
quantreg     (5.75       -> 5.85      ) [CRAN]
Installing 9 packages: fansi, RcppArmadillo, matrixStats, stringi, tibble, MatrixModels, SparseM, splus2R, quantreg
Installing packages into ‘C:/Rpackages’
(as ‘lib’ is unspecified)

  There are binary versions available but the source versions are later:
                  binary     source needs_compilation
fansi              0.4.2      0.5.0              TRUE
RcppArmadillo 0.10.4.0.0 0.10.5.0.0              TRUE
stringi            1.5.3      1.6.2              TRUE
tibble             3.1.1      3.1.2              TRUE

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/fansi_0.4.2.zip'
Content type 'application/zip' length 224064 bytes (218 KB)
downloaded 218 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/RcppArmadillo_0.10.4.0.0.zip'
Content type 'application/zip' length 2682848 bytes (2.6 MB)
downloaded 2.6 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/matrixStats_0.58.0.zip'
Content type 'application/zip' length 1589957 bytes (1.5 MB)
downloaded 1.5 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/stringi_1.5.3.zip'
Content type 'application/zip' length 15243599 bytes (14.5 MB)
downloaded 14.5 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/tibble_3.1.1.zip'
Content type 'application/zip' length 825914 bytes (806 KB)
downloaded 806 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/MatrixModels_0.5-0.zip'
Content type 'application/zip' length 433423 bytes (423 KB)
downloaded 423 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/SparseM_1.81.zip'
Content type 'application/zip' length 1066248 bytes (1.0 MB)
downloaded 1.0 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/splus2R_1.3-3.zip'
Content type 'application/zip' length 337723 bytes (329 KB)
downloaded 329 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/quantreg_5.85.zip'
Content type 'application/zip' length 1938021 bytes (1.8 MB)
downloaded 1.8 MB

package ‘fansi’ successfully unpacked and MD5 sums checked
package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
package ‘matrixStats’ successfully unpacked and MD5 sums checked
package ‘stringi’ successfully unpacked and MD5 sums checked
package ‘tibble’ successfully unpacked and MD5 sums checked
package ‘MatrixModels’ successfully unpacked and MD5 sums checked
package ‘SparseM’ successfully unpacked and MD5 sums checked
package ‘splus2R’ successfully unpacked and MD5 sums checked
package ‘quantreg’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\nealm\AppData\Local\Temp\RtmpaSePVg\downloaded_packages
√  checking for file 'C:\Users\nealm\AppData\Local\Temp\RtmpaSePVg\remotes33a0277e53f\aphalo-ggpmisc-4fd1dbf/DESCRIPTION' (484ms)
-  preparing 'ggpmisc': (2s)
√  checking DESCRIPTION meta-information ... 
-  checking for LF line-endings in source and make files and shell scripts (357ms)
-  checking for empty or unneeded directories
-  building 'ggpmisc_0.4.0.tar.gz'
   
Installing package into ‘C:/Rpackages’
(as ‘lib’ is unspecified)
* installing *source* package 'ggpmisc' ...
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package 'ggpmisc'
    finding HTML links ... done
    FC_format                               html  
    FC_name                                 html  
    find_peaks                              html  
    ggpmisc-ggproto                         html  
    ggpmisc-package                         html  
    moved-to-gginnards                      html  
    outcome2factor                          html  
    quadrant_example.df                     html  
    reverselog_trans                        html  
    scale_colour_outcome                    html  
    scale_shape_outcome                     html  
    scale_x_logFC                           html  
    scale_y_Pvalue                          html  
    stat_fit_augment                        html  
    stat_fit_deviations                     html  
    stat_fit_glance                         html  
    stat_fit_residuals                      html  
    stat_fit_tb                             html  
    finding level-2 HTML links ... done

    stat_fit_tidy                           html  
    stat_peaks                              html  
Rd warning: C:/Users/nealm/AppData/Local/Temp/RtmpaoHRnV/R.INSTALL3380766b74f/ggpmisc/man/stat_peaks.Rd:122: file link 'geom_label_repel' in package 'ggrepel' does not exist and so has been treated as a topic
    stat_poly_eq                            html  
    stat_quant_eq                           html  
    symmetric_limits                        html  
    volcano_example.df                      html  
    xy_outcomes2factor                      html  
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
*** arch - i386
*** arch - x64
** testing if installed package can be loaded from final location
*** arch - i386
*** arch - x64
** testing if installed package keeps a record of temporary installation path
* DONE (ggpmisc)

@markbneal
Copy link

Hi Pedro,

I've run the code from your documentation, and resulting graphs look really good to me!

Documentation also looks good, though for stat_quant_eq {ggpmisc} an unneeded comma gives a visual warning in Rstudio IDE "missing argument to function call" - it doesn't stop the code working fine for me though.

# user specified label
ggplot(my.data, aes(x, y, color = group, , grp.label = group)) +   #Unneeded comma
  geom_point() +
  geom_quantile(method = "rq", formula = formula,
                quantiles = c(0.05, 0.5, 0.95)) +
  stat_quant_eq(aes(label = paste(stat(grp.label), "*\": \"*",
                                   stat(eq.label), sep = "")),
                quantiles = c(0.05, 0.5, 0.95),
                formula = formula, parse = TRUE)

Do you think the documentation should read quantiles = c("0.25, 0.5, 0.75)" since that is the default?

stat_quant_eq(
  mapping = NULL,
  data = NULL,
  geom = "text_npc",
  position = "identity",
  ...,
  formula = NULL,
  quantiles = 0.5, #alter to list c(...)?

I note that documentation for geom_quantile {ggplot2} has the default for stat_quantile() as stat_quantile = c(0.25, 0.5, 0.75).

@markbneal
Copy link

I've run some code with some of my data. It looks to be working quite well, but I found this edge case, where when I split into groups, The quantiles will plot, but the equations do not. appear in plot 2. Any ideas? I can pass data to you privately if need be.

# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)

# no weights, custom data, no group and colour are
ggplot(db, aes(x=my.x, y=my.y))+
  geom_point() +
  geom_quantile(method = "rq", formula = formula) +
  stat_quant_eq(formula = formula, parse = TRUE, 
                quantiles = c(0.25, 0.5, 0.75))

# no weights, custom data, group and colour are "Category"
ggplot(db, aes(x=my.x, y=my.y, group = Category, colour = Category))+
  geom_point() +
  geom_quantile(method = "rq", formula = formula) +
  stat_quant_eq(formula = formula, parse = TRUE, 
                quantiles = c(0.25, 0.5, 0.75))

Error message for plot 2

Computation failed in `stat_quant_eq()`:
error in evaluating the argument 'x' in selecting a method for function 'diag': system is computationally singular: reciprocal condition number = 2.17592e-20 

plot1
plot2

@aphalo
Copy link
Owner Author

aphalo commented May 26, 2021

Many thanks!
I will go through these before submission. Yesterday when trying to update 'fansi' I run into the same problem... seems to be a package used by 'ggplot2' to handle ascii control characters. It might be that RStudio loads.
I will add a way to pass additional arguments to qr() similarly as in geom_quantile().

It is curious that quantile regression seems to be more frequently used in economics than in biology. It seems to me a very useful tools whenever there are outliers.

@aphalo
Copy link
Owner Author

aphalo commented Jun 3, 2021

@markbneal I will submit 'ggpmisc' to CRAN today. I smoothed some rough edges but surely improvements will be possible. I now close this issue but if you find that something is not working please reopen this issue or create a new one.

Once again, thanks!

@aphalo aphalo closed this as completed Jun 3, 2021
@markbneal
Copy link

markbneal commented Jun 14, 2021

It is curious that quantile regression seems to be more frequently used in economics than in biology. It seems to me a very useful tools whenever there are outliers.

Yes it is! Here's a plot I've used, showing how economies of size differ (or not) based on quantiles (i.e the quantile represents cost-efficient performance conditional on size).

image

aphalo added a commit that referenced this issue Jul 2, 2021
Fix GH-1 Add comment stating that list columns in data returned by stats like stat_poly_eq() do not work together with facets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants