Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Re: linear regression in formula editor

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

linear regression in formula editor

Jan 2, 2019 2:28 AM
(1282 views)

Hello,

I am studying the slope of a biological variable over time in a clinical study, using linear regression.

Is that possible to calculate the slope in a new column, with the formula editor ? Or another way to generate such a column ?

I am using JMP 12.

Thank you for your help,

Florent

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: linear regression in formula editor

It feels wrong to put the slope value into a column in the same table as the data.

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:

```
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") << getValues;
yVals = Column(dt, "weight") << getValues;
betas = myLinearRegression(xVals, yVals);
slope = betas[2];
dt << NewColumn("Slope of weight vs. height", Numeric, Continuous, Values(Repeat(slope, NRow(dt))));
```

Do 'File > New > New Script', cut and paste the JSL above into that window, then 'Edit > Run Script'.

Of course it's always a good idea to plot the data before trusting any slope you calculate, and that's what 'Analyze > Fit Y By X' is designed for.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: linear regression in formula editor

Here's two examples of column formulas to calculate the length-weight regression slopes. The "Big Class" example table is used.

The first calculates the slope based an all observations and shows it in first row only instead of repeating the same number throughout the column.

The second calculates the slope incrementally (which may be what you're asking for) and it allows to see how the slope changes/stabilizes as more data are included and finally converges with the whole sample slope (i.e. first example).

To apply this to your own data, copy the whole formula inside the parentheses in *F=Expr( )* and paste it into the formula editor and subtitute *weight* and *height* for your variables.

```
dt = Open("$SAMPLE_DATA/Big Class.jmp");
dt << 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() <= Expr(Row()))) * (:weight
-Col Mean(:weight, Row() <= Expr(Row()))),
Row() <= Expr(Row())
) / Col Sum(
(:height - Col Mean(:height, Row() <= Expr(Row()))) ^ 2,
Row() <= Expr(Row())
)
)
)
);
dt << New Column("Incremental Slope", Formula(Name Expr(F)));
```