[英]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)
我有上面的代碼,它應該返回一個密度分布直方圖,因為我將概率設置為 TRUE,而我只是得到頻率圖。 我還應該對數據做些什么嗎?
如果你設置一個隨機種子,你可以復制你的結果。 否則,您將需要根據您的數據調整您的xlim=
。 你沒有說為什么你使用sd=2.4/sqrt(40))
作為標准偏差,而不是我在這里使用的sd(xbar)
。 這會產生一條非常寬泛、平坦的曲線,與數據完全不匹配。 如果您想要標准誤差曲線,那就是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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.