Hi, I have an error in my model when I am trying to SUM a subscripted variable.
The attachment model.png shows the relevant part of the model structure.
The 'unscheduled procedures' variable in green is a data variable with a value for 2018 and 2022 imported via Model>>Import Dataset and it appears in the Advanced tab of the simulation control panel as a data source titled nonelective.vdfx
unscheduled procedures is arrayed by Trust,age,Unscheduled Primary Operation
The flow 'unscheduled entrances to cath lab' simply takes this as an input for the equation, and produces the results as in the attached file data2.png
When I try to sum the total across the array, using the equation: total unscheduled = SUM(unscheduled entrances to cath lab[Trust!, age!, Unscheduled Primary Operation!]) the model returns rubbish as displayed in the attachment data.png.
Whereas I can easily write a successful equation such as other_variable*unscheduled entrances to cath lab[Trust, age, unscheduled primary operation]
I've never had difficulty summing something before, is it perhaps related to how I've loaded the data?
Thank you,
Sarah
summing subscripts
-
- Junior Member
- Posts: 16
- Joined: Mon May 08, 2023 8:34 am
- Vensim version: PRO
-
- Super Administrator
- Posts: 4827
- Joined: Wed Mar 05, 2003 3:10 am
Re: summing subscripts
It's probably best to test that the data variable is not :NA: when summing.
Something like
Sum of data variable = sum ( if then else ( data variable[subscript!] <> :NA: data variable[subscript!], 0 ) )
Something like
Sum of data variable = sum ( if then else ( data variable[subscript!] <> :NA: data variable[subscript!], 0 ) )
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: summing subscripts
There are two ways to get an :NA: value:
- the data series has a missing point, and the interpolation type is :RAW:
- the data is completely absent for the variable/element
Since the first is not the default, the problem likely arises from the second.
If you have a data model doing preprocessing, you can test for the missing value and add a 0 at initial time, or something like that, if you want to avoid the IF THEN ELSE test inside the summations. \
Another option is to use the X IF MISSING function to substitute missing values on the fly.
- the data series has a missing point, and the interpolation type is :RAW:
- the data is completely absent for the variable/element
Since the first is not the default, the problem likely arises from the second.
If you have a data model doing preprocessing, you can test for the missing value and add a 0 at initial time, or something like that, if you want to avoid the IF THEN ELSE test inside the summations. \
Another option is to use the X IF MISSING function to substitute missing values on the fly.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
-
- Junior Member
- Posts: 16
- Joined: Mon May 08, 2023 8:34 am
- Vensim version: PRO
Re: summing subscripts
OK, so there are no missing values in the data (only 0's).
And I thought that, given it already flows through another equation, that the third variable (total) would be able to take the values from the second variable (flow equation), so as long as the flow equation is working, the data variable is properly incorporated the model from there.
I am wondering if the reason is that this equation is actually a subrange of a subscript, and if I haven't mapped that properly yet (e.g. I still need to change the default of 'Primary Operation' to 'Unscheduled Primary Operation'... basically, if there are problems with my subscript mapping, that's why the :NA is occurring.
?
And I thought that, given it already flows through another equation, that the third variable (total) would be able to take the values from the second variable (flow equation), so as long as the flow equation is working, the data variable is properly incorporated the model from there.
I am wondering if the reason is that this equation is actually a subrange of a subscript, and if I haven't mapped that properly yet (e.g. I still need to change the default of 'Primary Operation' to 'Unscheduled Primary Operation'... basically, if there are problems with my subscript mapping, that's why the :NA is occurring.
?
Re: summing subscripts
There isn't anything special about subranges that should cause an :NA: value to occur.
However, if the dimensions of your imported dataset aren't completely populated, there will be :NA: values in the model that you can't see in the source data. For example, if you have
sub : a,b,c
and the data defines x[ a ] and x[ b ] but not x[ c ], the model will use x[c]=:NA:. You could check for this by inspecting a table of x[] or exporting the model run to csv/xls/tab.
However, if the dimensions of your imported dataset aren't completely populated, there will be :NA: values in the model that you can't see in the source data. For example, if you have
sub : a,b,c
and the data defines x[ a ] and x[ b ] but not x[ c ], the model will use x[c]=:NA:. You could check for this by inspecting a table of x[] or exporting the model run to csv/xls/tab.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/