Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Re: factorial to degree script

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 22, 2019 7:56 PM
(1797 views)

I'm having a hard time making factorial to degree inside of a script in order to assign certain DOE factors to some factorial.

I can get the first level with nchoosekMatrix() but can't figure out how to dynamically get levels higher than 1.

```
Names default to here(1);
factorial_to_degree = function({factors, degree = 2, termlist = {}},
{DEFAULT LOCAL},
if(degree!=1,
termlist = recurse(factors, degree-1, termlist);
);
nckmat = nchoosekmatrix(nrows(factors), degree);
for(r = 1, r<=nrows(nckmat), r++,
AddTermExpr = Expr(AddTerm());
for(c=1, c<=ncols(nckmat), c++,
insert into(AddTermExpr, Evallist({factors[nckmat[r, c]], 1}));
);
insert into(termlist, nameexpr(AddTermExpr));
);
return(termlist);
);
factorial_to_degree([1, 2, 3], 2);
//returns
/*
{AddTerm( {1, 1} ), AddTerm( {2, 1} ), AddTerm( {3, 1} ), AddTerm( {1, 1}, {2, 1} ),
AddTerm( {1, 1}, {3, 1} ), AddTerm( {2, 1}, {3, 1} )}
*/
//want it to return
/*
{
AddTerm( {1, 1} ), AddTerm( {2, 1} ), AddTerm( {3, 1} ),
AddTerm( {1, 2} ), AddTerm( {2, 2} ), AddTerm( {3, 2} ),
AddTerm( {1, 1}, {2, 1} ), AddTerm( {1, 1}, {3, 1} ), AddTerm( {2, 1}, {3, 1} )
}
*/
```

I think I should be doing something recursively but I'm having a hard time actually getting anything to work.

Any help would greatly be appreciated.

Vince Faller - Predictum

- Tags:
- Factorial

1 ACCEPTED SOLUTION

Accepted Solutions

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

I realized that my expectations were not right. This actually does the same thing as what fit model macro does.

I like the following iterative approach more though.

```
factorial_to_degree = function({factors, degree},
n_factors = nitems(factors);
factors = shape(factors, n_factors);
termlist = {};
for(i=1, i<=degree, i++,
nckmat = nchoosekmatrix(n_factors, i);
for(r = 1, r<=nrows(nckmat), r++,
AddTermExpr = Expr(AddTerm());
for(c=1, c<=ncols(nckmat), c++,
insert into(AddTermExpr, Evallist({factors[nckmat[r, c]], 1}));
);
insert into(termlist, nameexpr(AddTermExpr));
);
);
return(termlist);
);
```

Vince Faller - Predictum

1 REPLY 1

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

I realized that my expectations were not right. This actually does the same thing as what fit model macro does.

I like the following iterative approach more though.

```
factorial_to_degree = function({factors, degree},
n_factors = nitems(factors);
factors = shape(factors, n_factors);
termlist = {};
for(i=1, i<=degree, i++,
nckmat = nchoosekmatrix(n_factors, i);
for(r = 1, r<=nrows(nckmat), r++,
AddTermExpr = Expr(AddTerm());
for(c=1, c<=ncols(nckmat), c++,
insert into(AddTermExpr, Evallist({factors[nckmat[r, c]], 1}));
);
insert into(termlist, nameexpr(AddTermExpr));
);
);
return(termlist);
);
```

Vince Faller - Predictum