LIS 4370: Module # 11 Debugging and defensive programming in R

 

Module # 11 Debugging and defensive programming in R


Bugged Code:

tukey_multiple <- function(x) {

   outliers <- array(TRUE,dim=dim(x))

   for (j in 1:ncol(x))

    {

    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])

    }

outlier.vec <- vector(length=nrow(x))

    for (i in 1:nrow(x))

    { outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) }



Corrected Code:

tukey_multiple <- function(x) {

  outliers <- array(FALSE, dim = dim(x))  # Corrected initialization

  for (j in 1:ncol(x)) {

    outliers[, j] <- tukey.outlier(x[, j])  # Corrected logic

  }

  outlier.vec <- vector(length = nrow(x))

  for (i in 1:nrow(x)) { 

    outlier.vec[i] <- any(outliers[i, ])  # Corrected logic

  } 

  return(outlier.vec) 

}


Explanation:

Immediately as I looked over the code, I saw that the loop's update of the outliers array might have a flaw. The array's initialization and update appeared to deviate from the Tukey method's rationale. I found that debugging is frequently a gratifying and demanding task. To prepare it for updating with the output of the tukey.outlier method, the outliers array was initialized with FALSE rather than TRUE. The mechanism for updating the outliers array inside the loop has been corrected to correctly identify outliers for each input matrix x column.Then, to appropriately detect rows having at least one outlier, the logic inside the second loop was adjusted.

The tukey_multiple function's problem was successfully fixed after closely examining the code and comprehending the underlying reasoning. 

Comments

Popular posts from this blog

LIS 4370: Module # 10 Building your own R package

LIS 4317 Final Project: Fuel Economy Data from the U.S Dept. of Energy

LIS 4317: Module # 10 assignment