Open In App

Getting Kernel Density Estimates in R Programming - density() Function

Last Updated : 23 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
density() function in R Language is used to compute kernel density estimates.
Syntax: density(x) Parameters: x: number vector
Example 1: Python3
# 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
Output:
 [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
Example 2: Python3
# 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
Output:
 [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  

Next Article
Article Tags :

Similar Reads