在R中绘制自定义PDF [英] Plotting Custom PDF in R

查看:68
本文介绍了在R中绘制自定义PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑对 $ \ mathbb {R} $ 定义的 $ \ phi $ 密度函数,span class ="math-container"> $ a \ in \ mathbb {R} $ 和 $ b \ in \ mathbb {R} _ + ^ \ star $ 这样 $ \ forall x \ in \ mathbb {R} $ $$\ phi(x; a,b)= \ frac {1} {\ sqrt {2 \ pi b ^ 2}} e ^ {-\ frac {1} {2} \ left(\ frac {xa} {b}\ right)^ 2}.$$ 现在考虑由下式给出的所谓的Bart Simpson概率密度函数 $ f $ \ begin {eqnarray}\ label {eq:bart:1d:1}f(x)= \ frac {1} {2} \ phi(x; 0,1)+ \ frac {1} {10} \ sum_ {j = 0} ^ 4 {\ phi(x;(j/2)-1,1/10)}.\ end {eqnarray}

Consider the density function $\phi$ defined on $\mathbb{R}$ for $a\in \mathbb{R}$ and $b \in \mathbb{R}_+^\star$ such that $\forall x \in \mathbb{R}$, $$ \phi(x; a,b) = \frac{1}{\sqrt{2\pi b^2}}e^{-\frac{1}{2}\left(\frac{x-a}{b}\right)^2}. $$ Consider now the so-called Bart Simpson probability density function $f$ given by \begin{eqnarray} \label{eq:bart:1d:1} f(x) = \frac{1}{2}\phi(x; 0,1) + \frac{1}{10}\sum_{j=0}^4{\phi(x; (j/2)-1, 1/10)}. \end{eqnarray}

  1. $ [-\ pi,\ pi] $ 中绘制pdf $ f $ .
  1. Plot the pdf $f$ in $[-\pi, \pi]$.

尝试:

所以,我理解了符号 $ \ phi(x; a,b)$ - $ a $ 是平均值,而 $ b $ 是密度函数 $ \ phi $ 的标准偏差.

Attempt:

So, I understand the notation $\phi(x; a,b)$ -- $a$ is the mean and $b$ is the standard deviation for the density function $\phi$.

我可以编写 R 代码来模拟 $ \ phi $ $ f(x)$ :

I can write R code to simulate $\phi$ and $f(x)$:

  calc_cdf <- function(a, b, x) {
    coef <- 1/sqrt(2*pi*b^2)
    expon <- exp(-0.5*((x-a)/b)^2)
    return(coef * expon)
  }
  calc_pdf <- function(x) {
    term1 <- 0.5 * calc_cdf(0, 1, x)
    sum2 <- 0
    for (j in 0:4) { sum2 = sum2 + calc_cdf(j/2 - 1, 0.1, x) }
    term2 <- 0.1 * sum2
    return(term1 + term2)
  }

现在这就是我遇到的问题:如何在地球上绘制PDF?有一些用于绘制定义的PDF的库,例如 EnvStats :: pdfPlot,但这不允许您定义自己的PDF并进行打印.

Now this is where I'm stuck: How on earth do I plot a PDF? There are libraries for plotting defined PDFs, such as EnvStats::pdfPlot, but that doesn't allow you to define your own PDF and plot it.

据我所知,没有库可以这样做.我也找不到对巴特·辛普森" PDF的任何引用.

So far as I can tell, there are no libraries for doing so. I can't find any reference to a "Bart Simpson" PDF either.

请,我们将不胜感激!

推荐答案

您不需要整个库即可绘制函数.它内置在基本R中.

You don't need a whole library to plot a function. It's built into base R.

请参见?plot.function ?curve ,包括其示例.

See ?plot.function and ?curve, including their examples.

例如

 plot(dnorm,-3,3)
 plot(function (x) dgamma(x,shape=2),0,6)

这篇关于在R中绘制自定义PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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