I'm new to R and I'm working on a test-retest analysis using Cohen's kappa on several questions from a survey that was administered at 2 different times.
I wrote a function to run kappa and agree (from the irr package) cycling through several columns using a while statement that takes the first two columns from a dataframe as arguments for kappa2() and agree(), and then the next two columns, and so on. Here is a sample of the dataframe and the function:
Q1T1 Q1T2 Q2T1 Q2T2...
1 0 0 1
0 0 0 0
1 1 1 1
1 0 0 1
1 0 0 1
1 0 1 0
1 0 1 1
0 1 1 0
1 1 0 1
0 1 1 1
a <- 1 #argument for the first column containing question 1 at time 1
b <- 2 #argument for the second column containing question 1 at time 2
c <- 3 #number of questions
while (a < c * 2 + 1) {
kap <- kappa2(select(dat,a:b)) #Use this line for unweighted kappa
#kap <- kappa2(select(dat,a:b), "equal") #Use this line for weighted linear kappa
#kap <- kappa2(select(dat,a:b), "squared") #Use this line for weighted squared kappa
agr <- agree(select(dat,a:b), tolerance = 0)
ttl <- paste("Question", a %/% 2 + 1, "-")
cat(paste("..............................................................",ttl, sep = "\n"))
print(kap)
print(agr)
a = a + 2
b = b + 2
}
Is there a way to do this kind of cycling using apply(), lapply(), sapply(), or tapply() instead of the while loop? I feel that my function, although it works, is not a very R approach.
Thanks!