How to find the statistical mode?

前端 未结 30 2345
时光取名叫无心
时光取名叫无心 2020-11-21 07:00

In R, mean() and median() are standard functions which do what you\'d expect. mode() tells you the internal storage mode of the objec

30条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 07:41

    I case your observations are classes from Real numbers and you expect that the mode to be 2.5 when your observations are 2, 2, 3, and 3 then you could estimate the mode with mode = l1 + i * (f1-f0) / (2f1 - f0 - f2) where l1..lower limit of most frequent class, f1..frequency of most frequent class, f0..frequency of classes before most frequent class, f2..frequency of classes after most frequent class and i..Class interval as given e.g. in 1, 2, 3:

    #Small Example
    x <- c(2,2,3,3) #Observations
    i <- 1          #Class interval
    
    z <- hist(x, breaks = seq(min(x)-1.5*i, max(x)+1.5*i, i), plot=F) #Calculate frequency of classes
    mf <- which.max(z$counts)   #index of most frequent class
    zc <- z$counts
    z$breaks[mf] + i * (zc[mf] - zc[mf-1]) / (2*zc[mf] - zc[mf-1] - zc[mf+1])  #gives you the mode of 2.5
    
    
    #Larger Example
    set.seed(0)
    i <- 5          #Class interval
    x <- round(rnorm(100,mean=100,sd=10)/i)*i #Observations
    
    z <- hist(x, breaks = seq(min(x)-1.5*i, max(x)+1.5*i, i), plot=F)
    mf <- which.max(z$counts)
    zc <- z$counts
    z$breaks[mf] + i * (zc[mf] - zc[mf-1]) / (2*zc[mf] - zc[mf-1] - zc[mf+1])  #gives you the mode of 99.5
    

    In case you want the most frequent level and you have more than one most frequent level you can get all of them e.g. with:

    x <- c(2,2,3,5,5)
    names(which(max(table(x))==table(x)))
    #"2" "5"
    

提交回复
热议问题