<?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 How can I use a lag function applied by groups? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/499985#M73511</link>
    <description>&lt;P&gt;I have data where I'd like to compute the lag from one row to the next (it is a time series), but I have data for many groups.&amp;nbsp; I want the lag created within each group separately.&amp;nbsp; For many statistical functions (e.g., Col Mean) there is the option to add one or more By Group variables.&amp;nbsp; There does not appear to be the same functionality for the Lag or Dif functions and I can't find anything in the scripting documentation to indicate how that might be done.&amp;nbsp; Is there a way to easily compute the lags for each group separately?&amp;nbsp; The only way I have currently found is to first sort the data by date and by group, and then use an IF formula that depends on the Group equaling the lag (Group), such as IF (Group) - lag (Group), then lag (column of interest), else missing.&lt;/P&gt;</description>
    <pubDate>Sat, 10 Jun 2023 23:49:09 GMT</pubDate>
    <dc:creator>dale_lehman</dc:creator>
    <dc:date>2023-06-10T23:49:09Z</dc:date>
    <item>
      <title>How can I use a lag function applied by groups?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/499985#M73511</link>
      <description>&lt;P&gt;I have data where I'd like to compute the lag from one row to the next (it is a time series), but I have data for many groups.&amp;nbsp; I want the lag created within each group separately.&amp;nbsp; For many statistical functions (e.g., Col Mean) there is the option to add one or more By Group variables.&amp;nbsp; There does not appear to be the same functionality for the Lag or Dif functions and I can't find anything in the scripting documentation to indicate how that might be done.&amp;nbsp; Is there a way to easily compute the lags for each group separately?&amp;nbsp; The only way I have currently found is to first sort the data by date and by group, and then use an IF formula that depends on the Group equaling the lag (Group), such as IF (Group) - lag (Group), then lag (column of interest), else missing.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:49:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/499985#M73511</guid>
      <dc:creator>dale_lehman</dc:creator>
      <dc:date>2023-06-10T23:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a lag function applied by groups?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/500007#M73512</link>
      <description>&lt;P&gt;To make this more concrete, I've attached a simple example.&amp;nbsp; The lagged deaths column is what I want - but I'd like to be able to do it more simply or elegantly.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jun 2022 13:20:45 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/500007#M73512</guid>
      <dc:creator>dale_lehman</dc:creator>
      <dc:date>2022-06-05T13:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a lag function applied by groups?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/500037#M73513</link>
      <description>&lt;P&gt;Here is a formula that appears to work, even when the rows are not sorted by the 2 levels. (i.e. sorted by date)&amp;nbsp; The lagList in the formula, is there to handle the issue that the Col Moving Average() function returns the current row, even on the first occurrence.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1, lagList = {} );
If( Contains( lagList, :administrative_area_level_1 || :administrative_area_level_2 ),
	Col Moving Average(
		:deaths,
		weighting = 1,
		before = 1,
		:administrative_area_level_1,
		:administrative_area_level_2
	) * 2 - :deaths,
	Insert Into( lagList, :administrative_area_level_1 || :administrative_area_level_2 )
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1654440546090.png" style="width: 766px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/42973iD8258584899BDB31/image-dimensions/766x565?v=v2" width="766" height="565" role="button" title="txnelson_0-1654440546090.png" alt="txnelson_0-1654440546090.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Here is an inefficient formula that also works&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;curLevel1 = :administrative_area_level_1;
curLevel2 = :administrative_area_level_2;
curRow = Row();
Try(
	:deaths[Max(
		Current Data Table() &amp;lt;&amp;lt; get rows where(
			:administrative_area_level_1 == curLevel1 &amp;amp; (:administrative_area_level_2 == curLevel2 &amp;amp; Row() &amp;lt; curRow)
		)
	)]
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jun 2022 15:07:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/500037#M73513</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-06-05T15:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use a lag function applied by groups?</title>
      <link>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/500047#M73514</link>
      <description>&lt;P&gt;I might have guessed that txnelson would have a solution!&amp;nbsp; It isn't really more elegant or simpler than what I had, but it avoids the need to sort the data - which makes it considerably more flexible.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jun 2022 15:07:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-can-I-use-a-lag-function-applied-by-groups/m-p/500047#M73514</guid>
      <dc:creator>dale_lehman</dc:creator>
      <dc:date>2022-06-05T15:07:18Z</dc:date>
    </item>
  </channel>
</rss>

