其他分享
首页 > 其他分享> > R语言绘制不同自由度下的卡方分布、t分布和F分布

R语言绘制不同自由度下的卡方分布、t分布和F分布

作者:互联网

# === chi-squared distribution ===
chif <- function(x, df) {
  dchisq(x, df = df)
}

## === chi-squared distribution with df=1,2, 4, 6 and 10 ===
curve(chif(x, df = 1), 0, 20, ylab = "p(x)", lwd = 2)
curve(chif(x, df = 2), 0, 20, col = 2, add = T, lty = 2, lwd = 2)
curve(chif(x, df = 4), 0, 20, col = 3, add = T, lty = 3, lwd = 2)
curve(chif(x, df = 6), 0, 20, col = 4, add = T, lty = 4, lwd = 2)
curve(chif(x, df = 10), 0, 20, col = 5, add = T, lty = 5, lwd = 2)
legend("topright", legend = c("df=1", "df=2", "df=4", "df=6", "df=10"), col = 1:5, lty = 1:5, lwd = 2)


## === chi-squared distribution with df=4,6 and 10 ===
curve(dchisq(x, 4), 0, 20, col = 3, lty = 3, lwd = 2, ylab = "p(x)")
curve(dchisq(x, 6), 0, 20, col = 4, add = T, lty = 4, lwd = 2)
curve(dchisq(x, 10), 0, 20, col = 5, add = T, lty = 5, lwd = 2)
legend("topright", legend = c("df=4", "df=6", "df=10"), col = 3:5, lty = 3:5, lwd = 2)

### quantiles
curve(dchisq(x, 10), 0, 30, col = 1, lty = 1, lwd = 2, ylab = "p(x) of chisq(10)")
lines(c(qchisq(0.95, 10), qchisq(0.95, 10)), c(-0.05, dchisq(qchisq(0.95, 10), 10)), col = 2, lwd = 3, 
      lty = 2)

qchisq(0.95,10)

## ==== t ===
curve(dt(x, 1), -6, 6, ylab = "p(x)", lwd = 2, ylim = c(0, 0.4))
curve(dt(x, 2), -6, 6, col = 2, add = T, lwd = 2)
curve(dt(x, 5), -6, 6, col = 3, add = T, lwd = 2)
curve(dt(x, 10), -6, 6, col = 4, add = T, lwd = 2)
curve(dnorm(x), col = 6, add = T, lwd = 2, lty = 2)
legend("topright", legend = c("df=1", "df=2", "df=5", "df=10", "df=Inf"), col = c(1:4, 6), lty = c(rep(1, 
                                                                                                       4), 2), lwd = 2)

curve(dt(x, 4), -6, 6, col = 4, lwd = 2, ylim = c(0, 0.4), ylab = "p(x)")
curve(dnorm(x), col = 6, add = T, lwd = 2, lty = 2)
legend("topright", legend = c("t(4)", "N(0,1)"), col = c(4, 6), lty = c(1, 2), lwd = 2)

qt(0.025,10)
qt(0.975,10)

## === F ==
curve(df(x, 4, 1), 0, 4, ylab = "p(x)", lwd = 2, ylim = c(0, 0.8))
curve(df(x, 4, 4), 0, 4, col = 2, add = T, lwd = 2)
curve(df(x, 4, 10), 0, 4, col = 3, add = T, lwd = 2)
curve(df(x, 4, 4000), 0, 4, col = 4, add = T, lwd = 2)
legend("topright", legend = c("F(4,1)", "F(4,4)", "F(4,10)", "F(4,4000)"), col = 1:4, lwd = 2)

qf(0.95,10,5)
qf(0.05,5,10)
1/qf(0.05,5,10)

卡方分布

卡方分布
在这里插入图片描述
在这里插入图片描述

t分布

在这里插入图片描述
在这里插入图片描述

F分布

在这里插入图片描述

#卡方分布
> qchisq(0.95,5)
[1] 11.0705
> qchisq(0.95,10)
[1] 18.30704
> qchisq(0.95,15)
[1] 24.99579
> qchisq(0.95,20)
[1] 31.41043
> qchisq(0.95,25)
[1] 37.65248
> qchisq(0.95,30)
[1] 43.77297
#t分布
> qt(0.95,5)
[1] 2.015048
> qt(0.95,10)
[1] 1.812461
> qt(0.95,15)
[1] 1.75305
> qt(0.95,20)
[1] 1.724718
> qt(0.95,25)
[1] 1.708141
> qt(0.95,30)
[1] 1.697261
> qf(0.95,10,5)
[1] 4.735063
> qf(0.95,5,10)
[1] 3.325835
> qf(0.95,5,5)
[1] 5.050329
> qf(0.95,10,10)
[1] 2.978237

标签:10,自由度,curve,df,分布,卡方,0.95,col,lwd
来源: https://blog.csdn.net/weixin_46530492/article/details/114600245