R Generating Random Numbers and Random Sampling
感性复习:如何理解钟形曲线Permalink
感性复习:分布来分布去Permalink
随机变量
- 离散型随机变量
- 连续型随机变量
对于连续型随机变量
新知识:分位数 Quantile [‘kwɒntaɪl]Permalink
对正态分布
- 若
,称 为 下侧 分位数(在 图中 左侧) - 若
,i.e ,称 为 上侧 分位数(在 图中 右侧)
对一个确定的
正态分布的一些区间特性Permalink
图片来源:Lecture 2: Descriptive Statistics and Exploratory Data Analysis
其实还有,根据 quartile 这一节 IQR 的定义,50% of the observations 是在 [Ave - 0.5*IQR, Ave + 0.5*IQR] 这个区间内的。
正态分布相关函数Permalink
- rnorm: generate random Normal variates with a given mean and standard deviation [ˌdi:viˈeɪʃn]
rnorm(n, mean = 0, sd = 1)
即生成 n 个符合正态分布的随机值- mean 指 均值,i.e. 期望值,即
中的 ;默认为 0 - standard deviation 指 标准差,即
中的 ;默认为 1 是 方差 variance [ˈvɛriəns]
- dnorm: evaluate the Normal probability density (with a given mean/SD) at a point (or vector of points)
dnorm(x, mean = 0, sd = 1, log = FALSE)
计算 在这个N(mean, sd^2)
分布上的概率密度,即- if
log = TRUE
, probabilities are given as
- pnorm: evaluate the cumulative distribution function for a Normal distribution
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
计算 q 在这个N(mean, sd^2)
分布上的分布函数值,即- cumulative distribution 是 累积分布函数,其实就是 F(x),不知为何我的教材只翻译成分布函数
- if
log.p = TRUE
, probabilities are given as - if
lower.tail = TRUE (default)
, probabilities are otherwise, .
- qnorm: gives the quantile
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
给出 在这个N(mean, sd^2)
分布上的分位数- 默认是给出下侧分位数
- if
lower.tail = FALSE
,给出上侧分位数 - 比如
qnorm(0.05) = -1.644854
,qnorm(0.05, lower.tail=F) = 1.644854
泊松分布相关函数Permalink
- rpois: generate random Poisson variates with a given rate
rpois(n, lambda)
生成 n 个满足P(lambda)
泊松分布的随机数
- 同理有 dpois、ppois、qpois
二项分布相关函数Permalink
- rbinom: generate random Binomial variates with a given number of trials and the probability of success on each trial
rbinom(n, size, prob)
生成 n 个满足B(size, prob)
二次分布的随机数
- 同理有 dbinom、pbinom、qbinom
r、d、p、q 一般规律Permalink
- r for random number generation
- d for density, i.e.
- p for cumulative distribution, i.e.
- q for quantile function, i.e.
set.seed(n)Permalink
Setting the random number seed with set.seed()
ensures reproducibility.
< set.seed(1)
< rnorm(5)
[1] -0.6264538 0.1836433 -0.8356286 1.5952808
[5] 0.3295078
< rnorm(5)
[1] -0.8204684 0.4874291 0.7383247 0.5757814
[5] -0.3053884
< set.seed(1)
< rnorm(5)
[1] -0.6264538 0.1836433 -0.8356286 1.5952808
[5] 0.3295078
Always
set the random number seed when conducting a simulation!
an example of simulating linear modelPermalink
假设
> set.seed(20)
> x <- rnorm(100)
> e <- rnorm(100, 0, 2)
> y <- 2x + e
> summary(y)
Min. 1st Qu. Median
-6.4080 -1.5400 0.6789 0.6893 2.9300 6.5050
> plot(x, y)
Random SamplingPermalink
The sample()
function draws randomly from a specified set of (scalar) objects allowing you to sample from arbitrary distributions.
> set.seed(1)
> sample(1:10, 4)
[1] 3 4 5 7
> sample(1:10, 4)
[1] 3 9 8 5
> sample(letters, 5)
[1] "q" "b" "e" "x" "p"
> sample(1:10) ## Generate Permutation(序列)
[1] 4 710 6 9 2 8 3 1 5
> sample(1:10)
[1] 2 3 4 1 9 5 10 8 6 7
> sample(1:10, replace = TRUE) ## Sample w/ replacement
[1] 2 9 7 8 2 8 5 9 7 8
Comments