Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Showing a random subset of all possible groups

  • DaveyKrebs
  • DaveyKrebs's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 9 months ago #96625 by DaveyKrebs
Hello,

I'm trying to set up a questionnaire that will display only a random subset of all possible groups to participants, and I'm wondering, is it possible to do this with Expression Manager (or a combination of javascript and EM)?

Within each group is a number of question variants, only one of which will be displayed to participants, selected at random. I found this to be very easy to implement with EM, thanks to this example.

Displaying only a subset of possible groups is more difficult since multiple groups need to qualify as relevant, and the selection of each group needs to be independent of the other groups. That is, each group can't simply occur with a fixed selection of other groups; the probability of occurring with any other group must be equal.

I'm currently using an unsatisfactory work-around, in which I have a hidden equation question corresponding to each group that randomly generates 1 or 2, with the relevance of each group set to 1. This means that each group has 50% probability of being displayed to any given participant, and that each participant will see, on average, half of the possible groups, but also that some participants could end up seeing quite a bit more/less than that, which is undesirable.

I thought that a possible solution would be to have a hidden question that randomly shuffles an array of integers (corresponding to the total number of groups) and then takes a slice of that shuffled array. Each group would have a unique relevance value corresponding to an integer from within this array, and would be set to display if it's value is among the slice generated. This sounds simple enough to explain, but as I'm nearly programming illiterate, I don't know how difficult it is to actually implement. The first part I could probably manage but I don't if the second part is within the current capabilities of the relevance equations to specify. Is it possible, and if so, how?

If you've read this far, many thanks for your patience, please forgive my ignorance, and many apologies if I've accidentally left out any important details. Any reply would be much appreciated.
The topic has been locked.
  • DaveyKrebs
  • DaveyKrebs's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 9 months ago #96675 by DaveyKrebs
Replied by DaveyKrebs on topic Showing a random subset of all possible groups
Ok, no-one's replied, so I'm guessing my question was problematic in some way, but it would really help to know what way that was. Please, any feedback at all would be really helpful; e.g. "I don't understand what you're asking", or "That's not possible", or "That is possible but I'm not going to explain how", or "It's really obvious and you should easily figure out the answer", or even "I don't know".

Incidentally, I'm not the only one who's asking this type of question. This post by Iosononando is essentially about the same thing.

Many thanks!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 9 months ago #96684 by tpartner
Replied by tpartner on topic Showing a random subset of all possible groups
Placing this script in the source of a multiple-numeric question will shuffle an array of group numbers and then load the numeric inputs with the corresponding array values. (So, if for example, you only want to capture the first 4 random numbers, use 4 subquestions it the multiple-numeric.)

You can then use relevance/conditions to display following groups depending on the multiple-numeric values.

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function(){
 
    // Identify the questions
    var q1ID = '{QID}';
    var q1 = $('#question'+q1ID+'');
    var inputsCount = $('input[type="text"]', q1).length;
 
    if($('input[type="text"]:eq(0)', q1).val() == '') {
 
      // An array holding the number of groups
      var groups = [1,2,3,4,5,6,7,8,9,10,11,12];
 
      // Shuffle the array
      function shuffle(o){
        for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
        return o;
      }
      shuffle(groups);
 
      // Load the numeric inputs with the corresponding array values
      $(groups).each(function(i){
        if(i < inputsCount) {
          $('input[type="text"]:eq('+i+')', q1).val(this);
        }
      });
    }
 
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DaveyKrebs
The topic has been locked.
  • DaveyKrebs
  • DaveyKrebs's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 9 months ago #96700 by DaveyKrebs
Replied by DaveyKrebs on topic Showing a random subset of all possible groups
Thanks very much for your reply, and for the javascript code - it works perfectly.

However, I'm still having trouble using the multiple-numeric values to control group displays. I tried an example with 10 groups, and specified 10 subquestions in the multiple-numeric, then set the relevance of following groups to display if the corresponding subquestion returned a value of less than or equal to 5. The effect should have been to display only 5 random selected groups, but it doesn't work. Instead, they all display, despite the survey logic coming out clear.

I tried the way you initially suggested but couldn't figure out to define an appropriate variable (the multiple-numeric question code was reported to be "undefined" but the subquestion codes were fine).

I'm attaching a copy of my survey - perhaps someone could take a look and see if it's an easily fixable problem?

Many thanks again Tony!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 9 months ago - 10 years 9 months ago #96710 by tpartner
Replied by tpartner on topic Showing a random subset of all possible groups
Your approach would work too.

In the attached survey, I've modified your relevance equations to something like:
Code:
rask_SQ01.NAOK gt 0 &amp;&amp; rask_SQ01.NAOK le 5

And, since the survey is in "all-in-one" mode, added a snippet to the script that fires the checkconditions() function after loading the numeric inputs:
Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function(){
 
    // Identify the question
    var q1ID = {QID};
    var q1 = $('#question'+q1ID+'');
    var inputsCount = $('input[type="text"]', q1).length;
 
    if($('input[type="text"]:eq(0)', q1).val() == '') {
 
      // An array holding the number of groups
      var groups = [1,2,3,4,5,6,7,8,9,10];
 
      // Shuffle the array
      function shuffle(o){
        for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
        return o;
      }
      shuffle(groups);
 
      // Load the numeric inputs with the corresponding array values
      $(groups).each(function(i){
        if(i < inputsCount) {
          var el = $('input[type="text"]:eq('+i+')', q1);
          $(el).val(this);
          checkconditions($(el).attr('value'), $(el).attr('name'), $(el).attr('type'));
        }
      });
    }
 
  });
</script>

File Attachment:

File Name: limesurvey...2_TP.lss
File Size:55 KB



If you want to use my approach, the relevance equations should look something like (one line per group):
Code:
rask_SQ01.NAOK == 1 OR rask_SQ02.NAOK == 1 OR rask_SQ03.NAOK == 1 OR rask_SQ04.NAOK == 1 OR rask_SQ05.NAOK == 1
rask_SQ01.NAOK == 2 OR rask_SQ02.NAOK == 2 OR rask_SQ03.NAOK == 2 OR rask_SQ04.NAOK == 2 OR rask_SQ05.NAOK == 2
rask_SQ01.NAOK == 3 OR rask_SQ02.NAOK == 3 OR rask_SQ03.NAOK == 3 OR rask_SQ04.NAOK == 3 OR rask_SQ05.NAOK == 3
rask_SQ01.NAOK == 4 OR rask_SQ02.NAOK == 4 OR rask_SQ03.NAOK == 4 OR rask_SQ04.NAOK == 4 OR rask_SQ05.NAOK == 4
rask_SQ01.NAOK == 5 OR rask_SQ02.NAOK == 5 OR rask_SQ03.NAOK == 5 OR rask_SQ04.NAOK == 5 OR rask_SQ05.NAOK == 5
...

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 9 months ago by tpartner.
The following user(s) said Thank You: DaveyKrebs
The topic has been locked.
  • DaveyKrebs
  • DaveyKrebs's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 9 months ago #96713 by DaveyKrebs
Replied by DaveyKrebs on topic Showing a random subset of all possible groups
Works perfectly now, thanks!

I didn't realise the switch between "all-in-one" and "group by group" would mess with function, and was actually only using "all-in-one" for quicker testing. Sorry, I should have tested that myself.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose