[R] Simulation
Mike Marchywka
marchywka at hotmail.com
Tue Mar 1 13:19:42 CET 2011
----------------------------------------
> Date: Mon, 28 Feb 2011 19:18:18 -0800
> From: kadodamball at hotmail.com
> To: r-help at r-project.org
> Subject: [R] Simulation
>
> I tried looking for help but I couldn't locate the exact solution.
> I have data that has several variables. I want to do several sample
> simulations using only two of the variables (eg: say you have data between
> people and properties owned. You only want to check how many in the samples
> will come up with bicycles) to estimate probabilities and that sort of
> thing.
> Now, I can only do a simulation in terms of this code: sample(1:10, size =
> 15, replace = TRUE).
> I do not know how select specific variables only.
> I'll appreciate the help
This is probably not the best R but you can do something like either of these.
Note that this is just the easiest derivative of stuff I already had
and can be fixed to your needs, I usually use runif instead of sample for example.
The first example probably being much less efficient than the second,
df<-data.frame(a=.1*rnorm(100), b=(1:100)/100,c=(1:100)/100+.1*rnorm(100))
res=1:100; for ( i in 1:100) {res[i]=cor(df[which(runif(100)>.9),])[1,3] }
res
hist(res)
res=1:100; for ( i in 1:100) {wh=which(runif(100)>.9); res[i]=cor(df$a[wh],df$c[wh]); }
res
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Simulation-tp3329173p3329173.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
More information about the R-help
mailing list