Search code examples
rdataframecorrelation

Why R cor() function doesn't work on this data


I have the following data set:

library(tidyverse)
dat <- structure(list(truth.agg = c(5, 4, 4, 8, 3, 2, 3, 8, 6, 8, 9, 
8, 7, 6, 8, 8, 8, 8, 8, 9, 8, 8, 9, 8, 8, 8, 5, 7, 9, 5), .pred = c(4.73240566253662, 
4.73240566253662, 4.73240566253662, 5.15031337738037, 2.28486394882202, 
4.60455703735352, 4.60455703735352, 8.46518325805664, 5.62833499908447, 
7.93664407730103, 7.9920597076416, 8.2073917388916, 7.03494453430176, 
6.60951709747314, 7.9366512298584, 8.44983863830566, 7.17515993118286, 
7.9701943397522, 7.94902610778809, 8.11329460144043, 7.66316652297974, 
7.51529359817505, 8.374342918396, 8.19860172271729, 7.70432281494141, 
7.67850542068481, 4.99736976623535, 6.99806356430054, 8.00393962860107, 
6.09957933425903)), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))

It looks like this:

> dat
# A tibble: 30 × 2
   truth.agg .pred
       <dbl> <dbl>
 1         5  4.73
 2         4  4.73
 3         4  4.73
 4         8  5.15
 5         3  2.28
 6         2  4.60
 7         3  4.60
 8         8  8.47
 9         6  5.63
10         8  7.94
# … with 20 more rows
# ℹ Use `print(n = ...)` to see more rows

But when I do this perform the correlation, I got error:

> cor(dat$truth.agg, dat$.pred)
Error in cor(dat$truth.agg, dat$.pred) : unused argument (dat$.pred)

What's the right way to go about it?


Solution

  • For me worked!

    cor(dat$truth.agg, dat$.pred)
    
    [1] 0.8890882
    

    Maybe check if you loaded some package with another function called cor, try ?corto see if other functions appears.