Home > Blockchain >  How can I get the residuals of correlation from tidy data
How can I get the residuals of correlation from tidy data

Time:04-07

I have a tidy data.

|ID   | variable | value.x | value.y |
| --- | -------- | ------- | ------- |
|1    | Temp     | -0.71   | -0.74   |
|2    | Temp     | -0.53   | -0.50   |
|3    | Temp     | -0.48   | -0.51   |
|4    | Temp     | -0.65   | -0.66   |
|5    | Temp     | -0.49   | -0.56   |
|6    | Prep     | -0.72   | -0.75   |
|7    | Prep     | -0.64   | -0.65   |
|8    | Prep     | -0.56   | -0.54   |
|9    | Prep     | -0.46   | -0.47   |
|10   | Prep     | -0.44   | -0.44   |

I would like to get the residuals of correlation for each variable.

Thanks in advance,

CodePudding user response:

If you just want to add the residuals from a model to your data table, modelr::add_residuals() is a nice convenience function for this exact purpose. You can use dplyr::group_split() and purrr::map() to simultaneously run this on multiple subsets of your original data. I assumed a simple linear model but you can substitute other standard model objects.

library(tidyverse)
library(modelr)

d <- structure(list(ID = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"), variable = c("Temp", "Temp", "Temp", "Temp", "Temp", "Prep", "Prep", "Prep", "Prep", "Prep"), x = c(-0.71, -0.53, -0.48, -0.65, -0.49, -0.72, -0.64, -0.56, -0.46, -0.44), y = c(-0.74, -0.5, -0.51, -0.66, -0.56, -0.75, -0.65, -0.54, -0.47, -0.44)), row.names = c(NA, -10L), class = "data.frame")

d %>% 
  group_split(variable) %>% 
  map(~add_residuals(data = .x, lm(y~x, data = .x))) %>% 
  bind_rows()
#> # A tibble: 10 x 5
#>    ID    variable     x     y    resid
#>    <chr> <chr>    <dbl> <dbl>    <dbl>
#>  1 6     Prep     -0.72 -0.75 -0.0116 
#>  2 7     Prep     -0.64 -0.65  0.00205
#>  3 8     Prep     -0.56 -0.54  0.0257 
#>  4 9     Prep     -0.46 -0.47 -0.0123 
#>  5 10    Prep     -0.44 -0.44 -0.00386
#>  6 1     Temp     -0.71 -0.74 -0.0156 
#>  7 2     Temp     -0.53 -0.5   0.0543 
#>  8 3     Temp     -0.48 -0.51 -0.00293
#>  9 4     Temp     -0.65 -0.66  0.00770
#> 10 5     Temp     -0.49 -0.56 -0.0435

Created on 2022-04-07 by the reprex package (v2.0.1)

  • Related