Plot histogram and lognormal fit on the same time

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Plot histogram and lognormal fit on the same time

Eric Leroy
Hi,

I would like to plot the histogram of data and fit it with a lognormal
distribution.

The ideal, would be to superimpose the fit on the histogram and write
the results of the fit on the figure.

Right now, I was able to plot the histogram and fit the density with a
lognormal, but I can't combine all together.

Here is the code I wrote :

histdata <- hist(dataframe$data)

library(MASS)

fitdistr(histdata$density, "lognormal")

Can you help me ?

Best regards,

--

*Eric Leroy*

/Responsable de la plateforme microscopie électronique/

/Correspondant Sécurité des Systèmes Informatiques de l'ICMPE/

ICMPE - UMR 7182 - CNRS - UPEC

2/8, rue Henri Dunant

94320 Thiais

Tél : 01.49.78.12.09

Fax : 01.49.78.12.03

courriel : [hidden email] <mailto:[hidden email]>

Page Web : : http://www.icmpe.cnrs.fr <http://www.icmpe.cnrs.fr>


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot histogram and lognormal fit on the same time

R help mailing list-2
Two solutions not exactly equivalent ;

data <- rlnorm(100, meanlog = 1, sdlog = 1)
histdata <- hist(data, ylim=c(0, 100))
library(MASS)

f <- fitdistr(data, "lognormal")

f$estimate

lines(x=seq(from=0, to=50, by=0.1),
 � y=dlnorm(x=seq(from=0, to=50, by=0.1), meanlog =
f$estimate["meanlog"], sdlog = f$estimate["sdlog"])*400
)

library(HelpersMG)

m <- modeled.hist(breaks=histdata$breaks, FUN=plnorm,
 ����������������������������� meanlog = f$estimate["meanlog"], sdlog =
f$estimate["sdlog"], sum = 100)

points(m$x, m$y, pch=19, col="red")

Marc Girondot

Le 21/01/2021 � 12:54, Eric Leroy a �crit�:

> Hi,
>
> I would like to plot the histogram of data and fit it with a lognormal
> distribution.
>
> The ideal, would be to superimpose the fit on the histogram and write
> the results of the fit on the figure.
>
> Right now, I was able to plot the histogram and fit the density with a
> lognormal, but I can't combine all together.
>
> Here is the code I wrote :
>
> histdata <- hist(dataframe$data)
>
> library(MASS)
>
> fitdistr(histdata$density, "lognormal")
>
> Can you help me ?
>
> Best regards,
>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot histogram and lognormal fit on the same time

Bert Gunter-2
In reply to this post by Eric Leroy
In future, you should try to search before posting. I realize that getting
good search terms can sometimes be tricky, but not in this case: 'plot
density with histogram in R' or similar on rseek.org or just on your usual
search platform brought up several ways to do it.

As a (slightly offtopic) side issue, be careful with this: both histograms
and smooth density estimates are just that: density **estimates** that
depend on both the data and various parameters, e.g. the location of the
bins for histograms and kernel for kernel density estimates. You may
therefore get plots where the two do not appear to "match" when you use the
defaults for each.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jan 21, 2021 at 3:54 AM Eric Leroy <[hidden email]> wrote:

> Hi,
>
> I would like to plot the histogram of data and fit it with a lognormal
> distribution.
>
> The ideal, would be to superimpose the fit on the histogram and write
> the results of the fit on the figure.
>
> Right now, I was able to plot the histogram and fit the density with a
> lognormal, but I can't combine all together.
>
> Here is the code I wrote :
>
> histdata <- hist(dataframe$data)
>
> library(MASS)
>
> fitdistr(histdata$density, "lognormal")
>
> Can you help me ?
>
> Best regards,
>
> --
>
> *Eric Leroy*
>
> /Responsable de la plateforme microscopie électronique/
>
> /Correspondant Sécurité des Systèmes Informatiques de l'ICMPE/
>
> ICMPE - UMR 7182 - CNRS - UPEC
>
> 2/8, rue Henri Dunant
>
> 94320 Thiais
>
> Tél : 01.49.78.12.09
>
> Fax : 01.49.78.12.03
>
> courriel : [hidden email] <mailto:[hidden email]>
>
> Page Web : : http://www.icmpe.cnrs.fr <http://www.icmpe.cnrs.fr>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot histogram and lognormal fit on the same time

Rui Barradas
In reply to this post by R help mailing list-2
Hello,

A solution based on Marc's first one, maybe easier? (It doesn't rely on
multiplying the dlnorm values by 400 since it plots the histogram with
freq = FALSE.)


set.seed(2020)
data <- rlnorm(100, meanlog = 1, sdlog = 1)

library(MASS)

f <- fitdistr(data, "lognormal")
f$estimate

p <- pretty(range(data))
x <- seq(from = min(p), to = max(p), by = 0.1)

hist(data, freq = FALSE)
lines(x, y = dlnorm(x,
                     meanlog = f$estimate["meanlog"],
                     sdlog = f$estimate["sdlog"])
)


Hope this helps,

Rui Barradas


Às 13:12 de 21/01/21, Marc Girondot via R-help escreveu:

> Two solutions not exactly equivalent ;
>
> data <- rlnorm(100, meanlog = 1, sdlog = 1)
> histdata <- hist(data, ylim=c(0, 100))
> library(MASS)
>
> f <- fitdistr(data, "lognormal")
>
> f$estimate
>
> lines(x=seq(from=0, to=50, by=0.1),
>   � y=dlnorm(x=seq(from=0, to=50, by=0.1), meanlog =
> f$estimate["meanlog"], sdlog = f$estimate["sdlog"])*400
> )
>
> library(HelpersMG)
>
> m <- modeled.hist(breaks=histdata$breaks, FUN=plnorm,
>   ����������������������������� meanlog = f$estimate["meanlog"], sdlog =
> f$estimate["sdlog"], sum = 100)
>
> points(m$x, m$y, pch=19, col="red")
>
> Marc Girondot
>
> Le 21/01/2021 � 12:54, Eric Leroy a �crit�:
>> Hi,
>>
>> I would like to plot the histogram of data and fit it with a lognormal
>> distribution.
>>
>> The ideal, would be to superimpose the fit on the histogram and write
>> the results of the fit on the figure.
>>
>> Right now, I was able to plot the histogram and fit the density with a
>> lognormal, but I can't combine all together.
>>
>> Here is the code I wrote :
>>
>> histdata <- hist(dataframe$data)
>>
>> library(MASS)
>>
>> fitdistr(histdata$density, "lognormal")
>>
>> Can you help me ?
>>
>> Best regards,
>>
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot histogram and lognormal fit on the same time

aBBy Spurdle, ⍺XY
In reply to this post by Bert Gunter-2
Sorry, Bert.
The fitdistr function estimates parameters via maximum likelihood.
(i.e. The "lognormal" part of this, is not a kernel).


On Fri, Jan 22, 2021 at 5:14 AM Bert Gunter <[hidden email]> wrote:

>
> In future, you should try to search before posting. I realize that getting
> good search terms can sometimes be tricky, but not in this case: 'plot
> density with histogram in R' or similar on rseek.org or just on your usual
> search platform brought up several ways to do it.
>
> As a (slightly offtopic) side issue, be careful with this: both histograms
> and smooth density estimates are just that: density **estimates** that
> depend on both the data and various parameters, e.g. the location of the
> bins for histograms and kernel for kernel density estimates. You may
> therefore get plots where the two do not appear to "match" when you use the
> defaults for each.
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Thu, Jan 21, 2021 at 3:54 AM Eric Leroy <[hidden email]> wrote:
>
> > Hi,
> >
> > I would like to plot the histogram of data and fit it with a lognormal
> > distribution.
> >
> > The ideal, would be to superimpose the fit on the histogram and write
> > the results of the fit on the figure.
> >
> > Right now, I was able to plot the histogram and fit the density with a
> > lognormal, but I can't combine all together.
> >
> > Here is the code I wrote :
> >
> > histdata <- hist(dataframe$data)
> >
> > library(MASS)
> >
> > fitdistr(histdata$density, "lognormal")
> >
> > Can you help me ?
> >
> > Best regards,
> >
> > --
> >
> > *Eric Leroy*
> >
> > /Responsable de la plateforme microscopie électronique/
> >
> > /Correspondant Sécurité des Systèmes Informatiques de l'ICMPE/
> >
> > ICMPE - UMR 7182 - CNRS - UPEC
> >
> > 2/8, rue Henri Dunant
> >
> > 94320 Thiais
> >
> > Tél : 01.49.78.12.09
> >
> > Fax : 01.49.78.12.03
> >
> > courriel : [hidden email] <mailto:[hidden email]>
> >
> > Page Web : : http://www.icmpe.cnrs.fr <http://www.icmpe.cnrs.fr>
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot histogram and lognormal fit on the same time

Bert Gunter-2
OK, but that's not the point of my comment.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jan 22, 2021 at 5:03 PM Abby Spurdle <[hidden email]> wrote:

> Sorry, Bert.
> The fitdistr function estimates parameters via maximum likelihood.
> (i.e. The "lognormal" part of this, is not a kernel).
>
>
> On Fri, Jan 22, 2021 at 5:14 AM Bert Gunter <[hidden email]>
> wrote:
> >
> > In future, you should try to search before posting. I realize that
> getting
> > good search terms can sometimes be tricky, but not in this case: 'plot
> > density with histogram in R' or similar on rseek.org or just on your
> usual
> > search platform brought up several ways to do it.
> >
> > As a (slightly offtopic) side issue, be careful with this: both
> histograms
> > and smooth density estimates are just that: density **estimates** that
> > depend on both the data and various parameters, e.g. the location of the
> > bins for histograms and kernel for kernel density estimates. You may
> > therefore get plots where the two do not appear to "match" when you use
> the
> > defaults for each.
> >
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along
> and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Thu, Jan 21, 2021 at 3:54 AM Eric Leroy <[hidden email]> wrote:
> >
> > > Hi,
> > >
> > > I would like to plot the histogram of data and fit it with a lognormal
> > > distribution.
> > >
> > > The ideal, would be to superimpose the fit on the histogram and write
> > > the results of the fit on the figure.
> > >
> > > Right now, I was able to plot the histogram and fit the density with a
> > > lognormal, but I can't combine all together.
> > >
> > > Here is the code I wrote :
> > >
> > > histdata <- hist(dataframe$data)
> > >
> > > library(MASS)
> > >
> > > fitdistr(histdata$density, "lognormal")
> > >
> > > Can you help me ?
> > >
> > > Best regards,
> > >
> > > --
> > >
> > > *Eric Leroy*
> > >
> > > /Responsable de la plateforme microscopie électronique/
> > >
> > > /Correspondant Sécurité des Systèmes Informatiques de l'ICMPE/
> > >
> > > ICMPE - UMR 7182 - CNRS - UPEC
> > >
> > > 2/8, rue Henri Dunant
> > >
> > > 94320 Thiais
> > >
> > > Tél : 01.49.78.12.09
> > >
> > > Fax : 01.49.78.12.03
> > >
> > > courriel : [hidden email] <mailto:[hidden email]>
> > >
> > > Page Web : : http://www.icmpe.cnrs.fr <http://www.icmpe.cnrs.fr>
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.