cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
tMinnx
Level III

Col Cumulative Sum without calling the current row's value

Hello, I am quite new to writing JMP scripts and primarily use "Edit Formula" for my work. I have been browsing solutions related to Col Cumulative Sum and could not find one for my specific problem. The simplified version of my data table is as follows.

 

RowWeekCol Cumulative SumMy Wish
1110
2231
3363
44106
551510
662115

 

The formula below works if the current row value of "Week" is fixed or if I have two separate columns "Week" and "My Wish."

 

 

If( Row() != 1,
	Lag( Col Cumulative Sum( :Week ), 1 ),
	0
)

 

The issue is that the value of the previous row's "Week" is fixed but the current row's is not. And I only have one column "Week" to work with. I am developing a formula that simulates progression based on the previous simulation value. I am simulating the current row's "Week" value based on the previous row's.

 

Suppose I am working on Row 4 in the column "Week." I want to cumulatively sum all the previous "Week" values without calling the current "Week" value in the function. Otherwise, it creates an illegal reference or cycling problem in my case.

 

I tried using subscript instead of lag since the lag function calls the current row's "Week" value. It did not work.

 

If( Row() != 1,
	Col Cumulative Sum( :Week )[Row() - 1],
	0
)

I hope my explanation was clear enough. Thank you for your time and expertise in advance. I use JMP Pro 17.

 

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: Col Cumulative Sum without calling the current row's value

Is "Sim Cases" a matrix or a column in a data table?

 

I do not understand 

You appear to be making your solution overly complex.

sim cases = [3,5,2,8];

sum1 = sum(sim cases);
sum2 = sum(([1 1 1 1]*sim cases)[1]);
show(sum1,sum2);

Both sum1 and sum2 provide the same value

sum1 = 18;
sum2 = 18;

I encourage you to read the Discovering JMP document available under the Help pull down menu.

Jim

View solution in original post

txnelson
Super User

Re: Col Cumulative Sum without calling the current row's value

Here is my solution, however, it does not match your solution.  I believe your calculations as shown are incorrect for Row 3. See my script, which includes your sample data table.

Names Default To Here( 1 );
dt = New Table( "Example",
	Add Rows( 4 ),
	New Column( "R",
		Numeric,
		"Continuous",
		Set Values( [., 1.4, 1.67, 2.3] )
	),
	New Column( "Cases",
		Expression,
		"None",
		Set Values( {[1, 1, 2, 1], Empty(), Empty(), Empty()} )
	)
);

// The calculation script
casesSum = 0;
For(i=2,i<=nrows(dt),i++,
		Eval( Eval Expr( casesMatrix = Expr( :cases[i - 1] ) ) );
		:R[i] = :R[i] * (1 - (Sum( casesMatrix )+casesSum) * 4 / 100);
		:Cases[i] = casesMatrix * :R[i];
		casesSum = casesSum + sum(casesMatrix);
);


// Row 3 R does not seem to be correct
// I can not come up with .71, I come up with .96192
show(1.4 * (1 - (5 * 4/100)) ); 
show(1.67 * (1 - (10.6 * 4/100)) ); 



txnelson_0-1711546109146.png

 

Jim

View solution in original post

15 REPLIES 15
jthi
Super User

Re: Col Cumulative Sum without calling the current row's value

You can deduct current weeks value from the cumulative sum

Col Cumulative Sum(:Week) - :Week

jthi_0-1711447034404.png

 

-Jarmo
tMinnx
Level III

Re: Col Cumulative Sum without calling the current row's value

The moment I use (:Week) in my formula I get an illegal reference problem. The current row (:Week) has not been fully calculated yet. I can only recall (:Week) from the previous row in theory.

jthi
Super User

Re: Col Cumulative Sum without calling the current row's value

I would use separate columns to avoid all sorts of issues, but I'm not exactly sure what you are trying to achieve as you have full data in your example. Maybe something like this if Col Cumulative Sum doesn't work for you (calculate value using Lag and variable)

 

If(Row() == 1,
	val = 0,
	val = val + Lag(:Week);
);
val;

 

 

-Jarmo
tMinnx
Level III

Re: Col Cumulative Sum without calling the current row's value

Yes, at first, I tried to emulate in the JMP data table what I usually do in Excel spreadsheets by separating columns. I would then call the values of the previous row of a different column. The JMP data table does not behave as Excel spreadsheets, and I am encountering cyclical problems.

 

So, I keep changing strategies and my current understanding is to keep every required variable in one column to avoid cyclical problems.

txnelson
Super User

Re: Col Cumulative Sum without calling the current row's value

If I am understanding correctly, I think this might be the solution

If( Row() != 1,
	Sum( :Week[Index( 1, Row() - 1 )] ),
	0
)
Jim
tMinnx
Level III

Re: Col Cumulative Sum without calling the current row's value

Thank you for your suggestion on using the Index subscript. I now encounter the issue "object not subscriptable."

 

My week example was over-simplified and I have a matrix Sim Cases of 4 rows and 1 column in place of week. To transform it into one summarized numerical value, I multiplied the matrix with [1 1 1 1] and added a subscript [1]. It was then followed by the Index subscript.

 

Sum( ([1 1 1 1] * :Sim Cases)[1][Index( 1, Row() - 1 )] )

I think the resulting numerical value from the original 4x1 matrix is not accepting any subscript after [1]. Or it could be because of the illegal reference (recursion) again.

txnelson
Super User

Re: Col Cumulative Sum without calling the current row's value

Is "Sim Cases" a matrix or a column in a data table?

 

I do not understand 

You appear to be making your solution overly complex.

sim cases = [3,5,2,8];

sum1 = sum(sim cases);
sum2 = sum(([1 1 1 1]*sim cases)[1]);
show(sum1,sum2);

Both sum1 and sum2 provide the same value

sum1 = 18;
sum2 = 18;

I encourage you to read the Discovering JMP document available under the Help pull down menu.

Jim
tMinnx
Level III

Re: Col Cumulative Sum without calling the current row's value

I will keep learning JMP. For now, I will just stick to my Excel spreadsheet. Thanks everyone for your responses.

 

Sim Cases is a column and I am using Sim Cases of previous rows in creating new rows. So I guess it is both a column and a variable inside that same column.

jthi
Super User

Re: Col Cumulative Sum without calling the current row's value

How JMP tables and spreadsheets behave are totally different and this old post might be a good read JMP is Not a Spreadsheet as it provides some examples and explanation. You can do most of the stuff in JMP but it can take some time to get your head around how columns+rows behave in JMP vs single cell in excel.

-Jarmo