Skip to content

Instantly share code, notes, and snippets.

@matthewwolak
Last active August 29, 2015 14:23
Show Gist options
  • Save matthewwolak/daeb12a84bfef88a8c8e to your computer and use it in GitHub Desktop.
Save matthewwolak/daeb12a84bfef88a8c8e to your computer and use it in GitHub Desktop.
ICC non-standard evaluation

ICC non-standard evaluation examples

The ICC package for R calculates the intraclass correlation coefficient (ICC) from a one-way analysis of variance. Recently, the package was updated to better execute R's non-standard evaluation within each function (version 2.3.0 and higher). The package functions should now be able to handle a range of possible scenarios for calling the functions in, what I hope, is a less grotesque and more standard way of writing R functions. To demonstrate, below are some of those scenarios. Note, the examples use the ICCbare function, but the way in which the function arguments are supplied will apply to all of the functions in ICC.

First, load the package (and make sure the version is >2.3.0)

library(ICC)
packageVersion("ICC")

Columns of a data.frame

Here we supply the column names and the data.frame that contains the data to calculate the ICC. We will use the ChickWeight data fame.

data(ChickWeight)
ICCbare(x = Chick, y = weight, data = ChickWeight)
#$ICC
#[1] 0.1077609

Iterating through columns of a data.frame

In this case, we might have a data.frame in which we want to estimate the ICC for a number of different types of measurements that each has the same grouping or factor variable (e.g., x). The extreme of this might be in a simulation or bootstrapping scenario or even with some fancy high-throughput phenotyping/data collection. The point being, we want to automate the calculation of the ICC for each column.

First, we will simulate our own dataset with 3 traits to use in the example:

set.seed(101)
n <- 15                                    # number of individuals/groups/categories/factors
k <- 3                                     # number of measures per 'n'
va <- 1                                    # variance among
icc <- 0.6                                 # expected ICC
vw <- (va * (1 - icc)) / icc               # solve for variance within
simdf <- data.frame(ind = rep(LETTERS[1:n], each = k),
   t1 = rep(rnorm(n, 10, sqrt(va)), each = k) + rnorm(n*k, 0, sqrt(vw)),
   t2 = rep(rnorm(n, 10, sqrt(va)), each = k) + rnorm(n*k, 0, sqrt(vw)),
   t3 = rep(rnorm(n, 10, sqrt(va)), each = k) + rnorm(n*k, 0, sqrt(vw)))

Two ways to run through the columns come to mind: iteratively pass the name of each column or iteratively pass the column index. I will demonstrate both below. I do these in for loops so it is easier to see, but an easy extension would be to vectorise this by using something from the apply family of functions. First, passing the name:

for(i in names(simdf)[-1]){
   cat(i, ":")
   tmp.icc <- ICCbare(x = ind, y = i, data = simdf)
   cat(tmp.icc, "\n")
}
#t1 : 0.60446 
#t2 : 0.6381197 
#t3 : 0.591065 

or even like this:

for(i in 1:3){
   cat(paste0("t", i), ": ")
   tmp.icc <- ICCbare(x = ind, y = paste0("t", i), data = simdf)
   cat(tmp.icc, "\n")
}
#t1 : 0.60446 
#t2 : 0.6381197 
#t3 : 0.591065 

Alternatively, pass the column index:

for(i in 2:ncol(simdf)){
   cat(names(simdf)[i], ": ")
   tmp.icc <- ICCbare(x = ind, y = simdf[, i], data = simdf)
   cat(tmp.icc, "\n")
}
#t1 : 0.60446 
#t2 : 0.6381197 
#t3 : 0.591065 

Passing a character as an argument is deprecated

Note that the function will still work if a character is passed directly (e.g., "t1"), albeit with a warning. The warning just means that this may no longer work in future versions of the package. For example:

ICCbare(x = ind, y = "t1", data = simdf)
#[1] 0.60446
#Warning message:
#In ICCbare(x = ind, y = "t1", data = simdf) :
#  passing a character string to 'y' is deprecated since ICC vesion 2.3.0 and will not be supported in future versions. The argument #to 'y' should either be an unquoted column name of 'data' or an object

Note, however, that an expression evaluating to a character (e.g., paste0("t", 1)) doesn't throw the warning, which is nice!

Analytics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment