使用 Math.NET 进行百分位数多元回归

问题描述 投票:0回答:1

我目前正在使用 Math.NET 中的 Fit.MultiDim 来计算具有 2 维数据集的估计值,如下所示:

using MathNet.Numerics;
using System.Collections.Generic;
using System.Linq;

namespace Project
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var xPoints = new List<List<double>>
            {
                new List<double> {2000, 100},
                new List<double> {2002, 60},
                new List<double> {2004, 50},
                new List<double> {2006, 30},
            };
            var yPoints = new List<double> { 50, 60, 70, 80 };
            var multiFit = Fit.MultiDim(
                xPoints.Select(item => item.ToArray()).ToArray(),
                yPoints.ToArray(),
                intercept: true
            );
            // multiFit = [-9949.999999999998,4.999999999999999,0.0]
            var inputDimension1 = 2003;
            var inputDimension2 = 55;
            var expectedY = multiFit[0] + inputDimension1 * multiFit[1] + inputDimension2 * multiFit[2];
            // expectedY = 65
        }
    }
}

如何更新逻辑以便能够指定我想要计算值的百分位?假设我想获得 25% 和 75% 的值。

我知道该库有

Percentile
Quantile
方法,但我没有任何统计学知识,所以不知道如何将其应用到我的用例中。

c# linear-regression math.net mathnet-numerics
1个回答
0
投票

我还不能发表评论,所以这不是如何在 C# 中实现这一点的答案,但我有一些建议。

您正在寻找的是分位数回归。分位数是百分位数的另一种说法。但是,Math.NET 目前还没有实现此功能。

如果您不熟悉 C#,Roger Koenker(统计方法的开发者)已经制作了一个名为“quantreg”的 R 包,您可以使用。此外,他还写了一本关于该主题的书,可以在这里免费获取。不过,公平的警告是,这本书的数学内容非常多,所以如果您确实需要执行此分位数/百分位数分析(而不是自己实现的替代方案),我建议您切换到 R。

使用一些 R 代码和“diamonds”数据集的快速示例:

#install.packages("quantreg") # Run this line first then comment back out
#install.packages("tidyverse") # Run this line first then comment back out
library(quantreg)
library(tidyverse)

# A normal linear regression: predict the carat of the diamond 
# using the depth, table, and price variables with no interaction effects
lm_diamond_model <- lm(carat ~ depth + table + price, diamonds)

summary(lm_diamond_model) # view the resuting details


# The same problem, except now we model the 50th percentile/quantile
median_reg_diamond_model <- rq(carat ~ depth + table + price,
                               tau = 0.5,
                               diamonds)

summary(median_reg_diamond_model) # view the resuting details


# The same problem, except now we model the 10th, 20th, ... , 90th percentiles
range_diamond_models <- rq(carat ~ depth + table + price,
                           tau = seq(from = 0.1, to = 0.9, by = 0.1),
                           diamonds)

summary(range_diamond_models) # view the resuting details
© www.soinside.com 2019 - 2024. All rights reserved.