将正态分布拟合到分组数据,给出预期频率
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")
这个问题有很多变体,其中大部分涉及在直方图上绘制正态密度,缩放以表示计数而不是密度。但是 none 明确显示了预期频率的计算。一个封闭的问题是
我可以很好地完成标准图(如下),但对于其他事情,如卡方检验或 vcd::rootogram
图,我需要相同 class 中的预期频率间隔。
> 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
我有一个观察频率分布,在 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")
这个问题有很多变体,其中大部分涉及在直方图上绘制正态密度,缩放以表示计数而不是密度。但是 none 明确显示了预期频率的计算。一个封闭的问题是
我可以很好地完成标准图(如下),但对于其他事情,如卡方检验或 vcd::rootogram
图,我需要相同 class 中的预期频率间隔。
> 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