stat_function 中的 dlnorm 不正确

2024-04-17

我试图通过叠加一个函数stat_function() in ggplot2如此处所述:在 ggplot 和 stat_function() 中叠加对数正态密度 https://stackoverflow.com/questions/12464396/superimposing-a-log-normal-density-in-ggplot-and-stat-function所以使用命令:

ggplot(data=data, aes(x=x)) +
  geom_histogram(aes(y = ..density..)) +
  stat_function(fun = dlnorm, size=1, color='gray') +
  theme_bw()

它适用于提供的示例,其中适合的数据是通过生成的rf。但是,如果我尝试将其应用到下面的数据集,它不适合。我的数据集有什么问题stat_function不能够适应吗?他们在我想做的事情中犯了一些数学错误吗?我的 data.frame 数字类型有问题吗?

以下是我用各自的数据集得到的两个结果:

不适合:

data <- data.frame(x=c(83.92527, 75.72644, 76.44609, 100.86324, 87.44626, 78.37094, 77.71285, 94.66197, 69.76701, 83.93192, 68.26451, 71.49349, 66.51735, 76.72893, 76.76861, 81.38741, 67.9929, 74.44888, 86.06689, 76.9507, 123.47084, 90.56689, 81.50586, 74.04925, 71.85926, 91.60573, 74.57221, 68.53912, 75.34062, 80.65242, 85.15228, 104.06124, 72.42447, 75.27314, 73.01164, 84.94915, 80.04429, 86.93343, 82.04338, 77.70276, 84.0946, 84.35794, 96.01299, 72.26497, 115.12634, 74.87349, 80.4077, 77.33795, 73.4267, 68.03937, 82.50726, 78.13893, 68.7824, 85.83253, 80.94278, 78.06742, 75.68488, 133.39636, 92.89265, 80.01308, 187.60977, 86.73605, 76.10981, 71.80097, 78.31453, 75.60157, 86.07133, 76.92616, 71.48474, 133.32378, 78.6234, 131.75722, 82.31215, 74.46081, 73.87192, 82.53808, 74.79978, 68.17945, 112.14891, 89.37358, 79.76679, 75.2691, 86.79122, 79.46324, 86.15034, 74.70525, 71.61041, 82.48748, 77.10785, 73.95811, 76.25556, 82.17103, 75.97427, 80.19654, 88.01052, 75.10031, 85.93202, 78.12773, 72.52136, 93.67812))

Fits:

data <- data.frame(x = rf(100, df1 = 7, df2 = 120))

默认参数值mean and sd of dlnorm是 0 和 1。你必须估计参数actual数据集。这可以通过函数来​​完成fitdistr in the MASS包裹。

library(MASS)
fit <- fitdistr(data$x, "lognormal")

现在,您可以使用估计值dlnorm功能:

ggplot(data=data, aes(x=x)) +
      geom_histogram(aes(y = ..density..)) +
      stat_function(fun = dlnorm, size = 1, color = 'gray',
                    args = list(mean = fit$estimate[1], sd = fit$estimate[2])) +
      theme_bw() 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

stat_function 中的 dlnorm 不正确 的相关文章

随机推荐