Randomly select subset of items

90 views
Skip to first unread message

Kate Stone

unread,
Apr 23, 2018, 5:30:23 AM4/23/18
to ibexexperiments
Hopefully my last question...

My experiment consists of two parts. I want to show all of the items from part 1, but only a randomly selected subset (33%) of the items from part 2. Is this possible?

I thought maybe to use modifyRunningOrder in some way, but I have two rshuffles (i.e. two "ro's") and I don't know how to isolate the one I want.

Thanks for any suggestions!!

Jérémy Zehr

unread,
Apr 24, 2018, 1:19:03 PM4/24/18
to ibexexperiments
Hi Kate

If I understand you correctly, you want to repeat the same items in part 2, but only a subset of them.

So your shuffleSequence variable declaration should look like this, assuming a randomProporition function that randomly picks a ratio of N items (here 0.33 = 33%) from a set:

var shuffleSequence = seq(rshuffle("itemLabel"), randomProportion(rshuffle("itemLabel"), .33));

If that's indeed what you want, here is the code for the randomProportion function (you can put it directly in your example_data.js, or in a new .js file under js_includes, or even append it to shuffle.js under www if you have a local setup)

function RandomProportion(x, n) {

    assert(typeof(n)=="number" && n>0, "N should be a number greater than 0");

    this.args = [x];

    this.run = function(arrays) {
        var targetLength = Math.round(arrays[0].length * n);
        if (n>1) {
            while (arrays[0].length<targetLength)
                arrays[0].push(arrays[0][Math.floor(Math.random()*arrays[0].length)]);
        }
        else {
            while (arrays[0].length>targetLength)
                arrays[0].splice(Math.floor(Math.random()*arrays[0].length), 1);
        }
        return arrays[0];
    }
}
function randomProportion(x, n) { return new RandomProportion(x, n); }


Does it correspond to what you wanted?

Kate Stone

unread,
Apr 24, 2018, 2:44:44 PM4/24/18
to ibexexperiments
Hi Jérémy,

Yes, great, that's exactly what I was looking for! I can't get it to work though - I tried putting the function in the example_data.js and alternatively appending it to shuffle.js, but that's not the issue. My items in part 2 are different to those in part 1, but I tested it on part 1 only and it still showed the full 100%. My sequence looks like:

var shuffleSequence = seq("intro",
                            "setcounter",
                                "demographics",
                                    "part1",sepWith("sep", seq(rshuffle("pva","pvb","filler"))),
                                        "part2",sepWith("sep", seq(rshuffle("awa","awb"), randomProportion(rshuffle("awa","awb"), .33))),
                                            "end");

Maybe I've made a typo somewhere? It runs, just shows 100% instead of 33%.

Best,
Kate

Jérémy Zehr

unread,
Apr 24, 2018, 4:14:21 PM4/24/18
to ibexexperiments
Hi Kate,

What you have right now after "part2" shows the full (randomized) sequence of your awa/awb items and then shows an additional random subset of 33% of these items.

So from my understanding, what you want is something like:

var shuffleSequence = seq("intro",
                           
"setcounter",
                               
"demographics",

                                   
"part1",sepWith("sep", rshuffle("pva","pvb","filler")),
                                       
"part2",sepWith("sep", randomProportion(rshuffle("awa","awb"), .33)),
                                           
"end");

Kate Stone

unread,
Apr 25, 2018, 2:32:11 AM4/25/18
to ibexexperiments
Oops, yes of course, that was silly :/

Now it's working perfectly!

Many thanks again for solving both my posts,
Kate
Reply all
Reply to author
Forward
0 new messages