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

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

Highlighted

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

Mar 25, 2020 8:19 AM
(898 views)

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

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

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

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

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

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

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
##

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

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

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

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

Article Labels

There are no labels assigned to this post.