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.