[英]fit a normal distribution to grouped data, giving expected frequencies
我有觀察的頻率分布,分為 class 間隔內的計數。 我想擬合正態(或其他連續)分布,並根據該分布在每個區間中找到預期頻率。
例如,假設以下,我想計算另一列, expected
給出胸部周長在chest
給出的區間內的預期士兵數量,其中假設這些以名義值為中心。 例如, 35
= 34.5 <= y < 35.5
。 我看到的一項分析給出了這個單元格中的預期頻率為 72.5 與觀察到的 81。
> data(ChestSizes, package="HistData")
>
> ChestSizes
chest count
1 33 3
2 34 18
3 35 81
4 36 185
5 37 420
6 38 749
7 39 1073
8 40 1079
9 41 934
10 42 658
11 43 370
12 44 92
13 45 50
14 46 21
15 47 4
16 48 1
>
> # ungroup to a vector of values
> chests <- vcdExtra::expand.dft(ChestSizes, freq="count")
這個問題有很多變體,其中大部分與在直方圖頂部繪制正常密度有關,按比例縮放以表示計數而不是密度。 但沒有一個明確顯示預期頻率的計算。 一個接近的問題是R:在 ggplot2 中為分組直方圖添加正態擬合
我可以很好地完成標准 plot(如下),但對於其他事情,如卡方檢驗或vcd::rootogram
plot,我需要在相同的 ZA2F2ED4F8EBC04CBB4C21A2DDC 間隔中的預期頻率。
> bw <- 1
n_obs <- nrow(chests)
xbar <- mean(chests$chest)
std <- sd(chests$chest)
plt <-
ggplot(chests, aes(chest)) +
geom_histogram(color="black", fill="lightblue", binwidth = bw) +
stat_function(fun = function(x)
dnorm(x, mean = xbar, sd = std) * bw * n_obs,
color = "darkred", size = 1)
plt
以下是假設正態性的情況下如何計算每個組的預期頻率。
xbar <- with(ChestSizes, weighted.mean(chest, count))
sdx <- with(ChestSizes, sd(rep(chest, count)))
transform(ChestSizes, Expected = diff(pnorm(c(32, chest) + .5, xbar, sdx)) * sum(count))
chest count Expected
1 33 3 4.7600583
2 34 18 20.8822328
3 35 81 72.5129162
4 36 185 199.3338028
5 37 420 433.8292832
6 38 749 747.5926687
7 39 1073 1020.1058521
8 40 1079 1102.2356155
9 41 934 943.0970605
10 42 658 638.9745241
11 43 370 342.7971793
12 44 92 145.6089948
13 45 50 48.9662992
14 46 21 13.0351612
15 47 4 2.7465640
16 48 1 0.4579888
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.