random - R: Generate data from a probability density distribution -


say have simple array, corresponding probability distribution.

library(stats)     data <- c(0,0.08,0.15,0.28,0.90) pdf_of_data <- density(data, from= 0, to=1, bw=0.1) 

is there way generate set of data using same distribution. operation probabilistic, need not match initial distribution anymore, generated it.

i did have success finding simple solution on own. thanks!

your best bet generate empirical cumulative density function, approximate inverse, , transform input.

the compound expression looks like

random.points <- approx(   cumsum(pdf_of_data$y)/sum(pdf_of_data$y),   pdf_of_data$x,   runif(10000) )$y 

yields

hist(random.points, 100) 

enter image description here


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -