nested statment in expression


Author
Message
Kugel
Kugel
Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)
Group: Awaiting Activation
Posts: 4, Visits: 24
Hi, 

I am relatively new to inquisit and have started to create some experiments as well as surveys. In one of the surveys, I am measuring BMI. Due to the fact that there are differences across gender when it comes to categorizing people based on their BMI, I wanted to use the raw BMI score in order to assign people to different weight groups conditional on sex. I have used a nested statement within an expression in order to do so. As long as I don't include this newly created expression within my summary data everything is fine, but once I enter it, inquisit shuts down at the end of the survey after all survey items have been filled in and  the finish button has been pressed. Strangely, I am able to run the survey, and I don't get any kind of error messages. This makes it hard for me as a novice to figure out what exactly is wrong with my code. Since I don´t get any summary files for these runs, I am absolutely sure that the problem arises due to adding this expression to the summary data. 

This is the code for the expression: 
/ BMICAT = {(if radiobuttons.Geschlecht.response == 1)
{
if (expressions.BMI < 20)
{
expressions.BMICAT == 1;
}
else if (expressions.BMI >= 20 && expressions.BMI < 25)
{
expressions.BMICAT == 2;
}
else if (expressions.BMI >= 25 && expressions.BMI <30)
{
expressions.BMICAT == 3;
}
else if (expressions.BMI >= 30 && expressions.BMI < 35)
{
expressions.BMICAT == 4;
}
else if (expressions.BMI >= 35 && expressions.BMI < 40)
{
expressions.BMICAT == 5;
}
else if (expressions.BMI >= 40)
{
expressions.BMICAT == 6;
}
}
}

This is just the code for males (thus having answered radiobuttons.Geschlecht with response 1). 

Hope that somebody can help me out and tell me what exactly went wrong, so that I can adapt the code. 

Cheers, 

Kugel
Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 13K, Visits: 104K
Kugel - Wednesday, March 22, 2017
Hi, 

I am relatively new to inquisit and have started to create some experiments as well as surveys. In one of the surveys, I am measuring BMI. Due to the fact that there are differences across gender when it comes to categorizing people based on their BMI, I wanted to use the raw BMI score in order to assign people to different weight groups conditional on sex. I have used a nested statement within an expression in order to do so. As long as I don't include this newly created expression within my summary data everything is fine, but once I enter it, inquisit shuts down at the end of the survey after all survey items have been filled in and  the finish button has been pressed. Strangely, I am able to run the survey, and I don't get any kind of error messages. This makes it hard for me as a novice to figure out what exactly is wrong with my code. Since I don´t get any summary files for these runs, I am absolutely sure that the problem arises due to adding this expression to the summary data. 

This is the code for the expression: 
/ BMICAT = {(if radiobuttons.Geschlecht.response == 1)
{
if (expressions.BMI < 20)
{
expressions.BMICAT == 1;
}
else if (expressions.BMI >= 20 && expressions.BMI < 25)
{
expressions.BMICAT == 2;
}
else if (expressions.BMI >= 25 && expressions.BMI <30)
{
expressions.BMICAT == 3;
}
else if (expressions.BMI >= 30 && expressions.BMI < 35)
{
expressions.BMICAT == 4;
}
else if (expressions.BMI >= 35 && expressions.BMI < 40)
{
expressions.BMICAT == 5;
}
else if (expressions.BMI >= 40)
{
expressions.BMICAT == 6;
}
}
}

This is just the code for males (thus having answered radiobuttons.Geschlecht with response 1). 

Hope that somebody can help me out and tell me what exactly went wrong, so that I can adapt the code. 

Cheers, 

Kugel

expressions.BMICAT == 1;

#1: == would be the wrong operator. == is a logical operator, = would be the assignment operator.
#2: You are trying to re-write expressions.bmicat itself.

Barring other mistakes in any of the other expressions you did not include, the correct formulation would be:

<values>
/ rand_BMI = round(rand(1,50))
</values>


<expressions>
/ BMI = values.rand_bmi
/ BMICAT = if(radiobuttons.Geschlecht.response == 1)
    {
    if (expressions.BMI < 20) 1;
    else if (expressions.BMI >= 20 && expressions.BMI < 25) 2;
    else if (expressions.BMI >= 25 && expressions.BMI <30) 3;
    else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
    else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
    else if (expressions.BMI >= 40) 6;
    }
</expressions>

<surveypage mypage>
/ caption = "Randomly generated BMI for testing purposes: <%values.rand_bmi%>"
/ questions = [1=geschlecht]
</surveypage>

<radiobuttons geschlecht>
/ options = ("male", "female")
/ optionvalues = ("1", "2")
</radiobuttons>

<survey mysurvey>
/ pages = [1=mypage]
</survey>

<summarydata>
/ columns = [script.subjectid radiobuttons.geschlecht.response values.rand_bmi expressions.bmicat]
/ separatefiles = true
</summarydata>


Kugel
Kugel
Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)
Group: Awaiting Activation
Posts: 4, Visits: 24
Thanks a lot Dave! Your reply was very helpful and it works fine. However, for females (radiobuttons.Geschlecht.response == 2) it does not work. Here the computation of BMI is executed as it is supposed to be, but BMICAT returns a blank in the summary data all the times. It seems as though the second part of the "if... else if... statement is simply not recognized. Apparently, I am missing out on some structuring parentheses. I have tried a bunch of different options for setting these additional parentheses, but none of them worked. In some of the options, the pattern reversed, so that for females everything was fine, but it did no longer work for men. I also looked on the internet and the forum here for similar cases, but could find none. 
Long story short: It would be really nice if you could help me out again, Dave;)! It would be fantastic to understand this basic syntax of inquisit code in order to use it in future projects. I will paste the code below. I also thought about using two different expressions (one for males and one for females), but having all in one expression would be much more elegant. 

  / BMI = {textbox.gewicht.response / pow (textbox.größe.response, 2)
}
/ BMICAT = if (radiobuttons.Geschlecht.response == 1)
                {
                if (expressions.BMI < 20) 1;
                else if (expressions.BMI >= 20 && expressions.BMI < 25) 2;
                else if (expressions.BMI >= 25 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }
else if (radiobuttons.Geschlecht.response == 2)
                {
                if (expressions.BMI < 19) 1;
                else if (expressions.BMI >= 19 && expressions.BMI < 24) 2;
                else if (expressions.BMI >= 24 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }

Cheers, 
Kugel


Dave
Dave
Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)Supreme Being (1M reputation)
Group: Administrators
Posts: 13K, Visits: 104K
Kugel - Thursday, March 23, 2017
Thanks a lot Dave! Your reply was very helpful and it works fine. However, for females (radiobuttons.Geschlecht.response == 2) it does not work. Here the computation of BMI is executed as it is supposed to be, but BMICAT returns a blank in the summary data all the times. It seems as though the second part of the "if... else if... statement is simply not recognized. Apparently, I am missing out on some structuring parentheses. I have tried a bunch of different options for setting these additional parentheses, but none of them worked. In some of the options, the pattern reversed, so that for females everything was fine, but it did no longer work for men. I also looked on the internet and the forum here for similar cases, but could find none. 
Long story short: It would be really nice if you could help me out again, Dave;)! It would be fantastic to understand this basic syntax of inquisit code in order to use it in future projects. I will paste the code below. I also thought about using two different expressions (one for males and one for females), but having all in one expression would be much more elegant. 

  / BMI = {textbox.gewicht.response / pow (textbox.größe.response, 2)
}
/ BMICAT = if (radiobuttons.Geschlecht.response == 1)
                {
                if (expressions.BMI < 20) 1;
                else if (expressions.BMI >= 20 && expressions.BMI < 25) 2;
                else if (expressions.BMI >= 25 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }
else if (radiobuttons.Geschlecht.response == 2)
                {
                if (expressions.BMI < 19) 1;
                else if (expressions.BMI >= 19 && expressions.BMI < 24) 2;
                else if (expressions.BMI >= 24 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }

Cheers, 
Kugel


The expression should simply read:


<expressions>
/ BMI = textbox.bmi_test.response
/ BMICAT = if (radiobuttons.Geschlecht.response == 1)
                {
                if (expressions.BMI < 20) 1;
                else if (expressions.BMI >= 20 && expressions.BMI < 25) 2;
                else if (expressions.BMI >= 25 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                };
if (radiobuttons.Geschlecht.response == 2)
                {
                if (expressions.BMI < 19) 1;
                else if (expressions.BMI >= 19 && expressions.BMI < 24) 2;
                else if (expressions.BMI >= 24 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }

</expressions>

<surveypage mypage>
/ questions = [1=bmi_test; 2=geschlecht]
</surveypage>

<textbox bmi_test>
/ caption = "Enter a BMI value for testing purposes"
/ mask = positiveinteger
</textbox>

<radiobuttons geschlecht>
/ options = ("male", "female")
/ optionvalues = ("1", "2")
</radiobuttons>

<survey mysurvey>
/ pages = [1=mypage]
</survey>

<summarydata>
/ columns = [script.subjectid radiobuttons.geschlecht.response expressions.bmi expressions.bmicat]
/ separatefiles = true
</summarydata>

Kugel
Kugel
Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)Associate Member (264 reputation)
Group: Awaiting Activation
Posts: 4, Visits: 24
Dave - Thursday, March 23, 2017
Kugel - Thursday, March 23, 2017
Thanks a lot Dave! Your reply was very helpful and it works fine. However, for females (radiobuttons.Geschlecht.response == 2) it does not work. Here the computation of BMI is executed as it is supposed to be, but BMICAT returns a blank in the summary data all the times. It seems as though the second part of the "if... else if... statement is simply not recognized. Apparently, I am missing out on some structuring parentheses. I have tried a bunch of different options for setting these additional parentheses, but none of them worked. In some of the options, the pattern reversed, so that for females everything was fine, but it did no longer work for men. I also looked on the internet and the forum here for similar cases, but could find none. 
Long story short: It would be really nice if you could help me out again, Dave;)! It would be fantastic to understand this basic syntax of inquisit code in order to use it in future projects. I will paste the code below. I also thought about using two different expressions (one for males and one for females), but having all in one expression would be much more elegant. 

  / BMI = {textbox.gewicht.response / pow (textbox.größe.response, 2)
}
/ BMICAT = if (radiobuttons.Geschlecht.response == 1)
                {
                if (expressions.BMI < 20) 1;
                else if (expressions.BMI >= 20 && expressions.BMI < 25) 2;
                else if (expressions.BMI >= 25 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }
else if (radiobuttons.Geschlecht.response == 2)
                {
                if (expressions.BMI < 19) 1;
                else if (expressions.BMI >= 19 && expressions.BMI < 24) 2;
                else if (expressions.BMI >= 24 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }

Cheers, 
Kugel


The expression should simply read:


<expressions>
/ BMI = textbox.bmi_test.response
/ BMICAT = if (radiobuttons.Geschlecht.response == 1)
                {
                if (expressions.BMI < 20) 1;
                else if (expressions.BMI >= 20 && expressions.BMI < 25) 2;
                else if (expressions.BMI >= 25 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                };
if (radiobuttons.Geschlecht.response == 2)
                {
                if (expressions.BMI < 19) 1;
                else if (expressions.BMI >= 19 && expressions.BMI < 24) 2;
                else if (expressions.BMI >= 24 && expressions.BMI < 30) 3;
                else if (expressions.BMI >= 30 && expressions.BMI < 35) 4;
                else if (expressions.BMI >= 35 && expressions.BMI < 40) 5;
                else if (expressions.BMI >= 40) 6;
                }

</expressions>

<surveypage mypage>
/ questions = [1=bmi_test; 2=geschlecht]
</surveypage>

<textbox bmi_test>
/ caption = "Enter a BMI value for testing purposes"
/ mask = positiveinteger
</textbox>

<radiobuttons geschlecht>
/ options = ("male", "female")
/ optionvalues = ("1", "2")
</radiobuttons>

<survey mysurvey>
/ pages = [1=mypage]
</survey>

<summarydata>
/ columns = [script.subjectid radiobuttons.geschlecht.response expressions.bmi expressions.bmicat]
/ separatefiles = true
</summarydata>

Thanks a lot for your help Dave!!
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Reading This Topic

Explore
Messages
Mentions
Search