@tpmjs/tools-linear-regression-ols
Perform simple linear regression using Ordinary Least Squares (OLS) to find the best-fit line for x and y data. Returns slope, intercept, R-squared (goodness of fit), residuals, and predictions. Useful for modeling linear relationships and making predictions.
Test @tpmjs/tools-linear-regression-ols (linearRegressionOLSTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-linear-regression-olspnpm add @tpmjs/tools-linear-regression-olsyarn add @tpmjs/tools-linear-regression-olsbun add @tpmjs/tools-linear-regression-olsdeno add npm:@tpmjs/tools-linear-regression-olsimport { linearRegressionOLSTool } from '@tpmjs/tools-linear-regression-ols';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { linearRegressionOLSTool } from '@tpmjs/tools-linear-regression-ols';
const result = await generateText({
model: openai('gpt-4o'),
tools: { linearRegressionOLSTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
xarrayIndependent variable values (predictor)
yarrayDependent variable values (response)
Schema extracted: 1/1/2026, 8:17:55 AM
Perform simple linear regression using Ordinary Least Squares (OLS) to find the best-fit line for your data.
npm install @tpmjs/tools-linear-regression-ols
import { linearRegressionOLSTool } from '@tpmjs/tools-linear-regression-ols'; // Use with AI SDK const result = await linearRegressionOLSTool.execute({ x: [1, 2, 3, 4, 5], y: [2, 4, 5, 4, 5], }); console.log(result); // { // slope: 0.6, // intercept: 2.2, // rSquared: 0.581, // residuals: [-0.8, 0.6, 0.4, -0.6, 0.4], // predictions: [2.8, 3.4, 4.0, 4.6, 5.2], // metadata: { // n: 5, // meanX: 3, // meanY: 4, // sst: 5, // sse: 2.096 // } // }
{ slope: number; // Slope of the regression line (β₁) intercept: number; // Y-intercept (β₀) rSquared: number; // Coefficient of determination (0-1) residuals: number[]; // Residuals (y - ŷ) for each point predictions: number[]; // Predicted values (ŷ) for each x metadata: { n: number; // Number of observations meanX: number; // Mean of x values meanY: number; // Mean of y values sst: number; // Total sum of squares sse: number; // Sum of squared errors } }
The tool implements simple linear regression using the OLS method:
Where:
R² measures the proportion of variance in y explained by x:
const result = await linearRegressionOLSTool.execute({ x: [1, 2, 3, 4, 5], y: [2.1, 3.9, 6.2, 7.8, 10.1], }); // Use slope and intercept to predict new values const predictY = (x: number) => result.intercept + result.slope * x; console.log(predictY(6)); // Predict y for x=6 // Output: ~12.0
MIT
Downloads/month
0
Quality Score