Me está costando mucho hacer factorial hasta el grado dentro de un script para poder asignar ciertos factores DOE a algún factorial.
Puedo obtener el primer nivel con nchoosekMatrix() pero no puedo entender cómo obtener dinámicamente niveles superiores a 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} )
}
*/
Creo que debería hacer algo de forma recursiva, pero me cuesta mucho hacer que algo funcione.
Cualquier ayuda sería muy apreciada.
Me di cuenta de que mis expectativas no eran las correctas. Esto realmente hace lo mismo que lo que hace la macro del modelo de ajuste.
Sin embargo, me gusta más el siguiente enfoque iterativo.
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);
);