Qualtrics联合实验JavaScript政党属性约束随机化需求
问题与需求
我正在Qualtrics平台使用JavaScript构建一项关于投票选择与民主规范违反的联合实验(conjoint experiment)。常规联合实验会随机所有属性水平,但选举场景中每个政党仅对应一位候选人,这种全随机方式不符合现实。
需要调整代码实现以下政党随机规则:
- 候选人1的政党随机生成
- 若候选人1为Liberal或NDP,候选人2只能是Conservative
- 若候选人1为Conservative,候选人2可随机选择Liberal或NDP
- 确保两位候选人的政党始终不同
现有代码
// 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); } });
修改后的代码
// 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) { // 先随机生成候选人1的政党 var c1Party = shuffle_one(CJ_pid); // 根据规则生成候选人2的政党 var c2Party; if (c1Party === "Liberal" || c1Party === "NDP") { c2Party = "Conservative"; } else if (c1Party === "Conservative") { // 随机从Liberal和NDP中选一个 c2Party = shuffle_one(["Liberal", "NDP"]); } // 生成其他属性,单独替换政党值 var c1 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), c1Party, 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), c2Party, 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); } });
关键修改说明
- 单独提取政党属性的生成逻辑,不再使用通用的
shuffle_one(CJ_pid)同时生成两个候选人的政党 - 先随机候选人1的政党,再根据设定规则生成候选人2的政党:
- 若候选人1是Liberal或NDP,候选人2固定为Conservative
- 若候选人1是Conservative,从Liberal和NDP中随机选取一个作为候选人2的政党
- 将生成好的政党值替换到c1和c2数组的对应位置(数组索引为3,对应
CJ_attribnames中的"Party")
内容的提问来源于stack exchange,提问作者Thom_G
相关产品推荐
相关产品推荐

