0 votes
in SoSci Survey (English) by s291395 (175 points)

Hi,

I'm running a survey in nine countries and want to use a quota to balance age and gender. I have written the following test syntax:

Quota values are saved to the internal variable on the previous page (will move to last page when I know it works). I create 72 quotas (9 countries 2 gender 4 age groups) of 250 respondents each. I read the respondent values and combine to a variable $demgroup.

//*****************************************************
// Quota redirect page
//*****************************************************


$quota = [
  '1-1-1' => 250, '1-2-1' => 250, //
  '2-1-1' => 250, '2-2-1' => 250, // 
  '3-1-1' => 250, '3-2-1' => 250, //
  '4-1-1' => 250, '4-2-1' => 250, // 
  '1-1-2' => 250, '1-2-2' => 250, // 
  '2-1-2' => 250, '2-2-2' => 250, // 
  '3-1-2' => 250, '3-2-2' => 250, // 
  '4-1-2' => 250, '4-2-2' => 250, // 
  '1-1-3' => 250, '1-2-3' => 250, // 
  '2-1-3' => 250, '2-2-3' => 250, // 
  '3-1-3' => 250, '3-2-3' => 250, // 
  '4-1-3' => 250, '4-2-3' => 250, // 
  '1-1-4' => 250, '1-2-4' => 250, // 
  '2-1-4' => 250, '2-2-4' => 250, // 
  '3-1-4' => 250, '3-2-4' => 250, // 
  '4-1-4' => 250, '4-2-4' => 250, // 
  '1-1-5' => 250, '1-2-5' => 250, // 
  '2-1-5' => 250, '2-2-5' => 250, // 
  '3-1-5' => 250, '3-2-5' => 250, // 
  '4-1-5' => 250, '4-2-5' => 250, // 
  '1-1-6' => 250, '1-2-6' => 250, // 
  '2-1-6' => 250, '2-2-6' => 250, // 
  '3-1-6' => 250, '3-2-6' => 250, // 
  '4-1-6' => 250, '4-2-6' => 250, // 
  '1-1-7' => 250, '1-2-7' => 250, // 
  '2-1-7' => 250, '2-2-7' => 250, // 
  '3-1-7' => 250, '3-2-7' => 250, // 
  '4-1-7' => 250, '4-2-7' => 250, // 
  '1-1-8' => 250, '1-2-8' => 250, // 
  '2-1-8' => 250, '2-2-8' => 250, // 
  '3-1-8' => 250, '3-2-8' => 250, // 
  '4-1-8' => 250, '4-2-8' => 250, // 
  '1-1-9' => 250, '1-2-9' => 250, // 
  '2-1-9' => 250, '2-2-9' => 250, // 
  '3-1-9' => 250, '3-2-9' => 250, // 
  '4-1-9' => 250, '4-2-9' => 250, // 
];


// Gender is read out directly
$gender = value('SD05');
debug($gender);

if ($gender == 1) {
  $genderGroup = 1;
} elseif ($gender == 2) {
  $genderGroup = 2;
} else {
  $genderGroup = 0;
}

debug($genderGroup);

// The age is recoded
$age = 2025 - value('SD04_01');
debug($age);

if ($age < 18) {
  $ageGroup = 0;
} elseif ($age <= 30) {
  $ageGroup = 1;
} elseif ($age <= 50) {
  $ageGroup = 2;
} elseif ($age <= 68) {
  $ageGroup = 3;
} else {
  $ageGroup = 4;
}

debug($ageGroup);


if (getLanguage() == 'eng') {
  $countryGroup = 1;
} else if  (getLanguage() == 'cze') {
  $countryGroup = 2;
} else if  (getLanguage() == 'hun') {
  $countryGroup = 3;
} else if  (getLanguage() == 'spa') {
  $countryGroup = 4;
} else if  (getLanguage() == 'dut') {
  $countryGroup = 5;
} else if  (getLanguage() == 'fre') {
  $countryGroup = 5;
} else if  (getLanguage() == 'ger') {
  $countryGroup = 6;
} else if  (getLanguage() == 'qaa') {
  $countryGroup = 7;
} else if  (getLanguage() == 'qab') {
  $countryGroup = 8;
} else if  (getLanguage() == 'gsw ') {
  $countryGroup = 8;
} else if  (getLanguage() == 'qac') {
  $countryGroup = 9;
};
debug($countryGroup);

$demGroup = $ageGroup.'-'.$genderGroup.'-'.$countryGroup ;
debug($demGroup);
 

/////////////////////////////////////

// Retrieval of the available cases for this characteristic
$cases = statistic('count', 'QU01_04', $demGroup, true);

debug($cases);


// screenout
if (!array_key_exists($demGroup, $quota)) {
  redirect('https://www.');
  html('<p>Redirect Screenout</p>');
  pageStop();
}
 
// Reading the quota for the demographic group
$maxPerGroup = $quota[$demGroup];
 
debug($maxPerGroup);

// quota stop
if ($cases >= $maxPerGroup) {
  //redirect('https://www.');
  html('<p>Redirect Quota stop</p>');
  pageStop();
}

However, when I try to read out all the quota-case values as an array as below it does not show it correctly in the debug, and trying to print the array as a string in HTML code is not possible ("There is an error in the PHP code: Questionnaire Error: Array to string conversion").

$casesarray = statistic('frequencies', 'QU01_04', true); // get all case values
debug($casesarray);

What would be a good way to see the current number of respondents in all quota groups?

by SoSci Survey (337k points)
> Questionnaire Error: Array to string conversion

This shall not occur, as debug() can handle arrays ... are you working on www.soscisurvey.de? When (which time) did that error show up? I do not find the proper entry in the logfiles to dig deeper, at the moment.

However, to understand your sampling strategy: Are you actually planning to recruit 250 respondents in each of the 72 demographic cells? That would be quite a large sample...

> What would be a good way to see the current number of respondents in all quota groups?

You could download the data and compute the frequencies. However, I would prefer debug() working properly...

1 Answer

0 votes
by s291395 (175 points)

Thanks for the quick response. I was unclear in my question, the error does not stem from from debug() but from me trying to print the array

html('<p> The current quota group has'.
  $cases.'interviews. All quota groups have the following number '.
  $casesarray.' .</p>');

Leading to this error

However as shown in this picture the 2-1-1 cell has 11 cases, but the array only prints one case in cell 3-2-1. I thought it had to do with me running debug mode therefore i added true to statistic()

$casesarray = statistic('frequencies', 'QU01_04', true); // get all case values

Hope that is clearer!

by SoSci Survey (337k points)
Well, according to debug() it seems that only one value is in the array. What does a look into the variable in the dataset say?

If you want to display the array with the keys, you may want to try this:

foreach ($casesarray as $group => $count) {
    html('<p>'.$group.': '.$count.'</p>');
}
by s291395 (175 points)
The internal variable 'QU01_04' contains many more values, however the 3-2-1 value is in the only interview with finished=1. It seems statistic() only counts finished interviews even though I specify AllData as true. Is there a way to get the frequencies of also non-finished interviews?
by SoSci Survey (337k points)
Now, that you ask more specifically ... the "true" must be the fourth parameter:

$casesarray = statistic('frequencies', 'QU01_04', null, true);

see https://www.soscisurvey.de/help/doku.php/de:create:functions:statistic
by s291395 (175 points)
That solved that problem, thanks!

I'm trying to create an efficient way to show the current quota values to a panel company. I used php code now with html(), using the array and key values as below:


html('<p> The current values for quota groups'.'<br>'.
    'United Kingdom <br>'.
    'Age group 1 <br>'.
    'Men: '.'1-1-1'.' : '.$fullArray['1-1-1'].'/250<br>'.
    'Women: '.'1-2-1'.' : '.$fullArray['1-2-1'].'/250<br>'.
    'Age group 2 <br>'.
    'Men: '.'2-1-1'.' : '.$fullArray['2-1-1'].'/250<br>'.
    'Women: '.'2-2-1'.' : '.$fullArray['2-2-1'].'/250<br>'.
    'Age group 3 <br>'.
    'Men: '.'3-1-1'.' : '.$fullArray['3-1-1'].'/250<br>'.
    'Women: '.'3-2-1'.' : '.$fullArray['3-2-1'].'/250<br>'.
    'Age group 4 <br>'.
    'Men: '.'4-1-1'.' : '.$fullArray['4-1-1'].'/250<br>'.
    'Women: '.'4-2-1'.' : '.$fullArray['4-2-1'].'/250<br>'.
etc etc

However, It would be easier to read as a table. Can I reference the array in a text element or would I need to assing 72 placeholders? I have a hard time seeing a way at the wiki on text elements https://www.soscisurvey.de/help/doku.php/en:create:texts
by SoSci Survey (337k points)
Well, you can use nested FOR-loops to loop through the 3 different dimensions. It's then your choice how to package the output in, for example, a table. The outer loop may, for example, write a header and start a table, the second loop could start a table row via <tr>, and the third loop would add the cells via <td>.
by s291395 (175 points)
Thank you, I will try that!

Willkommen im Online-Support von SoSci Survey.

Hier bekommen Sie schnelle und fundierte Antworten von anderen Projektleitern und direkt von SoSci Survey.

→ Eine Frage stellen


Welcome to the SoSci Survey online support.

Simply ask a question to quickly get answers from other professionals, and directly from SoSci Survey.

→ Ask a Question

...