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

as_matrix doesn't return the exact diagonal of a cordf object #52

Closed
cimentadaj opened this issue Mar 6, 2018 · 7 comments
Closed

as_matrix doesn't return the exact diagonal of a cordf object #52

cimentadaj opened this issue Mar 6, 2018 · 7 comments

Comments

@cimentadaj
Copy link
Contributor

cimentadaj commented Mar 6, 2018

With the development version of corrr

library(corrr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

x <- correlate(mtcars, diagonal = 2)
#> 
#> Correlation method: 'pearson'
#> Missing treated using: 'pairwise.complete.obs'
diag(as_matrix(x))
#>  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
#>    1    1    1    1    1    1    1    1    1    1    1

I understand that as_matrix has a diagonal argument but shouldn't it return the same cor_df? Let me know if this makes sense and I'll work the out the code and send a PR.

@drsimonj
Copy link
Collaborator

Excellent point. My original idea was to convert back state that cor() would have created. Perhaps could leave diagonal as an optional argument. Default to NULL, in which case no change is made. Thoughts?

@cimentadaj
Copy link
Contributor Author

cimentadaj commented Mar 24, 2018

Yeah, I think the correct behaviour would be something like: inherit the object as is but replace the diagonal if it's specified. But considering that as_matrix already inherits the params from as_cordf, to avoid inconsistencies, we can set diagonal to NA as it is already the default value in as_cordf.

So something like

as_matrix <- function(x, diagonal = NA) {
  UseMethod("as_matrix")
}

as_matrix.cor_df <- function(x, diagonal = NA) {
  # Separate rownames
  row_names <- x$rowname
  x %<>% dplyr::select_("-rowname")

  # Replace diagonal if specified
  if (!is.na(diagonal)) diag(x) <- diagonal
  
  # Convert to matrix and set rownames
  class(x) <- "data.frame"
  #x %<>% as.matrix()
  x <- as.matrix(x)
  rownames(x) <- row_names
  x
}

If that makes sense, I can wrap it up in a pull request, also including the minor backward incompatible change in the NEWS.md file.

@drsimonj
Copy link
Collaborator

drsimonj commented Mar 27, 2018

Why replace the diagonal if specified it? E.g., if I run correlate(x, diagonal = 100), wouldn't I want to preserve the 100 diagonal with as_matrix()?

In this case, could leave the argument as optional:

as_matrix.cor_df <- function(x, diagonal) {
    ...
    if (!missing(diagonal)) diag(x) <- diagonal
    ...
}

Thoughts?

Aside, if this does result in backward incompatible change, (1) great idea to add to NEWS.md; (2) we should consider version number jump to 0.3.0 for next CRAN submission.

@cimentadaj
Copy link
Contributor Author

I think in principle both approaches are similar, no? If it is non-NA or if it's non-missing, replace the diagonal. The only difference is the default argument. In any case, I think it's the way to go, because it gives the option to replace the diagonal once again. Ok, if this is alright I'll wrap your suggestions with the NEWS.md and the version bump into a pull request this week.

@drsimonj
Copy link
Collaborator

Yep, go for it. I think you should put yourself as a contributor in the DESCRIPTION file too!

@drsimonj
Copy link
Collaborator

drsimonj commented Apr 2, 2018

See #55

@drsimonj drsimonj closed this as completed Apr 2, 2018
@github-actions
Copy link

github-actions bot commented Mar 7, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants