Choose Language Hide Translation Bar
New Contributor

## Creating columns from Linear Fit Constants (slope, intercept)

Trying to pull constants, slope and intercept, from the linear fit formula across multiple plots.  I am using Big Class as an example.  I make a Bi-variate plot across different parameters, next I make a linear fit for each plot and create a new column for the predicted values.  I want to take the Linear Fit formula constants and create two new columns that hold the slope and intercept from that formula for each row. Here is what I have so far, I am stuck where I save the formula from the linear fit, but can't quite pull the formula values into a column, I think I want to use the expr() function but I am not sure, any help would be greatly appreciated, thanks in advance.

``````dt = Current Data Table(); //Big Class active

biv = Bivariate(
Y( :height ),
X( :weight ),
by(	:sex, :age ),
Fit Line( {Line Color( {212, 73, 88} )}, save predicteds ),
);
biv << (curve[1] << save predicteds);
Column( N Cols( dt ) ) << set name( "Linear Fit" );

myFormula = Column(dt, "Linear Fit") << getFormula;

// Stuck here want to use the "Linear Fit" formula and create a column for the slope in the formula
// and another for the intercept pulled from the predicted formula in "Linear Fit"

dt << NewColumn("Slope", Numeric, Continous, expr(left(myFormula,5)));
dt << NewColumn("Intercept", Numeric, Continous, );``````

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Super User

## Re: Creating columns from Linear Fit Constants (slope, intercept)

I believe you will have an easier time getting the slope and intercept values from all of the different combinations of sex and age, if you create a combined data table from the estimates table.  It will allow you to parse through the outputted data table and easily get the values you want.

``````dt = Current Data Table(); //Big Class active

biv = Bivariate(
Y( :height ),
X( :weight ),
by(	:sex, :age ),
Fit Line( {Line Color( {212, 73, 88} )}, save predicteds ),
);
report(biv[1])["Parameter Estimates"][tablebox(1)]<<make combined data table;``````
Jim
Highlighted
Super User

## Re: Creating columns from Linear Fit Constants (slope, intercept)

I like data table processing, so many times I use the Make Combined Data Table and then go from there.  Here is a version of the script that is what I think your final desired output is

``````Names default to here(1);
dt = Current Data Table(); //Big Class active

biv = Bivariate(
Y( :height ),
X( :weight ),
by(	:sex, :age ),
Fit Line( {Line Color( {212, 73, 88} )}, save predicteds ),
);
report(biv[1])["Parameter Estimates"][tablebox(1)]<<make combined data table(invisible);

biv << close window;

dtIntSlope = current data table();

dtTrans = dtIntSlope << Transpose(invisible,
columns( :Estimate ),
Transpose selected rows only( 1 ),
By( :age, :sex ),
Label( :Term ),
Label column name( "Term" ),
Output Table( "Transpose of Untitled 60" )
);

dtTrans:weight << set name("Slope");
dtTrans:age << data type(numeric)<<modeling type(ordinal);

dt << Update(
With( dtTrans),
Match Columns( :age = :age, :sex = :sex ),
Add Columns from Update Table( :Intercept, :Slope )
);

close(dtIntSlope, nosave );
close(dtTrans, nosave );``````
Jim
3 REPLIES 3
Highlighted
Super User

## Re: Creating columns from Linear Fit Constants (slope, intercept)

I believe you will have an easier time getting the slope and intercept values from all of the different combinations of sex and age, if you create a combined data table from the estimates table.  It will allow you to parse through the outputted data table and easily get the values you want.

``````dt = Current Data Table(); //Big Class active

biv = Bivariate(
Y( :height ),
X( :weight ),
by(	:sex, :age ),
Fit Line( {Line Color( {212, 73, 88} )}, save predicteds ),
);
report(biv[1])["Parameter Estimates"][tablebox(1)]<<make combined data table;``````
Jim
Highlighted
New Contributor

## Re: Creating columns from Linear Fit Constants (slope, intercept)

Jim,

I thought about that, but I lose connection with the original Big Class data set, i.e "Name", I guess its just as easy to go back and join the two tables instead of trying to get too fancy in one table.  Thanks!

Highlighted
Super User

## Re: Creating columns from Linear Fit Constants (slope, intercept)

I like data table processing, so many times I use the Make Combined Data Table and then go from there.  Here is a version of the script that is what I think your final desired output is

``````Names default to here(1);
dt = Current Data Table(); //Big Class active

biv = Bivariate(
Y( :height ),
X( :weight ),
by(	:sex, :age ),
Fit Line( {Line Color( {212, 73, 88} )}, save predicteds ),
);
report(biv[1])["Parameter Estimates"][tablebox(1)]<<make combined data table(invisible);

biv << close window;

dtIntSlope = current data table();

dtTrans = dtIntSlope << Transpose(invisible,
columns( :Estimate ),
Transpose selected rows only( 1 ),
By( :age, :sex ),
Label( :Term ),
Label column name( "Term" ),
Output Table( "Transpose of Untitled 60" )
);

dtTrans:weight << set name("Slope");
dtTrans:age << data type(numeric)<<modeling type(ordinal);

dt << Update(
With( dtTrans),
Match Columns( :age = :age, :sex = :sex ),
Add Columns from Update Table( :Intercept, :Slope )
);

close(dtIntSlope, nosave );
close(dtTrans, nosave );``````
Jim