将密度曲线拟合到 R 中的直方图 [英] Fitting a density curve to a histogram in R

查看:27
本文介绍了将密度曲线拟合到 R 中的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 中是否有将曲线拟合到直方图的函数?

Is there a function in R that fits a curve to a histogram?

假设您有以下直方图

hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))

看起来很正常,但它歪了.我想拟合一条扭曲的正态曲线来环绕这个直方图.

It looks normal, but it's skewed. I want to fit a normal curve that is skewed to wrap around this histogram.

这个问题很基础,但我似乎在互联网上找不到 R 的答案.

This question is rather basic, but I can't seem to find the answer for R on the internet.

推荐答案

如果我正确理解您的问题,那么您可能需要密度估计以及直方图:

If I understand your question correctly, then you probably want a density estimate along with the histogram:

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

稍后再

这是一个稍微打扮的版本:

Here is a slightly more dressed-up version:

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) 

连同它产生的图表:

这篇关于将密度曲线拟合到 R 中的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆