Is there a python equivalent function of Kde2d function in R, which returns us the same outputs in the form x, y and z?

Viewed 132

I am getting different outputs from both R and Python on using these two equivalent functions on the same sample data. I am also computing the bandwidth separately in python to make sure its same as that of R. Can someone please help..

Running R in python to compare outputs:



import numpy as np
from scipy.stats import gaussian_kde
from rpy2.robjects import r
from rpy2.robjects.packages import importr
import pandas as pd
import math

def R2DKDE():
    importr('MASS')
    r.assign('nelems', nelems)
    r.assign('maxX', 1)
    r.assign('maxY', 1)
    #r.assign('nruns', nruns)

    r("""
    
      #dataX <- runif(nelems, 0, 1)
      #print(dataX)
      dataX = c(0.20,0.40,0.45,0.50,0.50)
      dataY = c(0.90,0.60,0.89,0.70,0.88)
      #dataY <- runif(nelems, 0, 1)
      kde2dmap <- kde2d(dataX, dataY, n=5, lims=c(0, 1, 0, 1))
      #print(kde2dmap$z[1,])
      #print(sum(kde2dmap$z[1,]))
    """)
    
    vals = np.array(list(r('kde2dmap$z')))
    #print(sum(vals))
    return r('kde2dmap')

#nelems = 5
print("Running R")
vals = R2DKDE()
print(vals)


This one is python and the function(p) is for calculation bandwidth:



def function(p):
        xx = p.values.flatten()
        #r = quantile(x, [0.25, 0.75])
        r1 = np.quantile(xx,0.25)
        r2 = np.quantile(xx,0.75)
        r=[r1,r2]
         
        h = (r[1] - r[0])/1.34
        return(4 * 1.06 * min(math.sqrt(np.var(xx)), h) * len(xx)**(-1/5))


def py2DKDE( ):
    xmin, ymin, xmax, ymax = 0., 0., 1., 1.
    xx = np.array([0.20,0.40,0.45,0.50,0.50])
    yy = np.array([0.90,0.60,0.89,0.70,0.88])
    
    positions = np.vstack([xx, yy])
    
    c = None
    
    c = pd.DataFrame(xx,yy)
    c = c.reset_index()
    bandwidth = function(c)
    kde = gaussian_kde(c.T, bw_method=bandwidth)
    vals = kde.evaluate(positions)
    #print("sum",sum(vals))
    return vals




print("Running Python")
vals = py2DKDE()
print(vals)

Output:

Running R
$x
[1] 0.00 0.25 0.50 0.75 1.00

$y
[1] 0.00 0.25 0.50 0.75 1.00

$z
             [,1]         [,2]         [,3]         [,4]         [,5]
[1,] 1.173263e-17 5.254988e-11 8.200866e-06 4.329905e-03 7.655782e-03
[2,] 1.287456e-08 6.493505e-04 1.121958e-01 1.366880e+00 2.304749e+00
[3,] 8.724703e-08 4.865638e-03 1.600830e+00 9.082211e+00 4.925686e+00
[4,] 8.138891e-14 3.871074e-08 6.419998e-05 5.365424e-04 2.143860e-04
[5,] 3.213046e-26 1.583899e-20 2.636431e-17 2.194545e-16 8.669376e-17


Running Python

[2.45999350e-11 3.46916474e-02 1.38037056e-06 3.04028839e-02
 6.69103632e-06]
0 Answers
Related