19th Ave New York, NY 95822, USA

# 统计代写| Recap stat代写

## 统计代考

$\begin{array}{ll}3.11 & \mathrm{R}\end{array}$
Distributions in $\mathrm{R}$
All of the named distributions that we’ll encounter in this book have been im-
plemented in $\mathrm{R}$. In this section we’ll explain how to work with the Binomial
and Hypergeometric distributions in $\mathrm{R}$. We will also explain in general how
to generate r.v.s from any discrete distribution with a finite support. Typing
help(distributions) gives a handy list of built-in
available through $\mathrm{R}$ packages that can be loaded.
In general, for many named discrete distributions, three functions starting with $d$,
the function starting with $\mathrm{p}$ is not the PMF, but rather is the CDF.
Binomial distribution
The Binomial distribution is associated with the following three $\mathrm{R}$ functions:
dbinom, pbinom, and rbinom. For the Bernoulli distribution we can just use the
Binomial functions with $n=1$.

• dbinom is the Binomial PMF. It takes three inputs: the first is the value of $x$
at which to evaluate the PMF, and the second and third are the parameters $n$
and p. For example, dbinom $(3,5,0.2)$ returns the probability $P(X=3)$ where
$X \sim \operatorname{Bin}(5,0.2) .$ In other words
dbinom $(3,5,0.2)=\left(\begin{array}{l}5 \ 3\end{array}\right)(0.2)^{3}(0.8)^{2}=0.0512 .$
Binomial CDF. It takes three inputs: the first is the value of $x$
luate the CDF, and the second and third are the parameters.
• pbinom is the Binomial CDF. It takes three inputs: the first is the value of $x$
at which to evaluate the CDF, and the second and third are the parameters.
pbinom $(3,5,0.2)$ is the probability $P(X \leq 3)$ where $X \sim \operatorname{Bin}(5,0.2)$. So
pbinom $(3,5,0.2)=\sum_{k=0}^{3}\left(\begin{array}{l}5 \ k\end{array}\right)(0.2)^{k}(0.8)^{5-k}=0.9933$
• rbinom is a function for generating Binomial random variables. For rbinon, the
first input is how many r.v.s we want to generate, and the second and third
inputs are still the parameters. Thus the command rbinom $(7,5,0.2)$ produces
realizations of seven i.i.d. Bin(5,0.2) r.v.s. When we ran this command, we got
$\begin{array}{lllllll}2 & 1 & 0 & 0 & 1 & 0 & 0\end{array}$
but you’ll probably get something different when you try it!
Random variables and their distributions
We can also evaluate PMFs and CDFs at an entire vector of values. For exam-
ple, recall that $0: \mathrm{n}$ is a quick way to list the integers from 0 to $n$. The command
dbinom $(0: 5,5,0.2)$ returns 6 numbers, $P(X=0), P(X=1), \ldots, P(X=5)$, where
$X \sim \operatorname{Bin}(5,0.2)$
Hypergeometric distribution
The Hypergeometric distribution also has three functions: dhyper, phyper, and
rhyper. As one might expect, dhyper is the Hypergeometric PMF, phyper is the
rhyper. As one might expect, dhyper is the Hypergeometric PNF, phyper is the Hypergeometric CDF, and rhyper generates Hypergeometric r.v.s. Since the Hy-
Hypergeometric CDF, and rhyper generates Hypergeometric r.v.s. Since the Hy- pergeometric distribution has three parameters, each of these functions takes four
inputs. For dhyper and phyper, the first input is the value at which we wish to
evaluate the PMF or CDF, and the remaining inputs are the parameters of the
distribution.
Thus dhyper $(\mathrm{k}, \mathrm{w}, \mathrm{b}, \mathrm{n})$ returns $P(X=k)$ where $X \sim \operatorname{HGeom}(w, b, n)$, and
phyper $(k, w, b, n)$ returns $P(X \leq k)$. For rhyper, the first input is the num-
phyper $(k, w, b, n)$ returns $P(X \leq k)$. For rnyper, the first input is the num- ber of r.v.s we want to generate, and the remaining inputs are the parameters;
rhyper $(100, \mathrm{w}, \mathrm{b}, \mathrm{n})$ generates 100 i.i.d. HGeom $(w, b, n)$ r.v.s.
Discrete distributions with finite support
We can generate r.v.s from any discrete distribution with finite support using the
sample command. When we first introduced the sample command, we said that it
can be used in the form sample $(\mathrm{n}, \mathrm{k})$ or sample $(\mathrm{n}, \mathrm{k}$, replace=TRUE) to sample $k$
can be used in the form sample $(\mathrm{n}, \mathrm{k})$ or sample $(\mathrm{n}, \mathrm{k}$, replace=rnus) to sample $k$.
ple, to generate 5 independent DUnif $(1,2, \ldots, 100)$ r.v.s, we can use the command
ple, to generate 5 independent sample $(100,5$, replace=TRUE)
It turns out that sample is far more versatile. If we want to sample from the values
$x_{1}, \ldots, x_{n}$ with probabilities $p_{1}, \ldots, p_{n}$, we simply create a vector $x$ containing all
the $x_{i}$ and a vector p containing all the $p_{i}$, then feed them into sample. Suppose we
want realizations of i.i.d. r.v.s $X_{1}, \ldots, X_{100}$ whose PMF is
$P\left(X_{j}=0\right)=0.25$.
$P\left(X_{j}=0\right)=0.25$, $P\left(X_{j}=1\right)=0.5$,
$P\left(X_{j}=5\right)=0.1$ $P\left(X_{j}=10\right)=0.15$

## 统计代考

$\begin{array}{ll}3.11 & \mathrm{R}\end{array}$
$\mathrm{R}$ 中的分布

help(distributions) 提供了一个方便的内置列表

dbinom、pbinom 和 rbinom。对于伯努利分布，我们可以使用
$n=1$ 的二项式函数。

• dbinom 是二项式 PMF。它需要三个输入：第一个是 $x$ 的值
在哪里评估 PMF，第二个和第三个是参数 $n$
和 p。例如，dbinom $(3,5,0.2)$ 返回概率 $P(X=3)$ 其中
$X \sim \operatorname{Bin}(5,0.2) .$ 换句话说
dbinom $(3,5,0.2)=\left(\begin{array}{l}5 \ 3\end{array}\right)(0.2)^{3}(0.8)^{2}=0.0512 。$
二项式 CDF。它需要三个输入：第一个是 $x$ 的值
luate CDF，第二个和第三个是参数。
• pbinom 是二项式 CDF。它需要三个输入：第一个是 $x$ 的值
评估 CDF 的位置，第二个和第三个是参数。
pbinom $(3,5,0.2)$ 是概率 $P(X \leq 3)$ 其中 $X \sim \operatorname{Bin}(5,0.2)$。所以
pbinom $(3,5,0.2)=\sum_{k=0}^{3}\left(\begin{array}{l}5 \ k\end{array}\right)(0.2)^{k }(0.8)^{5-k}=0.9933$
• rbinom 是用于生成二项式随机变量的函数。对于 rbinon，
第一个输入是我们想要生成多少个 r.v.s，第二个和第三个
输入仍然是参数。因此命令 rbinom $(7,5,0.2)$ 产生
七个 i.i.d. 的实现Bin(5,0.2) r.v.s.当我们运行这个命令时，我们得到了
$\begin{array}{lllllll}2 & 1 & 0 & 0 & 1 & 0 & 0\end{array}$
但是当你尝试它时，你可能会得到不同的东西！
随机变量及其分布
我们还可以在整个值向量上评估 PMF 和 CDF。对于考试——
请记住，$0: \mathrm{n}$ 是列出从 0 到 $n$ 的整数的一种快速方法。命令
dbinom $(0: 5,5,0.2)$ 返回 6 个数字，$P(X=0), P(X=1), \ldots, P(X=5)$，其中
$X \sim \operatorname{Bin}(5,0.2)$
超几何分布
超几何分布也具有三个函数：dhyper、phyper 和
押韵。正如人们所预料的那样，dhyper 是超几何 PMF，phyper 是
押韵。正如人们所预料的那样，dhyper 是超几何 PNF，phyper 是超几何 CDF，而 rhyper 生成超几何 r.v.s。由于海
超几何 CDF 和 rhyper 生成超几何 r.v.s.由于超几何分布具有三个参数，因此这些函数中的每一个都需要四个
输入。对于 dhyper 和 phyper，第一个输入是我们希望达到的值
评估 PMF 或 CDF，其余输入是
分配。
因此 dhyper $(\mathrm{k}, \mathrm{w}, \mathrm{b}, \mathrm{n})$ 返回 $P(X=k)$ 其中 $X \sim \operatorname{HGeom}(w , b, n)$, 和
phyper $(k, w, b, n)$ 返回 $P(X \leq k)$。对于 rhyper，第一个输入是数字
phyper $(k, w, b, n)$ 返回 $P(X \leq k)$。对于 rnyper，第一个输入是我们想要生成的 r.v.s 的数量，其余的输入是参数；
rhyper $(100, \mathrm{w}, \mathrm{b}, \mathrm{n})$ 生成 100 i.i.d. HGeom $(w, b, n)$ r.v.s.
具有有限支持的离散分布
我们可以使用有限支持从任何离散分布生成 r.v.s
示例命令。当我们第一次介绍示例命令时，我们说它
可以使用 sample $(\mathrm{n}, \mathrm{k})$ 或 sample $(\mathrm{n}, \mathrm{k}$, replace=TRUE) 形式来采样 $k$
可以使用 sample $(\mathrm{n}, \mathrm{k})$ 或 sample $(\mathrm{n}, \mathrm{k}$, replace=rnus) 的形式来采样 $k$。
ple，要生成5个独立的DUnif $(1,2, \ldots, 100)$ r.v.s，我们可以使用命令
ple, 生成 5 个独立样本 $(100,5$, replace=TRUE)
事实证明，样本的用途要广泛得多。如果我们想从值中采样
$x_{1}, \ldots, x_{n}$ 概率为 $p_{1}, \ldots, p_{n}$，我们只需创建一个向量 $x$ 包含所有
$x_{i}$ 和一个包含所有 $p_{i}$ 的向量 p，然后将它们输入到样本中。假设我们
想要实现 i.i.d. r.v.s $X_{1}, \ldots, X_{100}$ 其 PMF 为
$P\left(X_{j}=0\right)=0.25$。
$P\left(X_{j}=0\right)=0.25$, $P\left(X_{j}=1\right)=0.5$,
$P\left(X_{j}=5\right)=0.1$ $P\left(X_{j}=10\right)=0.15$