視覺化單變量高斯混合模型
你已經把模型擬合到 fit_mix_example,並將參數提取到 comp_1、comp_2 和 comp_3(以及 proportions)。現在來把對應的叢集與密度長條圖一起畫出來。
為了方便這個目的,環境中已定義 fun_prop() 函式。它會像 dnorm 一樣提供高斯分配的密度值,但另外擴充為也能接受比例(proportions)。
本練習屬於課程
R 中的混合模型
練習說明
- 將密度長條圖與各叢集的機率密度一起繪製出來。記得資料框名為
mix_example。 - 使用
stat_function(),並將引數fun設為fun_prop,以繪出每個叢集的機率密度分佈。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
ggplot(___) + ___(aes(x = x, y = ..density..)) +
stat_function(geom = "line", fun = fun_prop,
args = list(mean = ___[1], sd = ___[2],
proportion = proportions[1])) +
stat_function(geom = "line", fun = fun_prop,
args = list(mean = comp_2[1], sd = comp_2[2],
proportion = ___[2]))+
stat_function(geom = "line", fun = ___,
args = list(mean = comp_3[1], sd = comp_3[2],
proportion = proportions[3]))