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

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.

Upvotes: 2

Views: 6856

Answers (3)

Kevin Zhu
Kevin Zhu

Reputation: 2846

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)

Upvotes: 3

Dave2e
Dave2e

Reputation: 24149

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.

Upvotes: 0

Patrick
Patrick

Reputation: 513

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

Upvotes: 2

Related Questions