[R] Probabilities greather than 1 in HIST
David Winsemius
dwinsemius at comcast.net
Thu Mar 3 16:58:57 CET 2011
On Mar 3, 2011, at 8:03 AM, jpmaroco wrote:
> Dear all,
> I am a newbie in R and could not find help on this problem. I am
> trying to
> plot an histogram with probabilities in the y axis. This is the code
> I am
> using:
>
> #TLC uniform
> n=30
> mi=1; mx=6
> nrep=1000
> xbar=rep(0,nrep)
> for (i in 1:nrep) {xbar[i]=mean(runif(n,min=mi,max=mx))}
> hist(xbar,prob=TRUE,breaks="Sturges",xlim=c(1,6),main=paste("n =",n),
> xlab="Média", ylab="Probabilidade")
> curve(dnorm(x,mean=mean(xbar),sd=sd(xbar)),add=TRUE,lwd=2,col="red")
>
> The problem is that I am getting greater than 1 probabilities in the
> Y axis?
> Is there a way to correct this?
Despite the argument name, which I agree suggests that probabilities
will be plotted, what is really described in the help page is that
densities will be plotted, and densities may be greater than 1. You
can suppress plotting of the y-axis, calculate the probabilities for
each of the groups returned by hist, and then use the axes function.
> xhist <- hist(xbar,breaks="Sturges",plot=FALSE)
> yhist <- xhist$counts/sum(xhist$counts)
> yhist
[1] 0.002 0.027 0.087 0.236 0.287 0.228 0.107 0.021 0.004 0.001
> Many thanks in advance.
> Joao
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Probabilities-greather-than-1-in-HIST-tp3333388p3333388.html
> Sent from the R help mailing list archive at Nabble.com.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list