Skip to content

fitdistr for t distribution

6 messages · lagreene, Jorge Ivan Velez, Martin Maechler +2 more

#
Hi,
I was wondering if anyone could tell me how m and s are calculated for a t
distribution?

I thought m was the sample mean and s the standard deviation- but obviously
I'm wrong as this doesn'y give the same answer.

Thank you
#
Thanks Jorge,

but I still don't understand where they come from.  when I use: 
fitdistr(mydata, "t", df = 9) and get values for m and s, and the variance
of my data should be the df/s?

I jsut want to be able to confirm how m and s are calculated

mydt <- function(x, m, s, df) dt((x-m)/s, df)/s
fitdistr(x2, mydt, list(m = 0, s = 1), df = 9, lower = c(-Inf, 0))

Thanks anyway for the help!
Jorge Ivan Velez wrote:

  
    
1 day later
#
l> Thanks Jorge,

    l> but I still don't understand where they come from.  when I use: 
    l> fitdistr(mydata, "t", df = 9) and get values for m and s, and the variance
    l> of my data should be the df/s?

definitely *not*;  How did you get to this completely wrong formula?
 
    l> I jsut want to be able to confirm how m and s are calculated
 
by maximum likelihood.
And, of course, only for the normal (aka Gaussian) are the ML
estimates of mu the artithmetic mean and of sigma  (n-1)/n * sd(x)
{i.e. even *there* the ML estimate of s is *not* the SD}

As you can read on  ?dt,
the variance of a (0,1)-t-distribution is  df / (df - 2)
and hence only defined for df > 2.
Consequently, the variance of a  (mu,sigma)-t-distribution is

   sigma^2 * df / (df - 2)

    l> mydt <- function(x, m, s, df) dt((x-m)/s, df)/s
    l> fitdistr(x2, mydt, list(m = 0, s = 1), df = 9, lower = c(-Inf, 0))

{this is copy-pasted from example(dt);
 the examples have nice comments there....}
l> Jorge Ivan Velez wrote:
>> 
    >> Dear lagreene,
    >> See the second example in
    >> 
    >> require(MASS)
    >> ?fitdistr
    >> 
    >> HTH,
    >> 
    >> Jorge
    >> 
    >>
>> On Thu, May 14, 2009 at 7:15 PM, lagreene <lagreene101 at gmail.com> wrote:
>> 
    >>> 
    >>> Hi,
    >>> I was wondering if anyone could tell me how m and s are calculated for a
    >>> t
    >>> distribution?
    >>> 
    >>> I thought m was the sample mean and s the standard deviation- but
    >>> obviously
    >>> I'm wrong as this doesn'y give the same answer.
    >>> 
    >>> Thank you
#
On Fri, May 15, 2009 at 6:22 AM, lagreene <lagreene101 at gmail.com> wrote:
I've wondered the same kind of thing and I've learned the answer is
easy!  It is not so easy for all R functions, but did you try this
with fitdistr?
the output that follows is the ACTUAL FORMULA that is used to make the
calculations!

I've not yet mastered the art of getting code for some functions.
function (object, ...)
UseMethod("predict")
<environment: namespace:stats>

But I know there is a way to get that code if you know the correct way
to run getS3method().  But I usually just go read the R source code
rather than puzzle over that.

  
    
#
In addition to seeing the code by typing the name of the function 
(and copying it from there into a file), you can also enter 
"debug(fitdistr)", for example.  Then the next time you use "fitdistr", 
either directly  or indirectly, it puts you in the environment of that 
function, and you can walk through it line by line, examining objects, 
changing them, etc. 


      To get the code for an S3 generic function like "predict", use the 
"methods" function, followed, e.g., by "getAnywhere". 


      Hope this helps. 
      Spencer Graves
Paul Johnson wrote: