如何在R中创建分布函数? [英] How to create a distribution function in R?
本文介绍了如何在R中创建分布函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下函数:
f(x)=(1/2 * pi)(1 /(1+ x ^ 2/4))
我如何确定其分布并在R中编写此分布函数?
解决方案
所以这是您的函数(希望您知道如何编写R函数;如果没有,请检查
Given the following function:
f(x) = (1/2*pi)(1/(1+x^2/4))
How do I identify it's distribution and write this distribution function in R?
解决方案
So this is your function right now (hopefully you know how to write an R function; if not, check writing your own function):
f <- function (x) (pi / 2) * (1 / (1 + 0.25 * x ^ 2))
f
is defined on (-Inf, Inf)
so integration on this range gives an indefinite integral. Fortunately, it approaches to Inf
at the speed of x ^ (-2)
, so the integral is well defined, and can be computed:
C <- integrate(f, -Inf, Inf)
# 9.869604 with absolute error < 1e-09
C <- C$value ## extract integral value
# [1] 9.869604
Then you want to normalize f
, as we know that a probability density should integrate to 1:
f <- function (x) (pi / 2) * (1 / (1 + 0.25 * x ^ 2)) / C
You can draw its density by:
curve(f, from = -10, to = 10)
这篇关于如何在R中创建分布函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文