我正在尝试从正态分布生成100个随机数据,创建它的直方图并将密度函数放在直方图上。
到目前为止,我已经创建了
set.seed(123)
rs <- rnorm(100, mean = weighted.mean(femals$Salary), sd = sd(femals$Salary))
h <- hist(rs, col = "lightgray" , density = 50 )
xfit <- seq(min(femals$Salary), max(femals$Salary), length = 40)
yfit <- dnorm(xfit, mean = mean(femals$Salary), sd = sd(femals$Salary))
yfit <- yfit * diff(h$mids[1:2]) * length(femals$Salary)
lines(xfit, yfit, col = "red", lwd = 2)
结果是
但我不确定这是否正确。该直方图的密度函数是否为低?不应该密度遵循直方图的边缘?这是正确的还是在我的代码中犯了错误?
平均和标准偏差是:
weighted mean(femals$Salary) = 5138.852
sed(femals$Salary) = 539.8707