0

Here are the observations of two individuals of my dataset.

data=structure(list(id = c(2L, 2L, 2L, 3L, 3L, 3L), trt = c(1L, 1L, 
1L, 1L, 1L, 1L), status = c(0L, 0L, 0L, 2L, 2L, 2L), stage = c(3L, 
3L, 3L, 4L, 4L, 4L), spiders = c(1L, 1L, 1L, 0L, 1L, 0L), sex = structure(c(2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("m", "f"), class = "factor"), 
    hepato = c(1L, 1L, 1L, 0L, 1L, 0L), edema = c(0, 0, 0, 0.5, 
    0, 0.5), ascites = c(0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 
-6L), class = "data.frame")

I want to calculate the the statistical mode for each individual after grouping by id. I used this code below:

library(dplyr)
library(modeest)

    data%>%
      group_by(id)%>%mutate(edema2=mlv(edema))

And I get an error message when calculating the mode, while this method work well with other statistical parameters such as mean, sd, min, max....

Seydou GORO
  • 1,147
  • 7
  • 13
  • What's the exact error that you got? – MrFlick Jan 16 '21 at 01:40
  • Sorry, it worked, But I have a warning message: ```Warning messages: 1: Problem with `mutate()` input `edema2`. ℹ argument 'method' is missing. Data are supposed to be continuous. Default method 'shorth' is used ℹ Input `edema2` is `print(mlv(edema))`. ℹ The error occurred in group 1: id = 2. 2: argument 'method' is missing. Data are supposed to be continuous. Default method 'shorth' is used 3: Problem with `mutate()` input `edema2`.``` – Seydou GORO Jan 16 '21 at 01:55
  • And the values created seem corrects – Seydou GORO Jan 16 '21 at 01:56
  • 1
    The warning says that you didn't supply the 'method' argument, so it has defaulted to the 'shorth' method. You'll have to check the documentation from the package author to find what that means. The "data are supposed to be continuous" may imply something wrong with your input values – Calum You Jan 16 '21 at 02:52

1 Answers1

4

The warnings that you are getting are suggesting two things.

  1. You have not specified what method to choose so default method 'shorth' is used.

  2. It is suggesting that there is a tie in selection of Mode value.

Alternatively, why not use the Mode function from here :

Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

To apply by group you can use it with dplyr as :

library(dplyr)
data%>% group_by(id)%>% mutate(edema2= Mode(edema))

#     id   trt status stage spiders sex   hepato edema ascites edema2
#  <int> <int>  <int> <int>   <int> <fct>  <int> <dbl>   <int>  <dbl>
#1     2     1      0     3       1 f          1   0         0    0  
#2     2     1      0     3       1 f          1   0         0    0  
#3     2     1      0     3       1 f          1   0         0    0  
#4     3     1      2     4       0 m          0   0.5       0    0.5
#5     3     1      2     4       1 m          1   0         0    0.5
#6     3     1      2     4       0 m          0   0.5       0    0.5
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213