0

如何使用 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]))
4

2 回答 2

2

将最后一行更改为:

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

要正确缩放比例,然后执行

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

df和颜色更改为您想要的任何值。

于 2013-09-30T21:09:47.250 回答
1

截至 2019 年 11 月,我发现获得此情节的方式是使用新情节。显然,使用dtwithcurve假设x是标准化的。df参数可以用from fitdistrpackageMASS估计。

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="")

在此处输入图像描述

于 2019-11-08T17:25:06.360 回答