[R] Simulation function
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@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.
Re: [R] Simulation Function - Save results
On Aug 17, 2009, at 1:40 PM, MarcioRibeiro wrote: Ok, the LIST function I understood... I didn't see how you got a random input to that function. Would seem to need one of the rdist functions as input. What I would like now is to simulate this Function A many times (S) in order to get S results for the MEAN and for the VARIANCE... ?replicate Zhiliang Ma wrote: 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, MarcioRibeiromes...@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@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. __ R-help@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. -- View this message in context: http://www.nabble.com/Simulation-Function---Save-results-tp24977851p25011101.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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.
Re: [R] Simulation function
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.33 $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, MarcioRibeiromes...@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@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? __ R-help@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.
Re: [R] Simulation Function - Save results
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, MarcioRibeiromes...@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@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. __ R-help@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.
[R] Simulation Function - Save results
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@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.