首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache数学进行带权重的线性回归

使用Apache数学进行带权重的线性回归
EN

Stack Overflow用户
提问于 2019-01-04 01:07:38
回答 1查看 950关注 0票数 1

我在使用OLSMultipleLinearRegression进行多元线性回归时,已经使用Apache Mathematic有一段时间了。现在,我需要扩展我的解决方案,为每个数据点包含一个权重因子。

我正在尝试复制MATLAB函数fitlm。

我有一个MATLAB调用,比如:

代码语言:javascript
复制
table_data = table(points_scored, height, weight, age);
model = fitlm( table_data, 'points_scored ~ -1, height, weight, age', 'Weights', data_weights)

从“模型”中,我得到了身高、体重、年龄的回归系数。

在Java中,我现在拥有的代码(大致)是:

代码语言:javascript
复制
double[][] variables = double[grades.length][3];
// Fill in variables for height, weight, age, 
...

OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
regression.newSampleData(points_scored, variables);

似乎没有一种方法可以将权重添加到OLSMultipleLinearRegression。似乎确实有一种方法可以向LeastSquaresBuilder添加权重。然而,我在弄清楚如何使用它时遇到了麻烦。我最大的问题(我认为)是创建预期的jacobians。

下面是我尝试过的大部分方法:

代码语言:javascript
复制
double[] points_scored = //fill in points scored
double[] height = //fill in 
double[] weight = //fill in
double[] age = // fill in

MultivariateJacobianFunction distToResidual= coeffs -> {
  RealVector value = new ArrayRealVector(points_scored.length);
  RealMatrix jacobian = new Array2DRowRealMatrix(points_scored.length, 3);

  for (int i = 0; i < measures.length; ++i) {
    double residual = points_scored[i];
    residual -= coeffs.getEntry(0) * height[i];  
    residual -= coeffs.getEntry(1) * weight[i];  
    residual -= coeffs.getEntry(2) * age[i];  
    value.setEntry(i, residual);
    //No idea how to set up the jacobian here
   }

   return new Pair<RealVector, RealMatrix>(value, jacobian);
};

double[] prescribedDistancesToLine = new double[measures.length];
Arrays.fill(prescribedDistancesToLine, 0);
double[] starts = new double[] {1, 1, 1};

LeastSquaresProblem problem = new LeastSquaresBuilder().
            start(starts).
            model(distToResidual).
            target(prescribedDistancesToLine).
            lazyEvaluation(false).
            maxEvaluations(1000).
            maxIterations(1000).
            build();
 LeastSquaresOptimizer.Optimum optimum = new LevenbergMarquardtOptimizer().optimize(problem);

因为我不知道如何计算雅可比的值,所以我一直在摸索,得到的系数与MATLAB的答案相去甚远。一旦我完成了这部分的工作,我就知道在LeastSquaresBuilder中添加权重应该是一个非常简单的额外代码。

提前感谢大家的帮助!

EN

回答 1

Stack Overflow用户

发布于 2019-10-01 17:24:31

您可以使用Apache中的GLSMultipleLinearRegression类。例如,让我们对权重为1,2,1的三个平面数据点(0,0),(1,2),(2,0)进行线性回归:

代码语言:javascript
复制
import org.apache.commons.math3.stat.regression.GLSMultipleLinearRegression;

public class Main {
    public static void main(String[] args) {
        GLSMultipleLinearRegression regr = new GLSMultipleLinearRegression();
        regr.setNoIntercept(false);
        double[] y = new double[]{0.0, 2.0, 0.0};
        double[][] x = new double[3][];
        x[0] = new double[]{0.0};
        x[1] = new double[]{1.0};
        x[2] = new double[]{2.0};
        double[][] omega = new double[3][];
        omega[0] = new double[]{1.0, 0.0, 0.0};
        omega[1] = new double[]{0.0, 0.5, 0.0};
        omega[2] = new double[]{0.0, 0.0, 1.0};
        regr.newSampleData(y, x, omega);
        double[] params = regr.estimateRegressionParameters();
        System.out.println("Slope: " + params[1] + ", intercept: " + params[0]);
    }
}

请注意,omega矩阵是对角线,其对角线元素是倒数权重。

查看多变量情况的documentation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54026757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档