R/GKappa.R

Defines functions GKappa GKappa.coMa print.GKappa

Documented in GKappa GKappa.coMa

#' @export
GKappa<- function(object, alpha){
    UseMethod("GKappa",object)
}

#' @name GKappa
#' @title Inference of the statistic Kappa
#' @description General function that groups the inference of the statistic Kappa. This function is made up of:
#' the statistic Kappa is a coefficient of agreement for nominal scales which measures the relationship of beyond chance agreement to expected disagreement,
#' the standard deviation is the square root of the Kappa variance that it is computed using the Delta method,
#' the confidence interval for Kappa statistic to confidence level 95 \% by default.
#' @usage GKappa(object,alpha)
#' @param object a coMa object (confusion matrix object)
#' @param alpha Significance level

#' @details Assuming a multinomial sampling model Cohen's Kappa is the maximum likelihood estimate of Kappa.
#' The standard deviation is calculated using the variance of Kappa according to Congalton.
#' @details The variance of Kappa is
#' \deqn{\frac{1}{n}\left( \frac{\theta_1(1-\theta_1)}{(1-\theta_2)^2} + \frac{2(1-\theta_1)(2\theta_1\theta_2-\theta_3)}{(1-\theta_2)^3} + \frac{(1-\theta_1)^2(\theta_4-4\theta_2^2)}{(1-\theta_2)^4} \right)}
#' where \eqn{n} is the sample size,
#' \deqn{\theta_1 = \frac{1}{n}\sum_{i = 1}^k n_{ii},}
#' \deqn{\theta_2 = \frac{1}{n^2}\sum_{i = 1}^k n_{i+}n_{+i},}
#' \deqn{\theta_3 = \frac{1}{n^2}\sum_{i = 1}^k n_{ii}(n_{i+} + n_{+i}),}
#' and
#' \deqn{\theta_4 = \frac{1}{n^3}\sum_{i = 1}^k \sum_{j = 1}^k n_{ij}(n_{j+} + n_{+i})^2.}
#' @details Confidence intervals can be computed using the approximate large sample variance and the fact tha de statistic is asymptotically normally distributed.
#' @return \code{GKappa} returns a list with the following elements:
#' @references
#' Cohen, J. (1960).
#' \emph{A coefficient of agreement of nominal scales.}
#'  Educational and Psychological Measurement, 20, 37-46.
#' @references
#' Rosenfield, G. H., & Fitzpatrick-Lins, K. (1986).
#' \emph{ A coefficient of agreement as a measure of thematic classification accuracy.}
#'  Photogrammetric Engineering and Remote Sensing, 52, 223-227.
#' @references
#' Congalton, R.G., Green, K. (2009)
#' \emph{Assessing the accuracy of Remote Sensed Data.}
#' Principles and Practices. CRC Press. Taylor & Francis Group.
#' @examples
#' #Let evaluate the inference of statistic Kappa.
#' ## Confusion matrix included in Congalton and Green (2009), pg. 108.
#' x <- coMa(cbind(c(65,6,0,4),c(4,81,11,7),c(22,5,85,3),c(24,8,19,90)))
#' ## Inference of statistic Kappa
#' InfKappa <- GKappa(x,0.02) # By default alpha = 0.05
#'
#' @importFrom  stats qnorm
#' @export
GKappa.coMa <- function(object, alpha = 0.05){
  if (!inherits(object,"coMa"))
     stop("object must be a coMa object")
  x<-object$data
  Margcol <- colSums(x)
  Margrow <- rowSums(x)
  nc <- nrow(x)
  dimension <- nrow(x)
  N <-sum(x)

  # The 4 coefficients
  O1 <- sum(diag(x))/N
  O2 <- sum((Margcol*Margrow))/(N^2)
  O3 <- sum(diag(x)*(Margcol+Margrow))/(N^2)
  mintermedia1<- matrix(rep(Margcol, nc), nrow =nc, ncol=nc, byrow=TRUE)
  mintermedia2<- matrix(rep(Margrow, nc), nrow =nc, ncol=nc, byrow=FALSE)
  mintermedia3 <-(mintermedia1+mintermedia2)^2
  O4 <- sum(x*(t(mintermedia3)) )/(N^3)

  t1 <- (1-O1)
  t2<- (1-O2)
  t3<- 2*O1*O2-O3
  t4<- O4-4*(O2^2)
  t5<- O1*t1/(t2^2)+2*t1*t3/(t2^3)+(t1^2)*t4/(t2^4)

  Kappa <- (O1-O2)/t2
  VarKappa = (1/N)*t5
  SdKappa <- sqrt(VarKappa)
  Ic1 <- Kappa - qnorm(1 - alpha/2)*sqrt(VarKappa)
  Ic2 <- Kappa + qnorm(1 - alpha/2)*sqrt(VarKappa)
  IC <- c(Ic1, Ic2)

  ans <- list(
    alpha = alpha,
    Kappa = Kappa,
    StandarDeviation.Kappa = SdKappa,
    Confidence.Interval = IC)
  class(ans)<-"GKappa"
  return(ans)

}


#' @method print GKappa
#' @export
print.GKappa<-function(x, ...){
  function (x, ...){
    if (!inherits(x, "GKappa")){
      stop("x not is a coMa object")
    }else{
      cat("Object class coMa\n")
      cat("GKappa\n")
    }
  }
}
ujaen-statistics/ThemAAs documentation built on Nov. 5, 2019, 11:03 a.m.