Module # 11 Debugging and defensive programming in R

 The bug in the code lies within the tukey.outlier function. The code for tukey_multiple seems to be designed to identify outliers in each column of a matrix x using the Tukey method, and then determine rows where all columns have outliers. However, there's a bug in the logic of the code. The && operator is used for element-wise logical AND operation, but it should be & for element-wise operation. 


Fixed CODE: 

tukey_multiple <- function(x) {

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

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

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

  }

  outlier.vec <- apply(outliers, 1, all)

  return(outlier.vec)

}


In this fixed version, we loop through each column of the input matrix x, identify outliers using tukey.outlier, and store the results in the outliers matrix. Then, we use the apply function to check for rows where all columns have outliers and return a logical vector indicating such rows.


Comments

Popular posts from this blog

Final Project [LIS4317]

Module # 7 assignment (Visual Analytics)

Module # 8 Input/Output, string manipulation and plyr package