Missing Data in Correlations


Table of Contents

  1. Create the data
  2. The Everything Method
  3. The “all.obs” method
  4. The “complete.obs” method
  5. The “pairwise.complete.obs” method

1. Create the Data

Create the data with the planned missing data.

library(tidyverse)
adverts <- c(5, 4, 4, 6, 8)
packetsNA <- c(8, 9, 10, NA, 15)
age <- c(5, 12, 16, 9, 14)

Create the dataframe and convert to tibble format.

advertNA <- as.tibble(data.frame(adverts, packetsNA, age))
advertNA
## # A tibble: 5 x 3
##   adverts packetsNA   age
##     <dbl>     <dbl> <dbl>
## 1      5.        8.    5.
## 2      4.        9.   12.
## 3      4.       10.   16.
## 4      6.       NA     9.
## 5      8.       15.   14.

Go to top


2. The everything method

cor(advertNA, use = "everything",  method = "pearson")
##              adverts packetsNA        age
## adverts   1.00000000        NA 0.02072962
## packetsNA         NA         1         NA
## age       0.02072962        NA 1.00000000

What happened?

Go to top


3. The “all.obs” method

cor(advertNA, use = "all.obs",  method = "pearson")
Error in cor(advertNA, use = "all.obs", method = "pearson") : 
  missing observations in cov/cor

What happened?:

Go to top


4. The “complete.obs” method

cor(advertNA, use = "complete.obs",  method = "pearson") 
##              adverts packetsNA        age
## adverts   1.00000000 0.8778665 0.08276409
## packetsNA 0.87786648 1.0000000 0.54869466
## age       0.08276409 0.5486947 1.00000000

What happened?:

Go to top


5. The “pairwise.complete.obs”

cor(advertNA, use = "pairwise.complete.obs",  method = "pearson")
##              adverts packetsNA        age
## adverts   1.00000000 0.8778665 0.02072962
## packetsNA 0.87786648 1.0000000 0.54869466
## age       0.02072962 0.5486947 1.00000000

What happened?:

Go to top


Complete the Table

  Packets and Advertisements Packets and Age Age and Advertisement
everything      
all.obs      
complete.obs      
pairwise.complete.obs      

Go to top