0

everyone! I'm a not a good programmer in R, so I have some issues trying to use the apply function. Could you help me?

My code is raising an error:

`Error in apply(as.array(na.omit(image_probabilities)), MARGIN = c(1, 2),  : 
  'MARGIN' does not match dim(X)
Called from: apply(as.array(na.omit(image_probabilities)), MARGIN = c(1, 2), 
    function(pixel) {
         which.max(pixel)
    })`

I'm coding a maximum_likelihood_classifier and saving the probability images in a list, when I try to access which position in this list has the maximum probability value, I get that error I showed above...

Below I share my code:

maximum_likelihood_classifier <- function(image_data, df_samples, classes) {

  num_classes <- length(classes)
  image_probabilities <- vector("list", num_classes)

  split_classes <- split_classes(df_samples, classes)

      for (i in 1:num_classes){
      params <- gaussian_parameters_estimation(split_classes[[i]])
      image_probabilities[[i]] <- gaussian_class_likelihood(image_data,params)
    }

   classified_image <- apply(as.array(na.omit(image_probabilities)), MARGIN = c(1, 2), function(pixel) {
    which.max(pixel)
  })

  return(classified_image)
}

I'm looking for a solution to solve this error I will be very thankful.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • The `apply()` function is use on a matrix, I am not sure what "image_probabilities". also MARGIN should equal 1 or 2 and not a vector. Use 1 to apply the function to the rows and use 2 to apply to columns. – Dave2e Sep 02 '23 at 17:28
  • 1
    Can you post `str(image_probabilities)`? @Dave2e: `apply` can be applied to any array of dimension >= 2 so that margin argument might make sense if `length(dim(.)) > 2`. – Rui Barradas Sep 02 '23 at 18:21
  • @Dave2e, `MARGIN` _can_ be a vector (length > 1), but that is generally only useful for arrays of dim > 2. For instance, `apply(matrix(1:20,nr=4), 1:2, sum)` works but is working cell-wise (e.g., `sum(4) -> 4`), whereas `apply(array(1:60,dim=3:5), 1:2, sum)` sums the planes by "pipe" into a single-plane matrix 3x4 matrix. (*Edit*: sorry, I see now that Rui replied as well, not trying to pile on to that point.) – r2evans Sep 02 '23 at 18:59
  • 1
    Welcome to SO, Vinícius D'Lucas! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (you have a good start, but please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Sep 02 '23 at 19:01
  • Thank you. Point taken. I assumed a two dimensional structure and did not consider 3 or more dimensions (my mistake). But considering the error message "'MARGIN' does not match dim(X)" and the "not a good programmer" comment, I think it seems likely "image_probabilities" is just a vector. – Dave2e Sep 02 '23 at 20:04

0 Answers0