fork download
  1. # Load the vcd library for Cohen's Kappa and Weighted Kappa calculation
  2. # If not installed, run: install.packages("vcd")
  3. library(vcd)
  4.  
  5. # Create the 5x5 contingency matrix based on the provided table data
  6. # Rows: Method II (None, Mild, Moderate, Severe, Extreme)
  7. # Columns: Method I (None, Mild, Moderate, Severe, Extreme)
  8. data_matrix <- matrix(
  9. c(89, 5, 16, 2, 4, # None (Method II)
  10. 36, 3, 15, 6, 2, # Mild (Method II)
  11. 20, 4, 22, 6, 1, # Moderate (Method II)
  12. 14, 2, 37, 18, 16, # Severe (Method II)
  13. 4, 1, 16, 23, 50), # Extreme (Method II)
  14. nrow = 5,
  15. byrow = TRUE
  16. )
  17.  
  18. # Set row and column names
  19. categories <- c("None", "Mild", "Moderate", "Severe", "Extreme")
  20. rownames(data_matrix) <- categories
  21. colnames(data_matrix) <- categories
  22.  
  23. # Display the contingency table
  24. print("Contingency Table:")
  25. print(data_matrix)
  26.  
  27. # (i) Calculate Unweighted Kappa statistic
  28. kappa_unweighted <- Kappa(data_matrix, weights = "equal")
  29. print("Unweighted Kappa Statistic:")
  30. print(kappa_unweighted)
  31.  
  32. # (ii) Calculate Weighted Kappa statistic (using default squared/Fleiss-Cohen weights)
  33. kappa_weighted <- Kappa(data_matrix, weights = "Fleiss-Cohen")
  34. print("Weighted Kappa Statistic:")
  35. print(kappa_weighted)
  36.  
  37. # Interpretation helper function based on Landis and Koch guidelines
  38. interpret_kappa <- function(k) {
  39. if (k < 0) {
  40. return("Poor agreement (less than chance)")
  41. } else if (k <= 0.20) {
  42. return("Slight agreement")
  43. } else if (k <= 0.40) {
  44. return("Fair agreement")
  45. } else if (k <= 0.60) {
  46. return("Moderate agreement")
  47. } else if (k <= 0.80) {
  48. return("Substantial agreement")
  49. } else {
  50. return("Almost perfect agreement")
  51. }
  52. }
  53.  
  54. # Extract values and print interpretations
  55. unweighted_val <- kappa_unweighted$Unweighted[1]
  56. weighted_val <- kappa_weighted$Weighted[1]
  57.  
  58. cat("\n--- Interpretation of Results ---\n")
  59. cat(sprintf("(i) Unweighted Kappa = %.4f -> %s\n", unweighted_val, interpret_kappa(unweighted_val)))
  60. cat(sprintf("(ii) Weighted Kappa = %.4f -> %s\n", weighted_val, interpret_kappa(weighted_val)))
Success #stdin #stdout 0.02s 25220KB
stdin
Standard input is empty
stdout
# Load the vcd library for Cohen's Kappa and Weighted Kappa calculation
# If not installed, run: install.packages("vcd")
library(vcd)

# Create the 5x5 contingency matrix based on the provided table data
# Rows: Method II (None, Mild, Moderate, Severe, Extreme)
# Columns: Method I (None, Mild, Moderate, Severe, Extreme)
data_matrix <- matrix(
  c(89,  5, 16,  2,  4,   # None (Method II)
    36,  3, 15,  6,  2,   # Mild (Method II)
    20,  4, 22,  6,  1,   # Moderate (Method II)
    14,  2, 37, 18, 16,   # Severe (Method II)
     4,  1, 16, 23, 50),  # Extreme (Method II)
  nrow = 5,
  byrow = TRUE
)

# Set row and column names
categories <- c("None", "Mild", "Moderate", "Severe", "Extreme")
rownames(data_matrix) <- categories
colnames(data_matrix) <- categories

# Display the contingency table
print("Contingency Table:")
print(data_matrix)

# (i) Calculate Unweighted Kappa statistic
kappa_unweighted <- Kappa(data_matrix, weights = "equal")
print("Unweighted Kappa Statistic:")
print(kappa_unweighted)

# (ii) Calculate Weighted Kappa statistic (using default squared/Fleiss-Cohen weights)
kappa_weighted <- Kappa(data_matrix, weights = "Fleiss-Cohen")
print("Weighted Kappa Statistic:")
print(kappa_weighted)

# Interpretation helper function based on Landis and Koch guidelines
interpret_kappa <- function(k) {
  if (k < 0) {
    return("Poor agreement (less than chance)")
  } else if (k <= 0.20) {
    return("Slight agreement")
  } else if (k <= 0.40) {
    return("Fair agreement")
  } else if (k <= 0.60) {
    return("Moderate agreement")
  } else if (k <= 0.80) {
    return("Substantial agreement")
  } else {
    return("Almost perfect agreement")
  }
}

# Extract values and print interpretations
unweighted_val <- kappa_unweighted$Unweighted[1]
weighted_val <- kappa_weighted$Weighted[1]

cat("\n--- Interpretation of Results ---\n")
cat(sprintf("(i) Unweighted Kappa = %.4f -> %s\n", unweighted_val, interpret_kappa(unweighted_val)))
cat(sprintf("(ii) Weighted Kappa = %.4f -> %s\n", weighted_val, interpret_kappa(weighted_val)))