我很難在腳本內進行階乘,以便將某些 DOE 因子分配給某些階乘。
我可以使用 nchoosekMatrix() 來獲得第一個級別,但無法弄清楚如何動態獲得高於 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} )
}
*/
我認為我應該遞歸地做一些事情,但我很難真正讓任何事情發揮作用。
任何幫助將不勝感激。
我意識到我的期望是不正確的。這實際上與擬合模型宏所做的相同。
不過,我更喜歡以下迭代方法。
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);
);