拟合密度曲线R中的直方图
-
19-09-2019 - |
题
是否有R中的功能适合的曲线以直方图?
让我们假设你有以下直方图
hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
它看起来正常,但它的歪斜。我想适合正常曲线偏斜环绕该直方图。
这个问题是相当基本的,但我似乎无法找到在互联网上R上的答案。
解决方案
如果我正确地理解你的问题,那么你可能想用柱状图沿着密度估计:
X <- c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))
hist(X, prob=TRUE) # prob=TRUE for probabilities not counts
lines(density(X)) # add a density estimate with defaults
lines(density(X, adjust=2), lty="dotted") # add another "smoother" density
编辑长而后来:
下面是一个稍微穿着后续版本:
X <- c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))
hist(X, prob=TRUE, col="grey")# prob=TRUE for probabilities not counts
lines(density(X), col="blue", lwd=2) # add a density estimate with defaults
lines(density(X, adjust=2), lty="dotted", col="darkgreen", lwd=2)
用它产生的图表沿着:
其他提示
这样的东西很容易与GGPLOT2
library(ggplot2)
dataset <- data.frame(X = c(rep(65, times=5), rep(25, times=5),
rep(35, times=10), rep(45, times=4)))
ggplot(dataset, aes(x = X)) +
geom_histogram(aes(y = ..density..)) +
geom_density()
或从德克的溶液模拟结果
ggplot(dataset, aes(x = X)) +
geom_histogram(aes(y = ..density..), binwidth = 5) +
geom_density()
下面是我做的方式:
foo <- rnorm(100, mean=1, sd=2)
hist(foo, prob=TRUE)
curve(dnorm(x, mean=mean(foo), sd=sd(foo)), add=TRUE)
一个奖金锻炼与GGPLOT2包来做到这一点...
德克解释如何绘制在直方图中的密度函数。但有时你可能会想要去一个偏正态分布的假设强和情节,与其密度。可以估算分布的参数和使用 SN包绘制它一个>:
> sn.mle(y=c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
$call
sn.mle(y = c(rep(65, times = 5), rep(25, times = 5), rep(35,
times = 10), rep(45, times = 4)))
$cp
mean s.d. skewness
41.46228 12.47892 0.99527
此可能适用于数据更好是更歪斜正常:
我有同样的问题,但德克的解决方案似乎并没有工作。 每次我得到这样的警告messege
"prob" is not a graphical parameter
我通过?hist
阅读并发现约freq: a logical vector set TRUE by default.
这为我工作的代码是
hist(x,freq=FALSE)
lines(density(x),na.rm=TRUE)
不隶属于 StackOverflow