R语言绘制直方图,
直方图:
核密度函数:
练习题目1:
绘制出15位同学体重的直方图和核密度估计图,并与正态分布的概率密度函数作对比
代码如下:
> w <- c(75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7, 63.5,
+ 66.6, 64.0, 57.0, 69.0, 56.9, 50.0, 72.0)
> hist(w, freq = FALSE)
> lines(density(w), col = "blue")
> x <- 44:76
> lines(x, dnorm(x, mean(w), sd(w)), col = "red")
经验分布:
练习题目2:
绘制出15位同学体重的经验分布图和正态分布曲线:
代码:
> w <- c(75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7, 63.5,
+ 66.6, 64.0, 57.0, 69.0, 56.9, 50.0, 72.0)
> plot(ecdf(w),verticals = TRUE, do.p = FALSE)
> x <- 44:78
> lines(x, pnorm(x, mean(w), sd(w)))