<?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: FInding the best knot for a linear piecewise regression in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730523#M35430</link>
    <description>&lt;P&gt;Yeah, I've read that and some of his other spliny blog posts, but I've been intimidated by them (which is of course due to my own limitations).&amp;nbsp; Basically "NLIN, why did it have to be NLIN?"&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I agree, this blog post looks like what I want to do.&amp;nbsp; Another colleague mentioned PROC MODEL.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Maybe I'll take another stab at learning NLIN, and will post what I come up with here, in hopes that others will point out my mistakes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thx.&lt;/P&gt;</description>
    <pubDate>Wed, 31 Mar 2021 21:20:46 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2021-03-31T21:20:46Z</dc:date>
    <item>
      <title>FInding the best knot for a linear piecewise regression</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730467#M35422</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm working on a piecewise regression (I think of it as spline regression). I've got data that I want to model with two linear splines (i.e. one knot). I'm doing this for multiple samples, and the location of the knot will vary across samples; I would like to find the "best" knot for each sample.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an example, data like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data foo ;
  call streaminit(12345); 
  do x=1 to 100 ;
    if x&amp;lt;=50 then y= 75 + rand('Normal',0,5) ;
    else y= -.6 * (x-50) + 75 + rand('Normal',0,5) ;
    output ;
  end ;
run ;

proc sgplot data=foo ;
  scatter x=x y=y ;
  xaxis values=(0 to 100 by 10) ;
  yaxis values=(0 to 100 by 10) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="spline1.PNG" style="width: 633px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56750i4B55623D0C36DC81/image-size/large?v=v2&amp;amp;px=999" role="button" title="spline1.PNG" alt="spline1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So basically values are fairly stable for the first 50 values, then start to decrease.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I haven't done much of this, my first thought was to parameterize it by hand, and just run PROC REG, which seems to work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data p1 ;
  set foo ;
  x1=x ;
  if x&amp;lt;=50 then x2=0 ;
  else x2=x-50 ;
run ;

proc reg data=p1 ;
  model y = x1 x2 ;
  output out=pred1 p=predicted ;
run ;
quit ;

proc sgplot data=pred1 ;
  scatter x=x y=y ;
  series x=x y=predicted ;
  xaxis values=(0 to 100 by 10) ;
  yaxis values=(0 to 100 by 10) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which yields:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="spline2.PNG" style="width: 629px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56751i0816DDBB98CAA225/image-size/large?v=v2&amp;amp;px=999" role="button" title="spline2.PNG" alt="spline2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Which I think I'm happy with.&amp;nbsp; But my guess is there is likely a better way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Questions:&lt;/P&gt;
&lt;P&gt;1. If you accept the constraint that I want two linear splines with a knot between them, is there an easier/better way to fit this model without parameterizing it by hand in a sort of dummy-coding way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. The location of the knot will vary from sample to sample (e.g. might be at x=30 rather than x=50).&amp;nbsp; I would like to find the "best" knot for each sample.&amp;nbsp; Any thoughts on finding the best knot?&amp;nbsp; I was thinking something simple like for each sample, run 99 regressions where the knot location is varied from 1 to 99, then find the regression with best fit (R**2), and call that the best knot for that data.&amp;nbsp; But I'm sure there's a better way (NLIN?).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. If I relaxed the constraint of two *linear* splines, what sort of spliny regression might you use for data like this, where values of y are roughly stable for an unknown period of time, and then begin to decay at some roughly stable rate?&amp;nbsp; Across samples the amount of time (x) before decay starts varies, as does the rate of decay.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any general suggestions for good into papers on splines welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 19:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730467#M35422</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-03-31T19:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: FInding the best knot for a linear piecewise regression</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730500#M35424</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; wrote a nice blog entry about this problem&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2020/12/14/segmented-regression-sas.html" target="_self"&gt;https://blogs.sas.com/content/iml/2020/12/14/segmented-regression-sas.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 20:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730500#M35424</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-31T20:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: FInding the best knot for a linear piecewise regression</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730523#M35430</link>
      <description>&lt;P&gt;Yeah, I've read that and some of his other spliny blog posts, but I've been intimidated by them (which is of course due to my own limitations).&amp;nbsp; Basically "NLIN, why did it have to be NLIN?"&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I agree, this blog post looks like what I want to do.&amp;nbsp; Another colleague mentioned PROC MODEL.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Maybe I'll take another stab at learning NLIN, and will post what I come up with here, in hopes that others will point out my mistakes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thx.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 21:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730523#M35430</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-03-31T21:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: FInding the best knot for a linear piecewise regression</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730528#M35434</link>
      <description>&lt;P&gt;Very good idea. You will find that nlin isn't that hard to master and works quite well.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 21:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730528#M35434</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-31T21:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: FInding the best knot for a linear piecewise regression</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730647#M35442</link>
      <description>Why not try non-parameter method ? Like PROC LOESS  &lt;BR /&gt;or add more term in model like x1^2  , x2^2</description>
      <pubDate>Thu, 01 Apr 2021 11:34:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/730647#M35442</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-01T11:34:37Z</dc:date>
    </item>
    <item>
      <title>Re: FInding the best knot for a linear piecewise regression</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/731131#M35453</link>
      <description>&lt;P&gt;Thanks again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;,&amp;nbsp;PROC NLIN did a really nice job of it.&amp;nbsp; Took me a little while to work through the docs, and then the highschool algebra (luckily, no calculus needed for my model...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Model is:
  y= C             if x  &amp;lt; x0
  y= B0 + B1 * X   if x &amp;gt;= x0

where
  C is some constant
  x0 is the x value of the knot

We force the two splines to meet, so:
  C = B0 + B1 * x0

therefore nlin models:
  if x &amp;lt; x0 then model y=B0 + B1*x0 
  else           model y=B0 + B1*x  &lt;/PRE&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc nlin data=foo ;
  parms x0=15  B0=100 B1=-1;

  if x &amp;lt; x0 then model y=B0 + B1*x0 ;
  else           model y=B0 + B1*x  ;

  output out=pred predicted=pred ;
run ;


proc sgplot data=pred ;
  scatter x=x y=y ;
  series x=x y=pred ;

  xaxis values=(0 to 100 by 10) ;
  yaxis values=(0 to 100 by 10) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NLINsplines.PNG" style="width: 792px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56872i3C5009A16355825E/image-size/large?v=v2&amp;amp;px=999" role="button" title="NLINsplines.PNG" alt="NLINsplines.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Apr 2021 13:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/FInding-the-best-knot-for-a-linear-piecewise-regression/m-p/731131#M35453</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-04-03T13:06:18Z</dc:date>
    </item>
  </channel>
</rss>

