[R] Simulation Function - Save results

Zhiliang Ma zhiliang.ma at gmail.com
Sat Aug 15 04:48:49 CEST 2009


in order to return more multiple variables, you can put them in a list
and then return this list.

e.g.

#Function A
boot<-function(a,b,c){
mean_boot<-(a+b)/2
var_boot<-c

list(mean_boot = mean_boot, var_boot = var_boot)
}

out <- boot(1,2,3)
out
$mean_boot
[1] 1.5

$var_boot
[1] 3



On Fri, Aug 14, 2009 at 1:15 PM, MarcioRibeiro<mestat at pop.com.br> wrote:
>
> Hi listers,
> I am working on a simulation... But I am having some troubles...
> Suppose I have a function A which produces two results (mean and
> variance)...
> Then I would like to simulate this function A with a function B many times
> using the results from function A
> For example:
>
> #Function A
> boot<-function(a,b,c){
> mean_boot<-(a+b)/2
> var_boot<-c
> #list(a=a,b=b,c=c)
> return(a)
> }
>
> Then I would like to create 2 vectors with the mean and var results from S
> simulations
>
> #Function B
> simul<-function(S){
> teste<-rep(0,S)
> for(i in 1:S){
> teste[i]<-boot(10,12,15)  #ACCORDING TO FUNCTION A I AM SAVING JUST THE
> MEAN_BOOT, BUT I ALSO NEED THE RESULT OF VAR_BOOT
> }
> var<-var(teste)
> mean_emp<-mean(var_boot) #THIS IS NOT WORKING, BECAUSE I DONT HAVE THE
> VAR_BOOT AT MY VECTOR
> var_emp<-(sum((var_boot-var)**2))/S #THIS IS NOT WORKING, BECAUSE I DONT
> HAVE THE VAR_BOOT AT MY VECTOR
> }
> simul(5)
>
> But my problem is that I don't know how to save my results in 2 vectors in
> order to use then at function B.
> Thanks in advance,
> Marcio
>
> --
> View this message in context: http://www.nabble.com/Simulation-Function---Save-results-tp24977851p24977851.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