Introduction to Statistics in R:02-Random Numbers and Probability

Random Numbers and Probability

What are the chances?

Measuring chance

What's the probability of an event?

Example: a coin flip

Assigning salespeople

Sampling from a data frame

sales_counts

sales_count %>%
    sample_n(1)

sales_count %>%
    sample_n(1)

Setting a random seed

set.seed(5)
sales_counts %>%
    sample_n(1)
 
set.seed(5)
sales_couonts %>%
    sample_n(1)

Sampling with replacement in R

sales_counts %>%
    sample_n(2, replace=TRUE)

sample(sales_team, 5, replace=TRUE)

Independent events

Two events are independent if the probability of the second event isn't affected by the outcome of the first event.

Sampling with replacement = each pick is independent

Dependent events

Two events are dependent if the probability of the sencond event is affected by the outcome of the first event.

Sampling without replacement = each pick is dependent

Discrete distributions

Rolling the dice

Choosing salespeople

Probability distribution

Describes the probability of each possible outcome in a scenario

Visualizing a probability distribution

Probability = area

Uneven die

Visualizing uneven probabilities

Adding areas

Discrete probability distributions

Discribe probabilities for discrete outcomes

Sampling from discrete distributions

die
mean(die$n)
rolls_10 <- die %>%
	sample_n(10, replace = TRUE)
rolls_10

Visualizing a sample

ggplot(rolls_10, aes(n)) +
	geom_histogram(bins = 6)

Sample distribution vs. theoretical distribution

A bigger sample

Law of large numbers

As the size of your sample increases, the sample mean will approach the expected value

Continuous distribution

Waiting for the bus

Continuous uniform distribution

Probability still = area

Uniform distribution in R

punif(7, min = 0, max = 12)
# 0.5833333

lower.tail

punif(7, min = 0, max = 12, lower.tail = FALSE)
# 0.4166667
 
punif(7, min = 0, max = 12) - punif(4, min = 0, max = 12)

Total area = 1

Other continuous distributions

Other special types of distributions

The binomial distribution

Coin fipping

Binary outcomes

A single flip

rbinom(# of trials, # of coins, # probability of heads/success)

1 = head, 0 = tails

rbinom(1, 1, 0.5)
# 1
rbinom(1, 1, 0.5)
# 0

One flip many times

rbinom(8, 1, 0.5)
# 1 0 0 1 0 0 1 0

Many flips one time

rbinom(1, 8, 0.5)
# 3

Many flips many times

rbinom(10, 3, 0.5)
# 2 0 1 0 1 1 3 3 3 1

Other probabilities

rbinom(10, 3, 0.25)
# 1 1 0 0 1 1 1 1 2 1

Binomial distribution

Probability distribution of the number of successes in a sequence of independent trials

E.g. Number of heads in a sequence of coin flips

Describe by n and p

  • n: total number of trials

  • p: probability of success

What's the probability of 7 heads?

P(heads = 7)

#dbinom(num heads, num trials, prob of heads)
dbinom(7, 10, 0.5)
# 0.1171875

What's the probability of 7 or fewer heads?

P(heads <= 7)

pbinom(7, 10, 0.5)
#0.9453125

What's the probability of more than 7 heads?

P(heads > 7)

pbinom(7, 10, 0.5, lower.tail = FALSE)
# 0.0546875
1 - pbinom(7, 10, 0.5)
# 0.0546875

Expected value

Expected value = n x p

Expected number of heads out of 10 flips = 10 x 0.5 = 5

Independence

The binomial distribution is a probability distribution of the number of successes in a sequence of independent trials

Probabilities of second trial are altered due to outcome of the first

If trials are not independent, the binomial distribution does not apply!

《概率与统计导论:面向工程师和科学家》是一本旨在帮助工程师和科学家理解概率与统计理论和实践的教科书。该教材涵盖了广泛的主题,旨在教授读者如何运用概率与统计的知识进行数据分析和做出合理的决策。 该教材首先介绍了概率论的基本概念和原理,如概率、事件、随机变量和概率分布。通过这些基本概念的讲解,读者将能够理解和应用概率论的基本原理,从而能够解决各种实际问题。 接着,该教材介绍了统计学的基本概念和方法。它包括描述性统计和推断统计的内容。描述性统计帮助读者了解如何对收集到的数据进行汇总和可视化表示,以便更好地理解数据的特征和规律。推断统计探讨了如何从样本数据中推断总体参数,并使用假设检验和置信区间等方法对统计结论进行验证和推断。 除了基本概念和方法,该教材还介绍了一些实际应用,如可靠性分析、实验设计和回归分析等。这些应用案例帮助读者将概率和统计理论与实际问题相结合,为他们在工程和科学领域中做出合理的决策提供指导和支持。 此外,该教材提供了许多练习题和案例研究,以帮助读者巩固自己的概率和统计知识,并培养他们的问题解决能力。这些练习和案例有助于读者将所学的理论知识应用到实际问题中,并培养他们的分析和推理能力。 总之,该教材是一本全面介绍概率与统计理论和实践的教科书,旨在培养工程师和科学家在数据分析和决策过程中的能力和技巧。通过深入的理论讲解、实际应用案例和丰富的练习,读者将能够全面理解和应用概率与统计的知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值