stat_function中的dlnorm不合适

时间:2014-07-23 09:39:47

标签: r ggplot2 distribution

我正尝试在stat_function()中通过ggplot2添加一个函数,如下所述: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数字类型有问题吗?

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

不合适:

enter image description here

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))

适合:

enter image description here

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

1 个答案:

答案 0 :(得分:4)

mean的{​​{1}}和sd的默认参数值为0和1.您必须估算实际数据集的参数。这可以使用dlnorm包中的函数fitdistr来完成。

MASS

现在,您可以使用library(MASS) fit <- fitdistr(data$x, "lognormal") 函数的估算值:

dlnorm

enter image description here