R-fit用户定义分布

R-fit用户定义分布,r,distribution,fitdistrplus,R,Distribution,Fitdistrplus,我试图将我自己的分布与我的数据相匹配,找到分布的最佳参数以匹配数据,并最终找到分布中峰值的半高宽。据我所知,FitDistripPlus软件包就是实现这一点的方法。我知道数据在二次背景上呈现洛伦兹峰的形状 数据曲线图: 使用的原始数据: 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

我试图将我自己的分布与我的数据相匹配,找到分布的最佳参数以匹配数据,并最终找到分布中峰值的半高宽。据我所知,FitDistripPlus软件包就是实现这一点的方法。我知道数据在二次背景上呈现洛伦兹峰的形状

数据曲线图:

使用的原始数据:

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))
这返回语句“function cannot evaluation at initial parameters>Error in fitdist(data,“FF”,start=list(0,0.3,-4e-0470000,13331)):函数mle无法估计参数,错误代码为100”,在第一种情况下,第二种情况下,我仅获得估计值的“NA”列表

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

我随机选择了“probs”值,因为我不完全理解它们的含义。这要么直接导致R会话崩溃,要么在短暂的口吃后返回一个“NA”值列表作为估计值,并返回行


我不确定我是否犯了一个基本的错误,我们非常感谢您的帮助或建议。

最后,我使用python软件包和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
我增加了多项式的阶数以获得更好的数据拟合,并返回了半高宽、误差和拟合值。可能有更好的方法实现这一点,但最终的适合是我所需要的


最后,我使用python包和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
我增加了多项式的阶数以获得更好的数据拟合,并返回了半高宽、误差和拟合值。可能有更好的方法实现这一点,但最终的适合是我所需要的

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")
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