简体   繁体   中英

R prob = TRUE in hist doesn't give out a density distribution

earthquake <- function(lambda=1, n_sim =10, n=100){
  meanls <- c()
  for (i in 1:n){
    meanls <- c(meanls,round(mean(rexp(n_sim,1/lambda)),2))
  }
  return(meanls)
}
xbar <- earthquake(2.4,1000,40)
hist(xbar, prob=TRUE, col="moccasin",las= TRUE)

I have the code above, and it should return a density distribution histogram since I set probability to TRUE, while I just get frequency diagram. Is there anything else I should do with the data?

If you set a random seed you can replicate your results. Otherwise you will need to adjust your xlim= according to your data. You do not say why you are using sd=2.4/sqrt(40)) as the standard deviation instead of sd(xbar) which is what I have used here. That produces a very broad, flat curve that does not match the data at all. If you wanted the standard error curve, that would be sd(xbar)/sqrt(40) .

set.seed(42)
xbar <- earthquake(2.4, 1000, 40)
range(xbar)
# [1] 2.19 2.59
hist(xbar, prob=TRUE, xlim=c(2.1, 2.7), col="moccasin", las= TRUE)
x <- seq(2.1, 2.7, length.out=100)
curve(dnorm(x, mean=mean(xbar), sd=sd(xbar)), col="blue", add=TRUE, lwd=2)
lines(density(xbar), col="red", lwd = 2)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM