Assignment 11

 Today's lesson is in debugging. we were given the code:

tukey.outlier <- function(x, k = 1.5) {

  q1 <- quantile(x, 0.25, na.rm = TRUE)

  q3 <- quantile(x, 0.75, na.rm = TRUE)

  iqr <- q3 - q1

  x < (q1 - k * iqr) | x > (q3 + k * iqr)

}

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("logical", length = nrow(x))

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

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

  }

  return(outlier.vec)

Error in outliers[, j] && tukey.outlier(x[, j]) : 'length = 10' in coercion to 'logical(1)

when run with a seed it gives this error, this happens because this only evaluates the first element in each vector.

A fully corrected and debugged version of the code can be found here. This replaces the broken && code and tests it alongside the checks to make sure it kicks future errors.


tukey.outlier <- function(x, k = 1.5) {

  q1 <- quantile(x, 0.25, na.rm = TRUE)

  q3 <- quantile(x, 0.75, na.rm = TRUE)

  iqr <- q3 - q1

  x < (q1 - k * iqr) | x > (q3 + k * iqr)

}

corrected_tukey <- function(x) {

  if (!is.matrix(x)) {

    stop("x must be a matrix.")

  }

  if (!is.numeric(x)) {

    stop("x must be a numeric matrix.")

  }

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

  for (j in seq_len(ncol(x))) {

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

  }

  outlier.vec <- logical(nrow(x))

  for (i in seq_len(nrow(x))) {

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

  }

  outlier.vec

}

set.seed(123)

test_mat <- matrix(rnorm(50), nrow = 10)

tukey_multiple(test_mat)


Comments

Popular posts from this blog

Assignment #10: Building Your Own R Package

Module # 7 R Object: S3 vs. S4 assignment

Week 3