cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
fguerville
Level I

linear regression in formula editor

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
ian_jmp
Level X

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.

ms
Super User (Alumni) ms
Super User (Alumni)

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)));