高斯分布

R 中的混合模型

Victor Medina

Researcher at The University of Edinburgh

性别数据集的混合模型

R 中的混合模型

拟合混合模型的包

  • mixtools
    • 未实现泊松分布。
  • bayesmix
    • 贝叶斯推断超出本课程范围。
  • EMCluster
    • 仅支持高斯分布。
  • flexmix
    • 覆盖所需分布,并可灵活构建更复杂模型。
R 中的混合模型

高斯分布的性质

均值

标准差

R 中的混合模型

从高斯分布采样

从高斯分布生成样本:

  • rnorm(n, mean, sd)

示例:从均值为 10、标准差为 5 的高斯分布生成 100 个值

 

> population_sample <- rnorm(n = 100, mean = 10, sd = 5)
> head(population_sample)
[1]  6.248874  9.564190 16.006521  9.139647 10.114969 16.423538
R 中的混合模型

均值的估计

  • 均值和标准差未知,只有观测值
    • 需从观测值中估计
  • 估计均值:计算样本均值

 

> mean_estimate <- mean(population_sample)
10.35759
R 中的混合模型

估计 sd 的步骤:

$$value_i\rightarrow (. -mean\_estimate)\rightarrow (.)^2\rightarrow mean (.)\rightarrow \sqrt{(.)}$$

> population_sample %>%  
+   subtract(mean_estimate) %>%
+   raise_to_power(2) %>% mean() %>% sqrt()
5.318641
  • 使用 sd 函数
> standard_deviation_estimate <- sd(population_sample)
> standard_deviation_estimate
5.345435
R 中的混合模型

可视化估计的高斯分布

# 将样本转换为数据框
population_sample <- data.frame(x = population_sample)

# 绘制直方图
ggplot(data = population_sample) + 
   geom_histogram(aes(x = x, y = ..density..)) +
   stat_function(geom = "line", 
                 fun = dnorm, 
                 args = list(mean = mean_estimate, 
                             sd = standard_deviation_estimate))
R 中的混合模型

R 中的混合模型

让我们来练习!

R 中的混合模型

Preparing Video For Download...