<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating statistical custom (formula) function which has byVar in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/511046#M73919</link>
    <description>&lt;P&gt;Two comments:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I can't seem to find the reference to it, but I do remember being definitively told at one point that there is no way to write a custom column formula that caches it's value between rows the way internal formulas do.&amp;nbsp; Thus you would need to write a function that evaluates in the context of each row, meaning it returns a single value at a time.&amp;nbsp;&amp;nbsp;(I would love for someone to correct me here if that is possible.)&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;JMP evaluates the column reference before passing it to the function (eager evaluation) which means you get the value at that row instead of the whole column.&amp;nbsp; You need to tell JMP to pass a reference to the column, and you can do that with Expr.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;I believe this fixes both issues, but I'm not sure it is very intuitive or user friendly.&lt;/P&gt;
&lt;P&gt;Update: the custom function can access the row context, so you can skip needing to pass the value for the current row into the function, as in the Test2.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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)&amp;lt;&amp;lt; get values);
		show(Loc(Name Expr(y) &amp;lt;&amp;lt; Get Values, yval));
		Min((Name Expr(x)&amp;lt;&amp;lt; get values)[Loc(Name Expr(y) &amp;lt;&amp;lt; Get Values, yval)]);
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);

custom:Test1(Expr(:age),Expr(:sex),"F");

dt &amp;lt;&amp;lt; 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)&amp;lt;&amp;lt; get values;
		yvals = Name Expr(y) &amp;lt;&amp;lt; Get Values;
		Min(xvals[Loc(yvals, yvals[row()])]);
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);

dt &amp;lt;&amp;lt; New Column("Test2", Numeric, Continuous, Formula(custom:Test2(Expr(:height), Expr(:sex))));&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Jun 2022 15:19:42 GMT</pubDate>
    <dc:creator>ih</dc:creator>
    <dc:date>2022-06-21T15:19:42Z</dc:date>
    <item>
      <title>Creating statistical custom (formula) function which has byVar</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/506731#M73661</link>
      <description>&lt;P&gt;Related to this wish list item I created: &lt;LI-MESSAGE title="Add normalization and robust statistical functions (and matrix functions)" uid="504984" url="https://community.jmp.com/t5/JMP-Wish-List/Add-normalization-and-robust-statistical-functions-and-matrix/m-p/504984#U504984" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt; . I thought I could just try to write these as custom functions for now (even though I know it will be impossible to share them and have them updated for everyone), but I faced my first problem almost immediately: how to reference columns... When using Custom Formulas am I stuck using row-level calculations or referencing columns as strings instead of :colname which would prevent me from using them directly from Formula Editor.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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) &amp;lt;&amp;lt; get values;
			groups = Column(y) &amp;lt;&amp;lt; get values;
		);
		cur_group = Loc(groups, As Column(y));
		vals[cur_group[Loc Min(vals[cur_group])]];
	),
	&amp;lt;&amp;lt; 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));
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);

my_min = New Custom Function(
	"custom",
	"My Min3",
	Function({x, y},
		Col Minimum(x, y);
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);

dt &amp;lt;&amp;lt; New Column("ColMin", Numeric, Continuous, Formula(Col Minimum(:height, :sex)));
dt &amp;lt;&amp;lt; New Column("MyMin1_str", Numeric, Continuous, Formula(custom:My Min1("height", "sex")));
dt &amp;lt;&amp;lt; New Column("MyMin2_str", Numeric, Continuous, Formula(custom:My Min2("height", "sex")));
dt &amp;lt;&amp;lt; New Column("MyMin3_str", Numeric, Continuous, Formula(custom:My Min3("height", "sex")));
dt &amp;lt;&amp;lt; New Column("MyMin1_ref", Numeric, Continuous, Formula(custom:My Min1(:height, :sex)));
dt &amp;lt;&amp;lt; New Column("MyMin2_ref", Numeric, Continuous, Formula(custom:My Min2(:height, :sex)));
dt &amp;lt;&amp;lt; New Column("MyMin3_ref", Numeric, Continuous, Formula(custom:My Min3(:height, :sex)));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:49:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/506731#M73661</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-06-10T23:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating statistical custom (formula) function which has byVar</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/508516#M73723</link>
      <description>&lt;P&gt;Does this do what you want?&amp;nbsp; It looks like you need to explicitly evaluate the function parameters if you expect them to be column references.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;my_min = New Custom Function(
	"custom",
	"My Min4",
	Function({x, y},
		EvalExpr(Col Minimum(Expr(x), Expr(y)));
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jun 2022 21:55:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/508516#M73723</guid>
      <dc:creator>Jasean</dc:creator>
      <dc:date>2022-06-13T21:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating statistical custom (formula) function which has byVar</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/510398#M73767</link>
      <description>&lt;P&gt;That doesn't seem to provide correct answer as it is most likely evaluating them "row by row" and not as columns&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1655279502111.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/43275i15EED2B3EBC08B7C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1655279502111.png" alt="jthi_0-1655279502111.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 07:52:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/510398#M73767</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-06-15T07:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating statistical custom (formula) function which has byVar</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/510493#M73784</link>
      <description>&lt;P&gt;Good point!&amp;nbsp; I was so focused on getting the column references to evaluate, I didn't even notice that the values were nonsensical.&amp;nbsp; I'll think about it more.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 17:57:07 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/510493#M73784</guid>
      <dc:creator>Jasean</dc:creator>
      <dc:date>2022-06-15T17:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating statistical custom (formula) function which has byVar</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/511046#M73919</link>
      <description>&lt;P&gt;Two comments:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I can't seem to find the reference to it, but I do remember being definitively told at one point that there is no way to write a custom column formula that caches it's value between rows the way internal formulas do.&amp;nbsp; Thus you would need to write a function that evaluates in the context of each row, meaning it returns a single value at a time.&amp;nbsp;&amp;nbsp;(I would love for someone to correct me here if that is possible.)&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;JMP evaluates the column reference before passing it to the function (eager evaluation) which means you get the value at that row instead of the whole column.&amp;nbsp; You need to tell JMP to pass a reference to the column, and you can do that with Expr.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;I believe this fixes both issues, but I'm not sure it is very intuitive or user friendly.&lt;/P&gt;
&lt;P&gt;Update: the custom function can access the row context, so you can skip needing to pass the value for the current row into the function, as in the Test2.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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)&amp;lt;&amp;lt; get values);
		show(Loc(Name Expr(y) &amp;lt;&amp;lt; Get Values, yval));
		Min((Name Expr(x)&amp;lt;&amp;lt; get values)[Loc(Name Expr(y) &amp;lt;&amp;lt; Get Values, yval)]);
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);

custom:Test1(Expr(:age),Expr(:sex),"F");

dt &amp;lt;&amp;lt; 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)&amp;lt;&amp;lt; get values;
		yvals = Name Expr(y) &amp;lt;&amp;lt; Get Values;
		Min(xvals[Loc(yvals, yvals[row()])]);
	),
	&amp;lt;&amp;lt; Formula Category("Statistical")
);
Add Custom Functions(my_min);

dt &amp;lt;&amp;lt; New Column("Test2", Numeric, Continuous, Formula(custom:Test2(Expr(:height), Expr(:sex))));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2022 15:19:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/511046#M73919</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2022-06-21T15:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating statistical custom (formula) function which has byVar</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/513089#M74056</link>
      <description>&lt;P&gt;Wish list item that I believe would make this a lot easier:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;LI-MESSAGE title="Option or function to evaluate a custom column formula at once, or cache values between rows" uid="513082" url="https://community.jmp.com/t5/JMP-Wish-List/Option-or-function-to-evaluate-a-custom-column-formula-at-once/m-p/513082#U513082" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 16:31:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-statistical-custom-formula-function-which-has-byVar/m-p/513089#M74056</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2022-06-23T16:31:59Z</dc:date>
    </item>
  </channel>
</rss>

