knitr::opts_chunk$set(echo = TRUE)

Introduction

In this analysis, we use an excerpt of a data on public perception on auditing independence set to create contigency table and perform Fisher’s Exact Test. We also create bar graph for categorical data.

# Load required libraries
library(ggplot2)
library(gridExtra)
library(tidyr)
# Create a data frame with your data
data <- data.frame(
  Statements = c("Independence is the essence of auditing",
                 "An internal auditor must be independent of both the personnel and operational activities of an organization",
                 "Independence is necessary for the effective achievement of the function and objective of internal audit",
                 "Independence auditing is necessary for the financial reporting quality of the organization.",
                 "The internal audit department in an organization must be independent from the activities which it controls and must likewise be independent from the day-to-day internal control processes"),
  `Strongly Agree` = c(15, 13, 16, 10, 9),
  `Agree` = c(9, 9, 4, 12, 11),
  `Neutral` = c(1, 2, 5, 2, 3),
  `Disagree` = c(0, 1, 0, 1, 2),
  `Strongly Disagree` = c(0, 0, 0, 0, 0)
)
# Convert data from wide to long format for easier plotting
data_long <- tidyr::gather(data, key = "Response", value = "Count", -Statements)
# Plot each statement separately
plots <- lapply(unique(data$Statements), function(statement) {
  plot_data <- subset(data_long, Statements == statement & Count > 0)  # Filter out responses with count = 0
  
  if (nrow(plot_data) > 0) {  # Check if there are any responses after filtering
    total_count <- sum(plot_data$Count)  # Calculate total count
    
    plot_data <- transform(plot_data, Percentage = Count / total_count * 100)  # Calculate percentage
    
    p <- ggplot(plot_data, aes(x = Response, y = Count, fill = Response)) +
      geom_bar(stat = "identity") +
      geom_text(aes(label = paste0(round(Percentage), "%")), vjust = -0.5, size = 3) +  # Add percentage labels
      labs(title = statement, x = "Response", y = "Count") +
      theme_minimal() +
      theme(legend.position = "none", plot.title = element_text(size = 12)) # Adjust title size
    
    return(p)
  } else {
    return(NULL)  # Return NULL if there are no responses
  }
})

# Remove NULL elements from the list
plots <- plots[!sapply(plots, is.null)]

# Arrange plots in a grid
grid.arrange(grobs = plots, ncol = 1)

# Remove the Statements column for creating the contingency table
response_data <- data[, -1]

# Create the contingency table with statements as row names
contingency_table <- t(response_data)

# Rename the rows with the statements
rownames(contingency_table) <- data$Statements

# Show the contingency table
contingency_table
##                                                                                                                                                                                            [,1]
## Independence is the essence of auditing                                                                                                                                                      15
## An internal auditor must be independent of both the personnel and operational activities of an organization                                                                                   9
## Independence is necessary for the effective achievement of the function and objective of internal audit                                                                                       1
## Independence auditing is necessary for the financial reporting quality of the organization.                                                                                                   0
## The internal audit department in an organization must be independent from the activities which it controls and must likewise be independent from the day-to-day internal control processes    0
##                                                                                                                                                                                            [,2]
## Independence is the essence of auditing                                                                                                                                                      13
## An internal auditor must be independent of both the personnel and operational activities of an organization                                                                                   9
## Independence is necessary for the effective achievement of the function and objective of internal audit                                                                                       2
## Independence auditing is necessary for the financial reporting quality of the organization.                                                                                                   1
## The internal audit department in an organization must be independent from the activities which it controls and must likewise be independent from the day-to-day internal control processes    0
##                                                                                                                                                                                            [,3]
## Independence is the essence of auditing                                                                                                                                                      16
## An internal auditor must be independent of both the personnel and operational activities of an organization                                                                                   4
## Independence is necessary for the effective achievement of the function and objective of internal audit                                                                                       5
## Independence auditing is necessary for the financial reporting quality of the organization.                                                                                                   0
## The internal audit department in an organization must be independent from the activities which it controls and must likewise be independent from the day-to-day internal control processes    0
##                                                                                                                                                                                            [,4]
## Independence is the essence of auditing                                                                                                                                                      10
## An internal auditor must be independent of both the personnel and operational activities of an organization                                                                                  12
## Independence is necessary for the effective achievement of the function and objective of internal audit                                                                                       2
## Independence auditing is necessary for the financial reporting quality of the organization.                                                                                                   1
## The internal audit department in an organization must be independent from the activities which it controls and must likewise be independent from the day-to-day internal control processes    0
##                                                                                                                                                                                            [,5]
## Independence is the essence of auditing                                                                                                                                                       9
## An internal auditor must be independent of both the personnel and operational activities of an organization                                                                                  11
## Independence is necessary for the effective achievement of the function and objective of internal audit                                                                                       3
## Independence auditing is necessary for the financial reporting quality of the organization.                                                                                                   2
## The internal audit department in an organization must be independent from the activities which it controls and must likewise be independent from the day-to-day internal control processes    0
# Perform Fisher's exact test with increased workspace
fisher_result <- fisher.test(contingency_table, workspace = 2e+08)

# Print the result
print(fisher_result)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  contingency_table
## p-value = 0.2323
## alternative hypothesis: two.sided

Result interpretation

The Fisher’s Exact Test yielded a p-value of 0.2323, indicating insufficient evidence to reject the null hypothesis of no significant association between the statements on auditing independence and the response categories.(In simpler terms, based on this test result, we don’t have enough evidence to suggest that there’s a meaningful relationship between how individuals responded to the statements and the statements themselves. Therefore, we cannot conclude that the responses significantly vary across the statements.)

Moreover, the alternative hypothesis stated as “two-sided” means that the test considers extreme results in both tails of the distribution. In other words, it tests whether there is a significant association between the statements and the response categories, regardless of the direction of the association.