1 绘制正态分布概率密度曲线

par(mai = c(0.6,0.6,0.1,0.1),cex = 0.7)

curve(dnorm(x,-2,1),from = -6, to = 2,
      xlim = c(-6,6),ylab = "Density")

abline(h = 0)

segments(-2,0,-2,dnorm(-2,-2,1),lty =2)

curve(dnorm(x,-2,1.5),from = -7, to = 3, add = T,
      xlim = c(-6,6),col = "red")

curve(dnorm(x,2,1.5),from = -3, to = 7, add = T,
      xlim = c(-6,6),col = "blue")

abline(h = 0)

segments(2,0,2,dnorm(2,2,1.5),lty =2)

legend("topright",inset = 0.01,
       legend = c("N(-2,1)","N(-2,1.5^2)","N(2,1.5^2)"),
       lty = 1, 
       col = c("black","red","blue"), 
       fill = c("black","red","blue"),
       box.col = "grey", cex = 0.8)

2 绘制t分布概率密度曲线

par(mai = c(0.6,0.6,0.1,0.1),cex = 0.7)

curve(dt(x,2),from = -6, to = 6,
      xlim = c(-6,6),ylim = c(0,0.5), ylab = "Density")

abline(h = 0)

segments(0,0,0,dt(0,2),lty =2)

curve(dt(x,5),from = -6, to = 6, add = T,
      xlim = c(-6,6),col = "red")

curve(dt(x,10),from = -3, to = 7, add = T,
      xlim = c(-6,6),col = "blue")

abline(h = 0)

legend("topright",inset = 0.01,
       legend = c("t(2)","t(5)","t(10)"),
       lty = 1, 
       col = c("black","red","blue"), 
       fill = c("black","red","blue"),
       box.col = "grey", cex = 0.8)

3 绘制chisq分布概率密度曲线

par(mai = c(0.6,0.6,0.1,0.1),cex = 0.7)

curve(dchisq(x,5),from = 0, to = 60,
      xlim = c(0,60),ylim = c(0,0.2), ylab = "Density")

abline(h = 0)

curve(dchisq(x,10),from = 0, to = 60, add = T,
      xlim = c(0,60),col = "red")

curve(dchisq(x,30),from = 0, to = 60, add = T,
      xlim = c(0,60),col = "blue")

abline(h = 0)

legend("topright",inset = 0.01,
       legend = c("chisq(3)","chisq(5)","chisq(15)"),
       lty = 1, 
       col = c("black","red","blue"), 
       fill = c("black","red","blue"),
       box.col = "grey", cex = 0.8)

4 绘制F分布概率密度曲线

par(mfrow = c(1,1))

par(mai = c(0.6,0.6,0.1,0.1),cex = 0.7)
curve(df(x,1,5),from = 0, to = 5,
      xlim = c(0,5),ylim = c(0,1), ylab = "Density")
abline(h = 0)
curve(df(x,6,8),from = 0, to = 5, add = T,
      xlim = c(0,5),col = "red")
curve(df(x,15,20),from = 0, to = 5, add = T,
      xlim = c(0,5),col = "blue")
abline(h = 0)
legend("topright",inset = 0.01,legend = c("F(1,5)","F(6,8)","F(15,20"),
       lty = 1:3, col = c("black","red","blue"), fill =
         c("black","red","blue"),
       box.col = "grey", cex = 0.8)

5 for loop

for (i in 1:5) {
  print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
for (i in seq(0, 10, 2)) {
  print(c(i, i^2))
}
[1] 0 0
[1] 2 4
[1]  4 16
[1]  6 36
[1]  8 64
[1]  10 100
a <- numeric(5)
for (i in 1:5) {
  a[i] <- i^2
}
a
[1]  1  4  9 16 25
for (i in 1:5) {
  for (j in 1:2) {
    print(i+j)
    }
}
[1] 2
[1] 3
[1] 3
[1] 4
[1] 4
[1] 5
[1] 5
[1] 6
[1] 6
[1] 7

6 循环语句绘制卡方分布概率密度曲线

6.1 用plot()绘制

# 定义x轴范围
x <- seq(0, 200, length.out = 1000)

# 创建画布
plot(x, dchisq(x, df = 1), 
     type = "l", 
     xlim = c(0, 200), 
     ylim = c(0, 0.3), 
     xlab = "x", 
     ylab = "Density")

# 添加卡方分布概率密度曲线
for (i in 2:100) {
  lines(x, dchisq(x, i), col = i)
  Sys.sleep(0.1)
}

6.2 ggplot2绘制

library(tidyverse)

# 创建数据框
df <- data.frame(x = seq(0, 60, length.out = 1000))

color <- colorRampPalette(c("cyan4", "cyan")) # 创建颜色函数


# 创建画布
p <- ggplot(df, aes(x)) +
  xlim(0, 60) + 
  ylim(0, 0.25) +
  labs(title = expression(paste("Probability Density Curves of ", 
                                chi^2,
                                " with df = 1 ~ 30")),
       x = "x", y = "Density") +
  theme_bw()

# 循环添加概率密度曲线
for (i in 1:30) {
  df$y <- dchisq(df$x, i)
  p <- p + geom_line(data = df,
                     aes(y = y),
                     color = color(30)[i],
                     linewidth = 0.5)
}

p
Warning: Removed 24 rows containing missing values or values outside the scale range
(`geom_line()`).

7 循环语句绘制t分布概率密度曲线

7.1 用plot()绘制

library(tidyverse)
# df = 1~30

#方法一:基础绘图工具
# 定义x轴范围
x <- seq(-5, 5, length.out = 100)

# 创建画布
plot(x, dt(x, df = 1), 
     type = "l", 
     xlim = c(-5, 5), 
     ylim = c(0, 0.4), 
     xlab = "x", 
     ylab = "Density")

lines(x, dnorm(x), col = "red", lwd = 1)

# 添加t分布概率密度曲线
for (i in 2:30) {
  lines(x, dt(x, i), col = i)
  Sys.sleep(0.3)
}

7.2 用ggplot2()绘制

library(tidyverse)
# 创建数据框
df <- data.frame(x = seq(-5, 5, length.out = 100)) # 创建数据框

color <- colorRampPalette(c("cyan4", "cyan")) # 创建颜色渐变函数

# 创建画布
p <- ggplot(df, aes(x)) +
  geom_line(y = dnorm(df$x), # dnorm() 正态分布密度函数
            linewidth = 0.5)+
  xlim(-5, 5) + 
  ylim(0, 0.4) +
  labs(title = "t distribution df = 1-30",
       x = "x", y = "Density") +
  theme_bw()
p

# 循环添加概率密度曲线
for (i in 1:30) {
  df$y <- dt(df$x, i)
  p <- p + geom_line(data = df,
                     aes(y = y),
                     color = color(30)[i],
                     linewidth = 0.1)
}

print(p)

8 绘制卡方分布的直⽅图

par(mfrow = c(2,3),             # 2行3列
    mai = c(0.6,0.6,0.2,0.1),   #
    cex = 0.7) #

n=5000

df <- c(2,5,10,15,20,30) 

for (i in 1:6) {
  x <- rchisq(n,df[i])
  hist(x,xlim = c(0,60), prob = T, col = 5, 
       xlab = expression(chi^2),
       ylab = "Density", 
       main = paste("df=", df[i]))
  curve(dchisq(x,df[i]),col = 2, add = T)
}

par(mfrow = c(2,3),mai = c(0.6,0.6,0.2,0.1),cex = 0.7)
n=5000
df <- c(2,5,10,15,20,30)
for (i in 1:6) {
  x <- rchisq(n,df[i])
  hist(x,xlim = c(0,60), prob = T, xlab = expression(chi^2),
       ylab = "Density", main = paste("df=", df[i]),col = df[i])
  curve(dchisq(x,df[i]),col = df[i], add = T)
}

9 本章习题

9.1 绘制概率密度曲线

1.在同⼀个坐标系下绘制N(0,1),N(-1,0.25),N(2,4)的概率密度曲线。

2.在同⼀个坐标系下绘制N(0,1), t(2), t(5),t(30)的概率密度曲线。

3.在同⼀个坐标系下绘制chisq(5), chisq(10), chisq(30)的概率密度曲线。

4.同⼀个坐标系下绘制F(2, 5), F(5, 10)的概率密度曲线。

9.2 绘制不同自由度下卡方分布的直方图。

用循环语句完成下列图形的绘制:

1.⽣成1000个服从自由度为4的卡方分布的随机数,绘制其直⽅图。

2.⽣成1000个服从自由度为8的卡方分布的随机数,绘制其直⽅图。

3.⽣成1000个服从自由度为12的卡方分布的随机数,绘制其直⽅图。

4.⽣成1000个服从自由度为16的卡方分布的随机数,绘制其直⽅图。

5.⽣成1000个服从自由度为20的卡方分布的随机数,绘制其直⽅图。

6.⽣成1000个服从自由度为24的卡方分布的随机数,绘制其直⽅图。