R 将高斯拟合到ggplot2中的数据几何点

R 将高斯拟合到ggplot2中的数据几何点,r,ggplot2,R,Ggplot2,我有以下数据集 structure(list(Collimator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("n", "y"), class = "factor"), angle = c(0L, 15L, 30L, 45L, 60L, 75L, 90L, 105L, 120

我有以下数据集

structure(list(Collimator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("n", "y"), class = "factor"), angle = c(0L, 
15L, 30L, 45L, 60L, 75L, 90L, 105L, 120L, 135L, 150L, 165L, 180L, 
0L, 15L, 30L, 45L, 60L, 75L, 90L, 105L, 120L, 135L, 150L, 165L, 
180L), X1 = c(2099L, 11070L, 17273L, 21374L, 23555L, 23952L, 
23811L, 21908L, 19747L, 17561L, 12668L, 6008L, 362L, 53L, 21L, 
36L, 1418L, 6506L, 10922L, 12239L, 8727L, 4424L, 314L, 38L, 21L, 
50L), X2 = c(2126L, 10934L, 17361L, 21301L, 23101L, 23968L, 23923L, 
21940L, 19777L, 17458L, 12881L, 6051L, 323L, 40L, 34L, 46L, 1352L, 
6569L, 10880L, 12534L, 8956L, 4418L, 344L, 58L, 24L, 68L), X3 = c(2074L, 
11109L, 17377L, 21399L, 23159L, 23861L, 23739L, 21910L, 20088L, 
17445L, 12733L, 6046L, 317L, 45L, 26L, 46L, 1432L, 6495L, 10862L, 
12300L, 8720L, 4343L, 343L, 38L, 34L, 60L), average = c(2099.6666666667, 
11037.6666666667, 17337, 21358, 23271.6666666667, 23927, 23824.3333333333, 
21919.3333333333, 19870.6666666667, 17488, 12760.6666666667, 
6035, 334, 46, 27, 42.6666666667, 1400.6666666667, 6523.3333333333, 
10888, 12357.6666666667, 8801, 4395, 333.6666666667, 44.6666666667, 
26.3333333333, 59.3333333333)), .Names = c("Collimator", "angle", 
"X1", "X2", "X3", "average"), row.names = c(NA, -26L), class = "data.frame")
I首先缩放准直器y和n的平均计数,使最高计数为1

df <- ddply(df, .(Collimator), transform,
            norm.average = average / max(average))

使用geom_线在眼睛上是非常不舒服的,我宁愿使用stat_smooth来拟合数据。每个数据集的平均值应该是对称的,所以我认为高斯拟合应该是理想的。如何将高斯拟合到ggplot2中的数据集准直器=“y”和准直器=“n”或使用基数R。我还想输出平均值和标准偏差。这可以做到吗?

根据定义,您的数据不是高斯的,而是一种类似高斯的形状,下面是拟合可视化的示例:

fit <- dlply(df, .(Collimator), function(x) {
    co <- coef(nls(norm.average ~ exp(-(angle - m)^2/(2 * s^2)), data = x, start = list(s = 50, m = 80)))
    stat_function(fun = function(x) exp(-(x - co["m"])^2/(2 * co["s"]^2)), data = x)
})

ggplot(df, aes(x = angle, y = norm.average, col = Collimator)) + geom_point() + fit

杰出的我能从拟合中提取平均值和标准偏差吗?
fit <- dlply(df, .(Collimator), function(x) {
    co <- coef(nls(norm.average ~ exp(-(angle - m)^2/(2 * s^2)), data = x, start = list(s = 50, m = 80)))
    stat_function(fun = function(x) exp(-(x - co["m"])^2/(2 * co["s"]^2)), data = x)
})

ggplot(df, aes(x = angle, y = norm.average, col = Collimator)) + geom_point() + fit
fit <- dlply(df, .(Collimator), function(x) {
    co <- coef(nls(norm.average ~ exp(-(angle - m)^2/(2 * s^2)), data = x, start = list(s = 50, m = 80)))
    r <- stat_function(fun = function(x) exp(-(x - co["m"])^2/(2 * co["s"]^2)), data = x)
    attr(r, ".coef") <- co
    r
})
> ldply(fit, attr, ".co")
  Collimator        s        m
1          n 52.99117 82.60820
2          y 21.99518 86.61268