I want to apply some statistical computations that comprise reliability measurements such as ICC or coefficient of variation. While I can compute them individually, I am not yet familiar with R functional programming practices to straight perform multiple computations without too much code repetition.
Consider the following data.frame
example comprising repeated measures (T1, T2
) on five different variables (Var1, ... Var5
) :
set.seed(123)
df = data.frame(matrix(rnorm(100), nrow=10))
names(df) <- c("T1.Var1", "T1.Var2", "T1.Var3", "T1.Var4", "T1.Var5",
"T2.Var1", "T2.Var2", "T2.Var3", "T2.Var4", "T2.Var5")
If I want to calculate an intraclass correlation coefficient between both repeated measures of each variable, I could: 1) Create function that returns: ICC, lower and upper bounds values:
calcula_ICC <- function(a, b) {
ICc <- ICC(matrix(c(a,b), ncol = 2))
icc <- ICc$results[[2]] [3]
lo <- ICc$results[[7]] [3]
up <- ICc$results[[8]] [3]
round(c(icc, lo, up),2)
}
and 2) apply it to each corresponding variable as follows:
calcula_ICC(df$T1.Var1, df$T2.Var1)
calcula_ICC(df$T1.Var2, df$T2.Var2)
calcula_ICC(df$T1.Var3, df$T2.Var3)
calcula_ICC(df$T1.Var4, df$T2.Var4)
calcula_ICC(df$T1.Var5, df$T2.Var5)
I would then procede similarly with other statistical computations on each variable such as coefficient of variation or standard error between repeated measurements.
However, how could use some of the functional programming principles? How could I create, for instance, a function that take each corresponding variable on T1
and T2
as well as the desired function as arguments?