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

Question about reverse of stretch() #65

Closed
imran1570 opened this issue Aug 22, 2018 · 5 comments
Closed

Question about reverse of stretch() #65

imran1570 opened this issue Aug 22, 2018 · 5 comments

Comments

@imran1570
Copy link

I was wondering if there is a function in this package which can convert the long format of correlation data to a square correlation matrix?
In other words, the inverse/opposite of stretch().
Thanks.

@drsimonj
Copy link
Collaborator

No, and this would require some work to implement. It would be something along the lines of tidyr::spread(long_dat, y, r), but with issues about checking compliant structure, column names (x or rowname), and ordering of columns and rows.

Anither thing to consider is whether you are intending to apply it to a long format structure created without corrr, or a long data frame created with stretch().

Either way, can you give an example of when you would use such a function?

@lnalborczyk
Copy link

lnalborczyk commented Mar 15, 2019

Hi,

thank you for writing this super useful package !

I also have the same issue/question. I have a tibble with three columns containing my first variable, my second variable, and their estimated correlation (estimate coming from a brmsfit model).

For instance, let's consider the following tibble:

tibble::tribble(
                                              ~a,    ~b,                     ~est,
                                           "fro", "fcr",   "0.061 [-0.276, 0.38]",
                                           "fro", "nek",  "0.115 [-0.218, 0.421]",
                                           "nek", "fcr",  "0.154 [-0.169, 0.476]",
                                           "ooi", "fcr", "-0.022 [-0.352, 0.305]",
                                           "ooi", "fro",  "0.006 [-0.326, 0.323]",
                                           "ooi", "nek",  "0.173 [-0.151, 0.486]",
                                           "ooi", "zyg",   "0.039 [-0.29, 0.362]",
                                           "zyg", "fcr",  "-0.02 [-0.347, 0.317]",
                                           "zyg", "fro",  "0.092 [-0.245, 0.406]",
                                           "zyg", "nek",   "0.315 [0.011, 0.613]"
                                           )

I would like to be able to reshape this in a correlation matrix using the somehow reverse action of corrr:stretch(). Any idea ?

Ladislas

@edgararuiz-zz
Copy link
Collaborator

Added a new retract() function which, I think does what you've requested:

corrr::retract(
  tibble::tribble(
    ~x,    ~y,     ~r,
    "fro", "ooi",   0.061,
    "fro", "nek",  0.115,
    "nek", "ooi",  0.154,
    "ooi", "ooi",  -0.022,
    "ooi", "fro",  0.006,
    "ooi", "nek",  0.173,
    "ooi", "zyg",  0.039,
    "zyg", "ooi",  -0.02,
    "zyg", "fro",  0.092,
    "zyg", "nek",  0.315
  )
)
#> # A tibble: 4 x 5
#>   rowname    ooi    fro    nek    zyg
#>   <chr>    <dbl>  <dbl>  <dbl>  <dbl>
#> 1 ooi     -0.022  0.006  0.173  0.039
#> 2 nek      0.154 NA     NA     NA    
#> 3 fro      0.061 NA      0.115 NA    
#> 4 zyg     -0.02   0.092  0.315 NA

@lnalborczyk
Copy link

Thank you for the follow-up!

I am not sure it does the inverse operation of stretch() however... do you know what are these NA in the resulting tibble? Should not the NA be on the diagonal? Plus, the correlation between X and Y is not the same as the correlation between Y and X...

@github-actions
Copy link

github-actions bot commented Mar 6, 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 6, 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

4 participants