pub fn linear_regression(args: &[Value]) -> Result<Value, RuntimeError>Expand description
简单线性回归
§功能
对数据进行简单线性回归分析,返回斜率和截距。
§参数
x: Array - 自变量数组y: Array - 因变量数组
§返回值
Array - [slope, intercept, r_squared]
- slope: 斜率
- intercept: 截距
- r_squared: 决定系数 R²
§公式
y = slope * x + intercept
slope = Σ[(xi - x̄)(yi - ȳ)] / Σ(xi - x̄)²
intercept = ȳ - slope * x̄
R² = 1 - SS_res / SS_tot§错误
- 两个数组长度必须相同
- 至少需要 2 个数据点
§示例
Set x [1, 2, 3, 4, 5]
Set y [2, 4, 5, 4, 5]
Set result LinearRegression(x, y)
Set slope result[0] # 0.6
Set intercept result[1] # 2.2
Set r2 result[2] # 0.4286