<?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: linear regression in formula editor in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86575#M38608</link>
    <description>&lt;P&gt;It feels wrong to put the slope value into a column in the same table as the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, if that is indeed what you meant and you want to avoid the use of a JMP platform, here's one way to do it in JMP 12:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

myLinearRegression = 
Function({x, y}, {Default Local},
	// Add the unit vector to the design matrix
	x = J(NRow(x), 1, 1)||x;
	// Get the parameters
	betas = Transpose(Inv(x`*x)*x`*y);
);


dt = Open("$SAMPLE_DATA/Big Class.jmp");
Wait(3);
xVals = Column(dt, "height") &amp;lt;&amp;lt; getValues;
yVals = Column(dt, "weight") &amp;lt;&amp;lt; getValues;
betas = myLinearRegression(xVals, yVals);
slope = betas[2];
dt &amp;lt;&amp;lt; NewColumn("Slope of weight vs. height", Numeric, Continuous, Values(Repeat(slope, NRow(dt))));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do 'File &amp;gt; New &amp;gt; New Script', cut and paste the JSL above into that window, then 'Edit &amp;gt; Run Script'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course it's always a good idea to plot the data before trusting any slope you calculate, and that's what 'Analyze &amp;gt; Fit Y By X' is designed for.&lt;/P&gt;</description>
    <pubDate>Wed, 02 Jan 2019 11:39:27 GMT</pubDate>
    <dc:creator>ian_jmp</dc:creator>
    <dc:date>2019-01-02T11:39:27Z</dc:date>
    <item>
      <title>linear regression in formula editor</title>
      <link>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86573#M38607</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am studying the slope of a biological variable over time in a clinical study, using linear regression.&lt;/P&gt;&lt;P&gt;Is that possible to calculate the slope in a new column, with the formula editor ? Or another way to generate such a column ?&lt;/P&gt;&lt;P&gt;I am using JMP 12.&lt;/P&gt;&lt;P&gt;Thank you for your help,&lt;/P&gt;&lt;P&gt;Florent&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 10:28:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86573#M38607</guid>
      <dc:creator>fguerville</dc:creator>
      <dc:date>2019-01-02T10:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: linear regression in formula editor</title>
      <link>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86575#M38608</link>
      <description>&lt;P&gt;It feels wrong to put the slope value into a column in the same table as the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, if that is indeed what you meant and you want to avoid the use of a JMP platform, here's one way to do it in JMP 12:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

myLinearRegression = 
Function({x, y}, {Default Local},
	// Add the unit vector to the design matrix
	x = J(NRow(x), 1, 1)||x;
	// Get the parameters
	betas = Transpose(Inv(x`*x)*x`*y);
);


dt = Open("$SAMPLE_DATA/Big Class.jmp");
Wait(3);
xVals = Column(dt, "height") &amp;lt;&amp;lt; getValues;
yVals = Column(dt, "weight") &amp;lt;&amp;lt; getValues;
betas = myLinearRegression(xVals, yVals);
slope = betas[2];
dt &amp;lt;&amp;lt; NewColumn("Slope of weight vs. height", Numeric, Continuous, Values(Repeat(slope, NRow(dt))));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do 'File &amp;gt; New &amp;gt; New Script', cut and paste the JSL above into that window, then 'Edit &amp;gt; Run Script'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course it's always a good idea to plot the data before trusting any slope you calculate, and that's what 'Analyze &amp;gt; Fit Y By X' is designed for.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 11:39:27 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86575#M38608</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2019-01-02T11:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: linear regression in formula editor</title>
      <link>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86627#M38644</link>
      <description>&lt;P&gt;Here's two&amp;nbsp;examples of column formulas to&amp;nbsp;calculate the length-weight regression slopes. The "Big Class" example table is used.&lt;/P&gt;
&lt;P&gt;The first calculates the slope based an all observations and shows it in first row&amp;nbsp;only instead of repeating the same number throughout the column.&lt;/P&gt;
&lt;P&gt;The second calculates the slope incrementally (which may be what you're asking for) and it allows&amp;nbsp;to see how the slope&amp;nbsp;changes/stabilizes as more data are included and finally converges with the whole sample slope (i.e. first example).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To apply this to your own data, copy&amp;nbsp;the whole formula&amp;nbsp;inside the parentheses in&amp;nbsp;&lt;EM&gt;F=Expr( )&lt;/EM&gt;&amp;nbsp;and paste it into the formula editor and subtitute &lt;EM&gt;weight&lt;/EM&gt; and &lt;EM&gt;height&lt;/EM&gt; for your variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Open("$SAMPLE_DATA/Big Class.jmp");
dt &amp;lt;&amp;lt; New Column("Slope",
    Formula(
        If(Row() == 1,
            Col Sum((:height - Col Mean(:height)) * (:weight - Col Mean(:weight))) /
            Col Sum((:height - Col Mean(:height)) ^ 2),
            .
        )
    )
);

F = Expr(
    Eval(
        Eval Expr(
            Col Sum(
                (:height - Col Mean(:height, Row() &amp;lt;= Expr(Row()))) * (:weight
                -Col Mean(:weight, Row() &amp;lt;= Expr(Row()))),
                Row() &amp;lt;= Expr(Row())
            ) / Col Sum(
                (:height - Col Mean(:height, Row() &amp;lt;= Expr(Row()))) ^ 2,
                Row() &amp;lt;= Expr(Row())
            )
        )
    )
);
        
dt &amp;lt;&amp;lt; New Column("Incremental Slope", Formula(Name Expr(F)));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 01:50:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/linear-regression-in-formula-editor/m-p/86627#M38644</guid>
      <dc:creator>ms</dc:creator>
      <dc:date>2019-01-03T01:50:38Z</dc:date>
    </item>
  </channel>
</rss>

