Return value of fitdistr function explanation

Viewed 182

I have the following generated dataset

library(MASS)    
df <- data.frame(product= sample(x= c("toyota","honda","nissan","bmw"),size = 1000 ,replace = TRUE),
                  parameter = sample(x= c("X","Y", "A"),size = 1000 ,replace = TRUE),
                  value= rgamma(1000, shape = 5, rate = 0.1))

and I want to fit the lognormal distribution on column "value" and I use the following code

dist_par <- fitdistr(unlist(df["value"]), "lognormal")

the result is something like below:

  meanlog    sdlog 
  3.8416    0.4292 
 (0.0458)  (0.0324)

I have two questions:

  1. I read the help and I guess that the meanlog and sdlog estimations are shown on the first row:
  meanlog    sdlog 
  3.8416    0.4292

but the second row of numbers (numbers in parentheses) are confusing, what are they?

  meanlog    sdlog 
 ....       ....
 (0.0458)  (0.0324)
  1. I know the result of fitdistr is a list but I don't know how to have access to those four values. For instance how can I get 3.8416 ?

If I run

dist_par[1]

then I get

  meanlog   sdlog 
  3.842   0.429

and if I run:

dist_par[1,1]

then I get the following error:

Error in dist_par[1, 1] : incorrect number of dimensions
1 Answers

According to the ?fitdistr documentation

An object of class "fitdistr", a list with four components,

estimate - the parameter estimates,

sd - the estimated standard errors,

vcov - the estimated variance-covariance matrix, and

loglik - the log-likelihood.

This would be evident if we check the structure

 str(out)
List of 5
 $ estimate: Named num [1:2] 3.801 0.455
  ..- attr(*, "names")= chr [1:2] "meanlog" "sdlog"
 $ sd      : Named num [1:2] 0.0144 0.0102
  ..- attr(*, "names")= chr [1:2] "meanlog" "sdlog"
 $ vcov    : num [1:2, 1:2] 0.000207 0 0 0.000103
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "meanlog" "sdlog"
  .. ..$ : chr [1:2] "meanlog" "sdlog"

i.e. the print method returns the 'estimatte and inside the parentheses the sd and as they are list the [1,1] doesn't work, we need to use standard extraction methods i.e. either $ or [[

> out
    meanlog       sdlog   
  3.80075311   0.45468543 
 (0.01437842) (0.01016708)
> out$estimate
  meanlog     sdlog 
3.8007531 0.4546854 
> out$estimate[["meanlog"]]
[1] 3.800753
> out$sd
   meanlog      sdlog 
0.01437842 0.01016708 

i.e inside the list, the elements are just named vectors, so use the [ or [[ to extract by name

Related