How to fit a t-distribution with scale and location parameters in fitdistrplus

我怕爱的太早我们不能终老 提交于 2020-04-30 07:37:23

问题


How can I use fitdistrplus to estimate the location a scale parameters of a t-distribution? I know that I need to provide initial values (in MASS it works very well) but in this package only the df is allowed. Do you have some solution?

Many thanks.


回答1:


The fitdist function in the fitdistrplus package uses the distribution functions based on the distr parameter. So given this code:

data = 1.5*rt(10000,df=5) + 0.5
fit1 <- fitdist(data,"t",start=list(df=3))

fitdist is using the R functions rt,dt,pt and qt. But those functions do not support a location and scale parameter (and hence the above code will only optimise the df parameter and provide a very bad fit). So the solution is to use a version of the t-distribution that does provide the parameters you want. The metRology package provides such a version metRology. The distribution in that package is called t.scaled, and the appropriate functions are defined (rt.scaled,dt.scaled,pt.scaled and qt.scaled).

Now you can fit for the three parameters df, mean and sd:

> library("metRology")
> fit2 <- fitdist(data,"t.scaled",
                  start=list(df=3,mean=mean(data),sd=sd(data)))



回答2:


The Student t distribution should take 2 parameters, the number of degree of freedom and the offset from zero number. See if this works for you:

library(fitdistrplus)
#sample data
x<-rnorm(10, 2)

fitdist(x, "t", start= list(df=length(x), ncp=mean(x)))

The two reported values should be the estimated degrees of freedom and the offset values.




回答3:


The location and scale parameters in base R can be extended according to this Wikipedia article.

library(fitdistrplus)
x<-rt(100,23)
dt_ls <- function(x, df=1, mu=0, sigma=1) 1/sigma * dt((x - mu)/sigma, df)
pt_ls <- function(q, df=1, mu=0, sigma=1)  pt((q - mu)/sigma, df)
qt_ls <- function(p, df=1, mu=0, sigma=1)  qt(p, df)*sigma + mu
rt_ls <- function(n, df=1, mu=0, sigma=1)  rt(n,df)*sigma + mu
fit.t<-fitdist(x, 't_ls', start =list(df=1,mu=mean(x),sigma=sd(x))) 
summary(fit.t)


来源:https://stackoverflow.com/questions/38668587/how-to-fit-a-t-distribution-with-scale-and-location-parameters-in-fitdistrplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!