How to calculate the Statistic Mode?

How to calculate the Statistic Mode?

by Marcelo Rodrigues -
Number of replies: 4

Hello everyone,

Is there a formula in Moodle or a function that calculates the frequency of numbers in a list?

For example: 2 3 4 6 2 9 7 2 0 1 2 => higher frequency => number 2 (4 times)

I refer to the Statistics formula for calculating Mode.

Thank you! Marcelo.

Average of ratings: -
In reply to Marcelo Rodrigues

Re: How to calculate the Statistic Mode?

by Bernat Martinez -
Hi Marcello,

As far as I know there is not any function in FORMULAS to calculate mode.
In this cases is always to think in a different way and to "prepare" the question with some set of numbers, random is limited of course

Attached is an example how to set up a question like this. 
Beware question uses mlang filter
In reply to Bernat Martinez

Re: How to calculate the Statistic Mode?

by Marcelo Rodrigues -
Hi Bernat,

Thank you very much for your help and for the accuracy of the explanations.

Thank you very much for the example you sent me.

Marcelo.
In reply to Marcelo Rodrigues

Re: How to calculate the Statistic Mode?

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Marcelo,

The Formulas question type does not have this function. However, it has other functions with which you can built many things. For example, the following code calculates the highest frequency in a list of numbers.

The code can certainly be more complete. For example, if there are two or more numbers that repeat most often, the code displays only one. But I only developed it as a demo. Feel free to modify it according to your needs.

Random variables
  r0={1:10};
  r1={1:10};
  r2={1:10};
  r3={1:10};
  r4={1:10};
  r5={1:10};
  r6={1:10};
  r7={1:10};
  r8={1:10};
  r9={1:10};
  r10={1:10};
Global variables  
  a = [r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10];
  vmax=a[0];
  b = fill (len(a),1);
  nmax = 1;
  ii=0;
  for ( i : b ) {
    jj = 0;
    n = 0 ;
    for ( j : b )  {
      n = ( a[ii] == a[jj] ) ? n + 1 : n ;
      jj = jj +1 ;
    }
    nmax = (n >= nmax) ? n : nmax ;
    vmax = (n >= nmax) ? a[ii] : vmax ;
    ii = ii + 1;
  } ;
Question text
  [{a[0]},{a[1]},{a[2]},{a[3]},{a[4]},{a[5]},{a[6]},{a[7]},{a[8]},{a[9],{a[10]}]<br>
  Highest frequency => number {vmax} ({nmax} times)

Here's what you'll get:


XML file attached:

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: How to calculate the Statistic Mode?

by Marcelo Rodrigues -
Hi Dominique,

Thanks a lot for the help.

This forum, for me, has been a true classroom, where I am learning a lot from the friends who respond.

Thank you so much for your time in teaching me.

Marcelo.