当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 density()用法及代码示例


R 语言中的 density() 函数用于计算核密度估计。

用法: density(x)

参数:
x:数字向量

范例1:


# R program to illustrate
# density function
  
# Generating 10 numbers randomly
x <- stats::rnorm(10)
  
# Getting 10 random number
x
  
# Calling density() function
d <- density(x)
  
# Getting kernel density estimates
d

输出:

 [1] -0.89243190 -0.76257379  1.17066825  0.09418077  1.08321504 -0.41264708
 [7]  0.39009942  0.69991113 -0.82339069 -0.68621705

Call:
    density.default(x = x)

Data:x (10 obs.);    Bandwidth 'bw' = 0.4594

       x                 y           
 Min.  :-2.2706   Min.  :0.001546  
 1st Qu.:-1.0657   1st Qu.:0.047506  
 Median:0.1391   Median:0.243764  
 Mean  :0.1391   Mean  :0.207189  
 3rd Qu.:1.3440   3rd Qu.:0.312477  
 Max.  :2.5488   Max.  :0.434704

范例2:


# R program to illustrate
# density function
  
# Generating number 1 to 10 
x <- seq(1, 10) 
x
  
# Calling density() function
d <- density(x)
  
# Getting kernel density estimates
d

输出:

 [1]  1  2  3  4  5  6  7  8  9 10

Call:
    density.default(x = x)

Data:x (10 obs.);    Bandwidth 'bw' = 1.719

       x                 y            
 Min.  :-4.1579   Min.  :0.0003034  
 1st Qu.:0.6711   1st Qu.:0.0092711  
 Median:5.5000   Median:0.0538486  
 Mean  :5.5000   Mean  :0.0517052  
 3rd Qu.:10.3289   3rd Qu.:0.0936045  
 Max.  :15.1579   Max.  :0.0997741  

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Getting Kernel Density Estimates in R Programming – density() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。