Re: [R] Fit model to data and use model for data generation

From: Stephen D. Weigand <weigand.stephen_at_charter.net>
Date: Thu 25 Jan 2007 - 05:03:53 GMT

On Jan 24, 2007, at 10:34 AM, Benjamin Otto wrote:

> Hi,
>
> Suppose I have a set of values x and I want to calculate the
> distribution of
> the data. Ususally I would use the "density" command. Now, can I use
> the
> resulting "density-object" model to generate a number of new values
> which
> have the same distribution? Or do I have to use some different
> function?
>
> Regards,
>
> Benjamin
>
> --
> Benjamin Otto
> Universitaetsklinikum Eppendorf Hamburg
> Institut fuer Klinische Chemie
> Martinistrasse 52
> 20246 Hamburg
>

You could sample from the x's in the density object with probability given by the y's:

### Create a bimodal distribution
x <- c(rnorm(25, -2, 1), rnorm(50, 3, 2)) d <- density(x, n = 1000)
plot(d)

### Sample from the distribution and show the two ### distributions are the same
x.new <- sample(d$x, size = 100000, # large n for proof of concept

                 replace = TRUE, prob = d$y/sum(d$y))
dx.new <- density(x.new)
lines(dx.new$x, dx.new$y, col = "blue")

Hope this helps,

Stephen
Rochester, Minnesota, USA



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Jan 25 16:14:06 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 25 Jan 2007 - 08:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.