# Linear Regression

Fit a simple linear regression and return the formula. Optionally, use one or more group\_by columns to create a regression per unique grouping.

Currently, only supports a single independent variable.

## Parameters

| Name      | Type         | Description                                                                                                                         | Is Optional |
| --------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| group\_by | column\_list | Columns to group by before building the linear regression model. Use this field to create multiple models (one per unique grouping) | True        |
| y         | column       | Dependent variable for the linear regression                                                                                        |             |
| x         | column       | Independent variable for the linear regression                                                                                      |             |

## Example

```python
internet_sales = rasgo.get.dataset(74)

ds1 = internet_sales.aggregate(
    group_by=['PRODUCTKEY','CUSTOMERKEY'],
    aggregations={'SALESAMOUNT':['AVG'],
                'TOTALPRODUCTCOST':['AVG']})

ds2 = ds1.linear_regression(
  x = 'SALESAMOUNT_AVG',
  y = 'TOTALPRODUCTCOST_AVG')

ds2.preview()
```

## Source Code

{% embed url="<https://github.com/rasgointelligence/RasgoTransforms/blob/main/rasgotransforms/rasgotransforms/transforms/linear_regression/linear_regression.sql>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rasgoml.com/rasgo-docs/rasgo-0.1/all-transforms/linear_regression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
