在直方图中使用x-log标度拟合法线拟合

时间:2013-11-13 15:07:32

标签: r histogram logarithm

我希望有些人帮助我。我正在处理我的直方图中的正常拟合与x-log量表。我使用对数比例,因为我的数据的正常直方图有一个长尾。我的代码是这样的..

breaks<- c(0,0.01, 0.05, 0.1,0.2,0.5,1,2,5,10,20,50,100,200,300) #bins
major <- c(0.1,1,10,100)

H <- hist(log10(B),plot=B) #using data "B"

plot(H$mids,H$counts,type="n",
  xaxt="n",
  xlab="B",ylab="Counts",
  main="Histogram of B",
  bg="lightgrey"
)
abline(v=log10(breaks),col="lightgrey",lty=2)
abline(v=log10(major),col="lightgrey")
abline(h=pretty(H$counts),col="lightgrey")
plot(H,add=T,freq=T,col="blue")
#Position of ticks
at <- log10(breaks)

#Creation X axis
axis(1,at=at,labels=10^at)
curve(dnorm(x, mean=mean(B), sd=sd(B)), add=TRUE) # 1st try
lines(density(B))  # 2nd try

xfit<-seq(min(B),max(B),length=40) # 3rd try
yfit<-dnorm(xfit,mean=mean(B),sd=sd(B)) 
yfit<-yfit*diff(H$mids[1:2])*length(B) 
lines(xfit, yfit, col="red", lwd=2) 

但是第1次,第2次,第3次尝试都没有用。请让我知道如何在我的直方图中添加正常拟合。非常感谢您的帮助。 Summy

1 个答案:

答案 0 :(得分:2)

我刚刚改变了你的第一次尝试,以便你得到你想要的东西:

B <- rlnorm(10000)
H <- hist(log10(B), freq = FALSE, col="blue", xaxt="n", xlab="B") 
at <- H$mids
axis(1,at=at,labels=round(10^at,2))
curve(dnorm(x, mean=mean(log10(B)), sd=sd(log10(B))), add=TRUE)

希望它有所帮助,

亚历