Skip to content

Issues with multilevel correlations #207

Open
@mattansb

Description

@mattansb
library(correlation)

Works:

mtcars |>
  transform(gear = factor(gear)) |>
  correlation(select = c("gear", "hp", "mpg"), multilevel = TRUE)
#> Parameter1 | Parameter2 |     r |             CI | t(30) |         p
#> --------------------------------------------------------------------
#> hp         |        mpg | -0.75 | [-0.87, -0.55] | -6.24 | < .001***
#> 
#> Observations: 32

Fails:

mtcars |>
  transform(gear = factor(gear)) |>
  correlation(select = c("gear", "hp"), select2 = c("mpg"), multilevel = TRUE)
#> Warning in sqrt(1/diag(V)): NaNs produced
#> Warning in stats::cov2cor(inverted): diag(.) had 0 or NA entries; non-finite
#> result is doubtful
#> Parameter1 | Parameter2 |    r |           CI | t(30) |         p
#> -----------------------------------------------------------------
#> hp         |        mpg | 1.00 | [1.00, 1.00] |   Inf | < .001***
#> 
#> Observations: 32

Method column is not updated:

(r <- mtcars |>
    transform(gear = factor(gear)) |>
    correlation(select = c("gear", "hp", "mpg"), multilevel = TRUE, method = "spearman"))
#> Parameter1 | Parameter2 |     r |             CI | t(30) |         p
#> --------------------------------------------------------------------
#> hp         |        mpg | -0.87 | [-0.93, -0.75] | -9.64 | < .001***
#> 
#> Observations: 32
r$Method # Why?
#> [1] "Pearson"

Group by fails:

mtcars |>
  transform(gear = factor(gear)) |>
  dplyr::group_by(am) |>
  correlation(select = c("gear", "hp", "mpg"), multilevel = TRUE)
#> Warning: non-unique values when setting 'row.names': 'hp', 'mpg'
#> Error in `.rowNamesDF<-`(x, value = value): duplicate 'row.names' are not allowed

Created on 2021-08-03 by the reprex package (v2.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions