-1

I was trying to carry out a simulation based on the kernel distribution on R. Dataset used - CYG OB1 on the HSAUR2 package. Using the dataset I carried out an analysis to find the Kernel Density Estimate. I was looking to use this Kernel Density to simulate the bivariate values. Code used till now

> CYGOB1d <- bkde2D(CYGOB1, bandwidth = sapply(CYGOB1, dpik))
> plot(CYGOB1, xlab = "log surface temperature", ylab = "log light intensity")
> contour(x = CYGOB1d$x1, y=CYGOB1d$x2, z=CYGOB1d$fhat, add = TRUE)
> persp(x=CYGOB1d$x1, y=CYGOB1d$x2, z = CYGOB1d$fhat, xlab = "log surface 
  temperature", ylab = "log light intensity", zlab ="density")

How can I carry out a Simulation(1000 runs) based on the Kernel density?

Sunichie
  • 25
  • 4

2 Answers2

1

If z is your random variable, you can use

d <- density(z,n=512) 
#n is the number of points used for the curve, should be a power of two (512 is default) 

sample(x = d$x,prob = d$y,size=1000,replace=TRUE) 
#samples from the n values of x, according to prob y (the density)

See ?density for various other options for how it does the calculations.

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32
0

Thanks for your answer. I tried using the above code and the solution I'm getting is as follows(attached as an image):

simulation results

While the data is a bivariate data as follows:

      logst logli
[1,]   4.37  5.23
[2,]   4.56  5.74
[3,]   4.26  4.93
[4,]   4.56  5.74
[5,]   4.30  5.19
[6,]   4.46  5.46
[7,]   3.84  4.65
[8,]   4.57  5.27
[9,]   4.26  5.57
[10,]  4.37  5.12

I was hoping that the simulation results would give me pair of similar looking numbers.

Sunichie
  • 25
  • 4