Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

ExpressionScript to calculate result of mathematical formula

  • ryanswj
  • ryanswj's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 weeks ago #266335 by ryanswj
Please help us help you and fill where relevant:
Your LimeSurvey version: 6.6.1+240806
Own server or LimeSurvey hosting: Self-hosting
Survey theme/template: default
==================
Hello!

I would like LimeSurvey to automatically calculate the result of a mathematical formula as shown below, using ExpressionScript:
y = -0.0011x^3 + 0.061x^2 + 2.0253x + 1.1913, where x is the output of a previous question (let's call it Q1.value).

I have created a new Equation question, and have used the following code:

{sum((-0.011*(pow(Q1.value,3))),(0.061*(pow(Q1.value,2))),(2.0253*(Q1.value)),1.1913)}


but the output/evaluated expression is nonsensical (totally different from expected.

What am I missing here, or have I evaluated something wrongly?

Thank you!

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 weeks ago #266336 by holch
Did you try to break your formula down to see where things are going wrong? This way you can see what works and what doesn't.

Maybe it makes sense to have seperate equations for the bit and calculate each seperately and only join the bits at the end.

Another thing: Q1.value references the assessment value of a question, not the value someone has put into the box.this might be what you want to do, but it also might be wrong. So give us some more info. And ideally provide an lss file with just the relevant questions, so we can see what yo do and play around (copy the survey and delete stuff or create a new dummy survey. No lsq or lsg files please)

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 weeks ago #266337 by Joffm
I do not see any issue
Maybe the  wrong use of the property ".value".
As you read here
[url] manual.limesurvey.org/ExpressionScript_-...#Access_to_variables [/url]
the assessment value for the question if it is relevant (otherwise blank), or the text value if it is not a coded question

We do not know the source of your x (type of Q1)

 
Here you may use ".value" or not, no difference, as it is a not coded question.

But if the source of your x is a coded question, e.g. list(radio), you must not use it if you do not use assessment values.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • ryanswj
  • ryanswj's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 weeks ago #266338 by ryanswj
Hi everyone,

Thank you so much for your replies - it is greatly appreciated.

I have attached the .lss file here.

To answer the questions above - yes, I have set the assessment values and am using them in the calculation. But I am quite new to this and if there is a better way to do what I am trying to do, please let me know.

Thank you!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 weeks ago #266340 by Joffm
Hi, 
did you check what is stored in the database?
This
 
You see the text is stored also. And now the calculation of SASCAL must fail.
Remove the text.

Why do you use assessment values?
 
These are useful if there are negative weights or two or more answer options have the same weoght.
But here?
Much better
 

And remember.
Assessment values are nor stored in the database
If you later want to calculate statistical measures like mean, std.deviation, etc. you need numbers
A mean of "AO02", "AO03", "AO05" and "AO04" is really not "AO04.5"
You have to recode everything.

So use numerical codes in scales.

Joffm

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • ryanswj
  • ryanswj's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 weeks ago #266341 by ryanswj
Hi Joffm,

Thank you for your reply.

I have removed assessment mode and set the code to 1, 2, 3, 4, 5, etc as you suggested.

I have also removed all the text, it is still not working. The calculation of SASTTL is always correct, but SASCAL is always wrong.

Reattached the lss file for you again (after the changes made).

Thank you!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 weeks ago #266342 by Joffm
Hi,
I do not see any issue.
Here a comparison of the values calculated in Excel and the result in LimeSurvey.
BTW: I only rounded and removed not necessary brackets.
{round(sum(-0.011*pow(SASTTL,3),0.061*pow(SASTTL,2),2.0253*SASTTL,1.1913),4)}

 

Or is there an error in your formula?

If there is a difference, do the same and show your response table.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose