创建具有 byVar 的统计自定义(公式)函数与我创建的这个愿望清单项目相关:添加归一化和稳健的统计函数(和矩阵函数) . 我想我现在可以尝试将它们编写为自定义函数(即使我知道不可能共享它们并为每个人更新它们),但我几乎立即遇到了我的第一个问题:如何引用列...... 使用自定义公式时,我是否坚持使用行级计算或将列引用为字符串而不是 :colname,这会阻止我直接从公式编辑器中使用它们。
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
my_min = New Custom Function(
"custom",
"My Min1",
Function({x, y},
As Constant(
vals = Column(x) << get values;
groups = Column(y) << get values;
);
cur_group = Loc(groups, As Column(y));
vals[cur_group[Loc Min(vals[cur_group])]];
),
<< Formula Category("Statistical")
);
Add Custom Functions(my_min);
my_min = New Custom Function(
"custom",
"My Min2",
Function({x, y},
Col Minimum(Column(x), Column(y));
),
<< Formula Category("Statistical")
);
Add Custom Functions(my_min);
my_min = New Custom Function(
"custom",
"My Min3",
Function({x, y},
Col Minimum(x, y);
),
<< Formula Category("Statistical")
);
Add Custom Functions(my_min);
dt << New Column("ColMin", Numeric, Continuous, Formula(Col Minimum(:height, :sex)));
dt << New Column("MyMin1_str", Numeric, Continuous, Formula(custom:My Min1("height", "sex")));
dt << New Column("MyMin2_str", Numeric, Continuous, Formula(custom:My Min2("height", "sex")));
dt << New Column("MyMin3_str", Numeric, Continuous, Formula(custom:My Min3("height", "sex")));
dt << New Column("MyMin1_ref", Numeric, Continuous, Formula(custom:My Min1(:height, :sex)));
dt << New Column("MyMin2_ref", Numeric, Continuous, Formula(custom:My Min2(:height, :sex)));
dt << New Column("MyMin3_ref", Numeric, Continuous, Formula(custom:My Min3(:height, :sex)));数据表脚本回复:创建具有 byVar 的统计自定义(公式)函数我相信会让这件事变得容易得多的愿望清单项目:
一次计算自定义列公式的选项或函数,或在行之间缓存值
回复:创建具有 byVar 的统计自定义(公式)函数两条评论:
- 我似乎找不到对它的引用,但我确实记得有一次明确地告诉我,没有办法编写一个自定义列公式来像内部公式那样在行之间缓存它的值。因此,您需要编写一个在每一行的上下文中进行计算的函数,这意味着它一次返回一个值。(如果可能的话,我希望有人在这里纠正我。)
- JMP 在将列引用传递给函数之前对其进行求值(急切求值),这意味着您将获得该行的值而不是整列的值。您需要告诉 JMP 传递对列的引用,您可以使用 Expr 来完成。
我相信这可以解决这两个问题,但我不确定它是否非常直观或用户友好。
更新:自定义函数可以访问行上下文,因此您可以跳过将当前行的值传递给函数的需要,就像在 Test2 中一样。
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
my_min = New Custom Function(
"custom",
"Test1",
Function({x,y,yval},
show(yval);
show(Name Expr(x)<< get values);
show(Loc(Name Expr(y) << Get Values, yval));
Min((Name Expr(x)<< get values)[Loc(Name Expr(y) << Get Values, yval)]);
),
<< Formula Category("Statistical")
);
Add Custom Functions(my_min);
custom:Test1(Expr(:age),Expr(:sex),"F");
dt << New Column("Test1", Numeric, Continuous, Formula(custom:Test1(Expr(:height), Expr(:sex), :sex)));
my_min = New Custom Function(
"custom",
"Test2",
Function({x,y},
xvals = Name Expr(x)<< get values;
yvals = Name Expr(y) << Get Values;
Min(xvals[Loc(yvals, yvals[row()])]);
),
<< Formula Category("Statistical")
);
Add Custom Functions(my_min);
dt << New Column("Test2", Numeric, Continuous, Formula(custom:Test2(Expr(:height), Expr(:sex))));回复:创建具有 byVar 的统计自定义(公式)函数好点子!我非常专注于获取要评估的列引用,我什至没有注意到这些值是荒谬的。我会再考虑的。
回复:创建具有 byVar 的统计自定义(公式)函数这似乎没有提供正确的答案,因为它很可能是“逐行”而不是按列评估它们

回复:创建具有 byVar 的统计自定义(公式)函数这会做你想要的吗?如果您希望函数参数是列引用,那么您似乎需要显式评估函数参数。
my_min = New Custom Function(
"custom",
"My Min4",
Function({x, y},
EvalExpr(Col Minimum(Expr(x), Expr(y)));
),
<< Formula Category("Statistical")
);
Add Custom Functions(my_min);