Qualtrics联合实验Javascript随机化条件

问题描述 投票:0回答:1

我正在 Qualtrics 和 JavaScript 中构建一个关于投票选择和违反民主规范的联合实验。在典型的联合中,您让所有属性级别都是随机的。然而,这在选举的情况下是不现实的,因为每个政党只有一名候选人。

这是我现在使用的代码:

    // Set number of choices (number of elections);
    var CJ_numChoice = 3;

    // Vectors containing all attribute levels:
    var CJ_eth = ["White", "South Asian", "East Asian", "Indigenous"];
    var CJ_gender = ["Man", "Woman"];
    var CJ_age = ["35", "65"];
    var CJ_pid = ["Liberal", "Conservative", "NDP"];
    var CJ_occup = ["Lawyer", "Political Staffer", "Construction worker", "Business person"];
    var CJ_exp = ["No experience", "Mayor", "Member of provincial legislature", "Member of Parliament"];
    var CJ_wel = ["Spending on welfare should be kept the same", "Spending on welfare should be increased", "Spending on welfare should be reduced"];
    var CJ_enviro = ["Must find a balance between reducing greenhouse gas emissions and maintaining jobs in the oil and gas sector", "Must prioritize reducing greenhouse gas emissions over the jobs in the oil and gas sector", "Must prioritize jobs in the oil and gas sector over reducing greenhouse gas emissions"];
    var CJ_court = ["A prime minister should always adhere to court decisions even though they might seem politicized", "A prime minister should not be bound by court decisions he or she regards as politicized"];
    var CJ_prot = ["The prime minister should not be allowed to ban non-violent opposition protests under any circumstances", "The prime minister should be free to ban non-violent opposition protests if they are disruptive to society"];

    // Fisher - Yates shuffle:
    function shuffle(array) {
        for (var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        return array;
    }

    // Shuffle a vector, choose the first entry:
    function shuffle_one(theArray) {
        var out = shuffle(theArray);
        return out[0];
    }

    var CJ_attribnames = ["Ethnicity", "Gender", "Age", "Party", "Previous occupation", "Prior political experience", "Position on welfare spending", "Position on environmental issues", "Position on courts", "Position on press freedom", "Position on protests rights"];

    function randomize(i) {
        // Randomly draw the attributes for each candidate
        var c1 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];
        var c2 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];

        
        
        // Save the results
        for (var k = 1; k <= CJ_attribnames.length; k++) {
            var CJ_index = k - 1;
            Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_name', CJ_attribnames[CJ_index]);
            Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c1', c1[CJ_index]);
            Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c2', c2[CJ_index]);
        }
    }

    for (var i = 1; i <= CJ_numChoice; i++) {
        randomize(i);
    }
});

你能帮我弄清楚如何确保两个候选人政党始终以以下方式不同:使候选人 1 的政党是随机的。如果候选人 1 是自由党或新民主党,那么候选人 2 只能是保守党。如果候选人 1 是保守党,候选人 2 可以随机是自由党或新民主党。

javascript arrays qualtrics
1个回答
0
投票

你必须稍微修改一下代码。最简单的解决方案是在

randomize()
函数的无限循环内添加 if 语句,检查双方是否不同,如果不同,则跳出循环。

function randomize(i) {
    var c1 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];

    var c2;

    var partyC1 = c1[3];
    do {
        c2 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];

        if (c2[3] !== partyC1) {
            break;
        }
    } while (true)

    // Save the results
    for (var k = 1; k <= CJ_attribnames.length; k++) {
        var CJ_index = k - 1;
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_name', CJ_attribnames[CJ_index]);
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c1', c1[CJ_index]);
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c2', c2[CJ_index]);
    }
}

对代码所做的修改如下:

  • 我添加了一个无限次运行的 do-while 循环
  • 创建c2
  • 检查 c2 的参与方是否与 c1 的参与方相同
  • 如果双方不同则中断,如果双方相同则循环重复
© www.soinside.com 2019 - 2024. All rights reserved.