[R] Simulation function

jim holtman jholtman at gmail.com
Wed Aug 19 01:01:05 CEST 2009


You need to store your results in a list and then access the
information in the list to get the values:

> boot<-function(a,b,c){
+     media<-(a+b+c)/3
+     var_app<-var(a)
+     list(media=media,var_app=var_app)
+ }
> boot(2,4,10)
$media
[1] 5.333333

$var_app
[1] NA

>
> simul<-function(S){
+     results<-list()
+     for(i in 1:S){
+         results[[i]]<-boot(2,4,10)
+     }
+     var<-var(sapply(results, '[[', 'media'))
+     mean_var<-mean(sapply(results, '[[', 'var_app'))
+     var_var<-var(sapply(results, '[[', 'var_app'))
+     list(var=var,mean_var=mean_var,var_var=var_var)
+ }
> simul(5)
$var
[1] 0

$mean_var
[1] NA

$var_var
[1] NA

>


On Tue, Aug 18, 2009 at 11:55 AM, MarcioRibeiro<mestat at pop.com.br> wrote:
>
> Hi listers,
> I've been looking for a procedure, but I am not succeding...
> I have a function that give multiple results...
> Then, I would like to simulate this function n times, so I need to save/keep
> the n multiple results, in order to calculate my desired statistics...
> I have already tried with the RETURN and LIST FUNCTION, but I am not getting
> it right...
> An example of what I am looking for sounds like this...
>
> boot<-function(a,b,c){
> media<-(a+b+c)/3
> var_app<-var(a)
> list(media=media,var_app=var_app)
> }
> boot(2,4,10)
>
> simul<-function(S){
> results<-rep(0,S)
> for(i in 1:S){
> results[i]<-boot(2,4,10)
> }
> var<-var(media)
> mean_var<-mean(var_app)
> var_var<-var(var_app)
> list(var=var,mean_var=mean_var,var_var=var_var)
> }
> simul(5)
>
> --
> View this message in context: http://www.nabble.com/Simulation-function-tp25027993p25027993.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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list