📜  在 R 中绘制 t 分布

📅  最后修改于: 2022-05-13 01:55:36.348000             🧑  作者: Mango

在 R 中绘制 t 分布

t 分布,也称为学生 t 分布,是一种概率分布,用于对样本量很小且输入分布的标准偏差未知的正态分布进行抽样。分布通常形成钟形曲线,即分布呈正态分布,但峰值较低,尾部附近的观测值较多。

t 分布只有一个相关参数,称为自由度 (df)。特定 t 分布曲线的形状取决于所选的自由度 (df) 的数量,该数量等于给定的样本量减 1,即,

df=n−1

可以使用 R 中的 seq() 方法生成坐标向量,该方法用于生成整数的增量序列,为给定的 t 分布提供分布序列。可以使用下面详述的 t 分布函数的各种变体来构建相应的 y 坐标。然后使用 R 编程语言中的 plot() 方法绘制这些图。

dt() 方法

R 中的 dt() 方法用于计算具有指定自由度的 t 分布的概率密度分析。

例子:

R
# generating x coordinates
xpos <- seq(- 100, 100, by = 20) 
 
print ("X coordinates")
print (xpos)
 
# generating y coordinates using dt() method
# degreesoffreedom
degree <- 2
ypos <- dt(xpos, df = degree)
 
print ("Y coordinates")
print (ypos)
 
# plotting t distribution
plot (ypos , type = "l")


R
# generating x coordinates
xpos <- seq(- 100, 100, by = 20) 
 
print ("X coordinates")
print (xpos)
 
# generating y coordinates using dt() method
# degreesoffreedom
degree <- 2
 
ypos <- pt(xpos, df = degree)
 
print ("Y coordinates")
print (ypos)
 
# plotting t distribution
plot (ypos , type = "l")


R
# generating x coordinates
xpos <- seq(0, 1, by = 0.05)
 
# generating y coordinates using dt() method
# degreesoffreedom
degree <- 2
ypos <- qt(xpos, df = degree)
 
# plotting t distribution
plot (ypos , type = "l")


R
# using a random number
n <- 1000
 
# degreesoffreedom
degree <- 2
 
# generating y coordinates using rt() method
ypos <- rt(n , df = degree)
 
# plotting t distribution in the form of
# distribution
hist(ypos,                                       
     breaks = 100,
     main = "")


输出



pt() 方法

R 中的 pt() 方法用于为给定的学生 T 分布生成分布函数。它用于产生累积分布函数。此函数返回任何给定间隔的 t 曲线下的面积。

例子:

电阻

# generating x coordinates
xpos <- seq(- 100, 100, by = 20) 
 
print ("X coordinates")
print (xpos)
 
# generating y coordinates using dt() method
# degreesoffreedom
degree <- 2
 
ypos <- pt(xpos, df = degree)
 
print ("Y coordinates")
print (ypos)
 
# plotting t distribution
plot (ypos , type = "l")

输出



qt() 方法

R 中的 qt() 方法用于计算指定数量自由度的给定 t 分布的分位数函数或逆累积密度函数。它用于计算具有指定自由度的学生 t 分布的第 n 个百分位数。

例子:

电阻

# generating x coordinates
xpos <- seq(0, 1, by = 0.05)
 
# generating y coordinates using dt() method
# degreesoffreedom
degree <- 2
ypos <- qt(xpos, df = degree)
 
# plotting t distribution
plot (ypos , type = "l")

输出



rt() 方法

rt() 方法用于使用指定数量的自由度随机生成 t 分布。可以生成 n 个随机样本。

例子:

电阻

# using a random number
n <- 1000
 
# degreesoffreedom
degree <- 2
 
# generating y coordinates using rt() method
ypos <- rt(n , df = degree)
 
# plotting t distribution in the form of
# distribution
hist(ypos,                                       
     breaks = 100,
     main = "")

输出