#===========================================================================================
#ac2.linear.i: This function produces all n linear components associated with
# the n subjects, and the linearized version of the ac2 coefficient.
#-------------
#The input data "ratings" is a nxr data frame of raw alphanumeric ratings
#from n subjects and r raters. Exclude all subjects that are not rated by any rater.
#Bibliography:
#Gwet, K. L. (2008). ``Computing inter-rater reliability and its variance in the presence of high
# agreement." British Journal of Mathematical and Statistical Psychology, 61, 29-48.
#============================================================================================
ac2.linear.i <- function(ratings,weights="unweighted",conflev=0.95,N=Inf){
ratings.mat <- as.matrix(ratings)
if (is.character(ratings.mat)){ratings.mat <- toupper(trim(ratings.mat))}
n <- nrow(ratings.mat) # number of subjects
r <- ncol(ratings.mat) # number of raters
f <- n/N # final population correction
# creating a vector containing all categories used by the raters
categ.init <- unique(as.vector(ratings.mat))
if (is.numeric(categ.init))
categ <- sort(as.vector(stats::na.omit(categ.init)))
else{
categ.init <- trim(categ.init) #trim vector elements to remove leading and trailing blanks
categ <- categ.init[nchar(categ.init)>0]
categ <- sort(categ)
}
q <- length(categ)
# creating the weights matrix
if (is.character(weights)){
if (weights=="quadratic")
weights.mat<-quadratic.weights(categ)
else if (weights=="ordinal")
weights.mat<-ordinal.weights(categ)
else if (weights=="linear")
weights.mat<-linear.weights(categ)
else if (weights=="radical")
weights.mat<-radical.weights(categ)
else if (weights=="ratio")
weights.mat<-ratio.weights(categ)
else if (weights=="circular")
weights.mat<-circular.weights(categ)
else if (weights=="bipolar")
weights.mat<-bipolar.weights(categ)
else weights.mat<-identity.weights(categ)
}else weights.mat= as.matrix(weights)
# creating the nxq agreement matrix representing the distribution of raters by subjects and category
agree.mat <- matrix(0,nrow=n,ncol=q)
for(k in 1:q){
if (is.numeric(ratings.mat)){
k.mis <-(ratings.mat==categ[k])
in.categ.k <- replace(k.mis,is.na(k.mis),FALSE)
agree.mat[,k] <- in.categ.k%*%rep(1,r)
}else{
in.k <- (trim(ratings.mat)==categ[k])
in.k[is.na(in.k)] <- FALSE
agree.mat[,k] <- in.k%*%rep(1,r)
}
}
agree.mat.w <- t(weights.mat%*%t(agree.mat))
# calculating gwet's ac1 coefficient
ri.vec <- agree.mat%*%rep(1,q)
sum.q <- (agree.mat*(agree.mat.w-1))%*%rep(1,q)
n2more <- sum(ri.vec>=2)
pa <- sum(sum.q[ri.vec>=2]/((ri.vec*(ri.vec-1))[ri.vec>=2]))/n2more
pi.vec <- t(t(rep(1/n,n))%*%(agree.mat/(ri.vec%*%t(rep(1,q)))))
if (q>1){
pe <- sum(weights.mat) * sum(pi.vec*(1-pi.vec)) / (q*(q-1))
}else{pe <- 0}
gwet.ac1 <- (pa-pe)/(1-pe)
# calculating variance, stderr & p-value of gwet's ac1 coefficient
den.ivec <- ri.vec*(ri.vec-1)
den.ivec <- den.ivec - (den.ivec==0) # this operation replaces each 0 value with -1 to make the next ratio calculation always possible.
pa.ivec <- sum.q/den.ivec
pe.r2 <- pe*(ri.vec>=2)
ac1.ivec <- (n/n2more)*(pa.ivec-pe.r2)/(1-pe)
if (q>1){
pe.ivec <- (sum(weights.mat)/(q*(q-1))) * (agree.mat%*%(1-pi.vec))/ri.vec
}else{
pe.ivec <- matrix(0,n,1)
}
ac1.ivec.x <- ac1.ivec - 2*(1-gwet.ac1) * (pe.ivec-pe)/(1-pe)
return(list("icoeff"=ac1.ivec.x,"weights"=weights.mat))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.