如何获得估计密度的函数? [英] How to gain a function of an estimated density?
问题描述
如何将密度(数据集)的结果保存为函数?
那么如果我想在该函数中评估点x,它给了我从这个密度(数据集)的概率?
正如您在下面看到的, density
函数会返回一个包含 x
和 y
密度函数值,可用于使用 approxfun
函数创建插值函数。
d < - density(rnorm(100))
str(d)
##列表7
## $ x:num [1:512] -3.85 -3.83 -3.82 -3.8 -3.79 ...
## $ y:num [1:512] 0.000135 0.000154 0.000176 0.0002 0.000227 ...
## $ bw:num 0.332
## $ n:int 100
## $ call:language density.default(x = rnorm(100))
## $ data.name:chrrnorm(100)
## $ has.na:logi FALSE
## - attr(*,class)= chrdensity
pdf < - approxfun(d)
pdf(2)
## [1] 0.05439069
approxfun
给出线性近似值
验证让我们绘出原始密度 d
plot(d)
现在让我们用新函数 pdf绘制内插值。
x < - seq(-2,2,by = 0.01)
points(x,pdf(x))
How do I save the result from density(dataset) as a function? So then if I want to evaluate point x in that function, it gives me the probability from that density(dataset)?
As you can see below, density
function retuns a list containing x
and y
values of density function which can be used to create a "interpolation" function using approxfun
function.
d <- density(rnorm(100))
str(d)
## List of 7
## $ x : num [1:512] -3.85 -3.83 -3.82 -3.8 -3.79 ...
## $ y : num [1:512] 0.000135 0.000154 0.000176 0.0002 0.000227 ...
## $ bw : num 0.332
## $ n : int 100
## $ call : language density.default(x = rnorm(100))
## $ data.name: chr "rnorm(100)"
## $ has.na : logi FALSE
## - attr(*, "class")= chr "density"
pdf <- approxfun(d)
pdf(2)
## [1] 0.05439069
approxfun
gives linear approximation
To verify lets plot the original density d
plot(d)
Now lets plot the interpolated values using the new function pdf
that we created
x <- seq(-2,2,by=0.01)
points(x, pdf(x))
这篇关于如何获得估计密度的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!