本文介绍了如何获得估计密度的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何将密度(数据集)的结果保存为函数? 那么如果我想在该函数中评估点x,它给了我从这个密度(数据集)的概率? 解决方案正如您在下面看到的, density 函数会返回一个包含 x 和 y 密度函数值,可用于使用 approxfun 函数创建插值函数。 d 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 pdf(2) ## [1] 0.05439069 approxfun 给出线性近似值 验证让我们绘出原始密度 d plot(d) 现在让我们用新函数 pdf绘制内插值。 code>我们创建了 x 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.05439069approxfun gives linear approximationTo verify lets plot the original density dplot(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)) 这篇关于如何获得估计密度的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 15:43