R适合用户定义的分布

时间:2016-06-29 15:30:24

标签: r distribution fitdistrplus

我正在尝试将自己的分布拟合到我的数据中,找到分布的最佳参数以匹配数据,并最终找到分布中峰值的FWHM。根据我的阅读,package fitdistrplus是实现此目的的方法。我知道数据在二次背景上呈现洛伦兹峰的形状。

数据图: plot of raw data

使用的原始数据:

data = c(0,2,5,4,5,4,3,3,2,2,0,4,4,2,5,5,3,3,4,4,4,3,3,5,5,6,6,8,4,0,6,5,7,5,6,3,2,1,7,0,7,9,5,7,5,3,5,5,4,1,4,8,10,2,5,8,7,14,7,5,8,4,2,2,6,5,4,6,5,7,5,4,8,5,4,8,11,9,4,8,11,7,8,6,9,5,8,9,10,8,4,5,8,10,9,12,10,10,5,5,9,9,11,19,17,9,17,10,17,18,11,14,15,12,11,14,12,10,10,8,7,13,14,17,18,16,13,16,14,17,20,15,12,15,16,18,24,23,20,17,21,20,20,23,20,15,20,28,27,26,20,17,19,27,21,28,32,29,20,19,24,19,19,22,27,28,23,37,41,42,34,37,29,28,28,27,38,32,37,33,23,29,55,51,41,50,44,46,53,63,49,50,47,54,54,43,45,58,54,55,67,52,57,67,69,62,62,65,56,72,75,88,87,77,70,71,84,85,81,84,75,78,80,82,107,102,98,82,93,98,90,94,118,107,113,103,99,103,96,108,114,136,126,126,124,130,126,113,120,107,107,106,107,136,143,135,151,132,117,118,108,120,145,140,122,135,153,157,133,130,128,109,106,122,133,132,150,156,158,150,137,147,150,146,144,144,149,171,185,200,194,204,211,229,225,235,228,246,249,238,214,228,250,275,311,323,327,341,368,381,395,449,474,505,529,585,638,720,794,896,919,1008,1053,1156,1134,1174,1191,1202,1178,1236,1200,1130,1094,1081,1009,949,890,810,760,690,631,592,561,515,501,489,467,439,388,377,348,345,310,298,279,253,257,259,247,237,223,227,217,210,213,197,197,192,195,198,201,202,211,193,203,198,202,174,164,162,173,170,184,170,168,175,170,170,168,162,149,139,145,151,144,152,155,170,156,149,147,158,171,163,146,151,150,147,137,123,127,136,149,147,124,137,133,129,130,128,139,137,147,141,123,112,136,147,126,117,116,100,110,120,105,91,100,100,105,92,88,78,95,75,75,82,82,80,83,83,66,73,80,76,69,81,93,79,71,80,90,72,72,63,57,53,62,65,49,51,57,73,54,56,78,65,52,58,49,47,56,46,43,50,43,40,39,36,45,28,35,36,43,48,37,36,35,39,31,24,29,37,26,22,36,33,24,31,31,20,30,28,23,21,27,26,29,21,20,22,18,19,19,20,21,20,25,18,12,18,20,20,13,14,21,20,16,18,12,17,20,24,21,20,18,11,17,12,5,11,13,16,13,13,12,12,9,15,13,15,11,12,11,8,13,16,16,16,14,8,8,10,11,11,17,15,15,9,9,13,12,3,11,14,11,14,13,8,7,7,15,12,8,12,14,9,5,2,10,8)

我计算了定义分布和累积分布的方程式:

dFF <- function(x,a,b,c,A,gamma,pos) a + b*x + (c*x^2) + ((A/pi)*(gamma/(((x-pos)^2) + (gamma^2))))

pFF <- function(x,a,b,c,A,gamma,pos) a*x + (b/2)*(x^2) + (c/3)*(x^3) + A/2 + (A/pi)*(atan((x - pos)/gamma))

我相信这些是正确的。根据我的理解,使用fitdist(或mledist)方法仅使用这些定义就可以实现分布拟合:

fitdist(data,'FF', start = list(0,0.3,-0.0004,70000,13,331))
mledist(data,'FF', start = list(0,0.3,-0.0004,70000,13,331))

这将返回语句&#39;函数无法在初始参数下评估&gt; fitdist中的错误(数据,&#34; FF&#34;,start = list(0,0.3,-4e-04,70000,13,331)):函数mle无法估计参数,错误代码为100& #39; 在第一种情况下,在第二种情况下,我只得到一份“NA&#39;估计值。

然后我计算了一个函数来给出分位数分布值以使用其他拟合方法(qmefit):

qFF <- function(p,a,b,c,A,gamma,pos)
{
     qList = c()
     axis = seq(1,600,1)
     aF = dFF(axis,a,b,c,A,gamma,pos)
     arr = histogramCpp(aF) # change data to a histogram format
     for(element in 1:length(p)){
         q = quantile(arr,p[element], names=FALSE)
         qList = c(qList,q)
     }
     return(qList)
}

此代码的一部分需要调用c ++函数(通过使用库Rcpp):

#include <Rcpp.h>
#include <vector>
#include <math.h>
using namespace Rcpp;

// [[Rcpp::export]]
std::vector<int>  histogramCpp(NumericVector x) {
    std::vector<int> arr;
    double number, fractpart, intpart;
    for(int i = 0; i <= 600; i++){
        number = (x[i]);
        fractpart = modf(number , &intpart);
        if(fractpart < 0.5){
            number = (int) intpart;
        }
        if(fractpart >= 0.5){
            number = (int) (intpart+1);
        }
        for(int j = 1; j <= number; j++){
            arr.push_back(i);
        }
   }
   return arr;
}

这种c ++方法只是将数据转换为直方图格式。如果描述数据的向量的第一个元素是4,那么&#39; 1&#39;向返回的矢量等添加4次。这似乎也起作用,因为返回了合理的值。分位数函数图:

Plot of quantiles returned for probabilities from 0 to 1 in steps of 0.001

&#39; qmefit&#39;然后可以通过fitdist函数尝试方法:

fitdist(data,'FF', start = list(0,0.3,-0.0004,70000,13,331), method = 'qme', probs = c(0,0.3,0.4,0.5,0.7,0.9))

我选择了&#39; probs&#39;价值是随机的,因为我不完全理解它们的含义。这可以直接使R会话崩溃,也可以在短暂的口吃之后返回一个&#39; NA&#39;值为估算值和行<std::bad_alloc : std::bad_alloc>

我不确定我是否在这里犯了一个基本错误,并且感谢任何帮助或推荐。

1 个答案:

答案 0 :(得分:0)

最后,我设法使用rPython包和python的lmfit找到了解决方法。它解决了我的问题,对于有同样问题的其他人可能会有用。 R代码如下:

library(rPython)
python.load("pyFit.py")
python.assign("row",pos)
python.assign("vals",vals)
python.exec("FWHM,ERROR,FIT = fitDist(row,vals)")
FWHM = python.get("FWHM")
ERROR = python.get("ERROR")
cFIT = python.get("FIT")

和被调用的python代码是:

from lmfit import  Model, minimize, Parameters, fit_report
from sklearn import mixture
import numpy as np
import matplotlib.pyplot as plt
import math

def cauchyDist(x,a,b,c,d,e,f,g,A,gamma,pos):
    return  a + b*x + c*pow(x,2) + d*pow(x,3) + e*pow(x,4) + f*pow(x,5) + g*pow(x,6) + (A/np.pi)*(gamma/((pow((x-pos),2)) + (pow(gamma,2))))

def fitDist(row, vals):  
    gmod = Model(cauchyDist)

    x = np.arange(0,600)
    result = gmod.fit(vals, x=x, a = 0, b = 0.3, c = -0.0004, d = 0, e = 0, f= 0, g = 0, A = 70000, gamma = 13, pos = row)

    newFile = open('fitData.txt', 'w')
    newFile.write(result.fit_report())
    newFile.close()

    with open('fitData.txt', 'r') as inF:
        for line in inF: 
            if 'gamma:' in line:
                j = line.split()
    inF.close()

    FWHM = float(j[1])
    error = float(j[3])
    fit = result.best_fit
    fit  = fit.tolist()
    return FWHM, error, fit

我增加了多项式的阶数以获得更好的数据拟合并返回FWHM,其误差和拟合值。可能有更好的方法来达到这个目的,但最终的合适是我需要的。

Final fit. Red data points are raw data, the black line is the fitted distribution.