使用R将t分布分布到我的直方图中?

问题描述 投票:0回答:2

如何使用R在我的直方图上叠加t密度?这是我的功能:

simfun <- function(a=56.25102409,b=1.78977412,c=0.08664925,n=18,x1.sd=18.87671,x2.sd=18.87671,e.sd=18.87671) {
   X1 <- rnorm(n, mean=0, sd=x1.sd)
   X2 <- rnorm(n, mean=0, sd=x2.sd) 
   e <-  rnorm(n, mean=0, sd=e.sd)
   Z <- a+b*X1+c*X2+e 
   data.frame(X1,X2,Z)
}

statfun <- function(samples) {
    coef(lm(Z~X1+X2,data=samples))
}

library(plyr)
B=raply(1000,statfun(simfun()))

(hist(B[,2]))
r overlay
2个回答
2
投票

将最后一行更改为:

hist(B[,2], prob=TRUE)

要正确缩放,然后执行

curve( dt(x, df=15), add=TRUE, col='blue' )

df和颜色更改为所需的任何值。


0
投票

截至2019年11月,我发现获得该地块的方法是新建一个地块。显然,将dtcurve结合使用时,假设x已标准化。 df参数可以用fitdistr包中的MASS估算。

fit.t.tc <- fitdistr(B[,2], "t", hessian = TRUE)
(param.t=fit.t.tc$estimate)

dh=hist(B[,2], prob=TRUE)
#to get the scaling correct, then do
curve( dt(x, df=param.t["df"]), add=TRUE, col='blue' ) # ??
par(new = TRUE)
ss=seq(range(dh$mids)[1],range(dh$mids)[2],length.out = 1000)
x=((ss-param.t["m"])/param.t["s"])
plot(x,100*dt(x=x,df=param.t["df"]), type="l",
      col="red", lwd=3,xlab="",axes=F,ylab="")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.