[R] reading simulations

David Winsemius dwinsemius at comcast.net
Sat Feb 19 17:22:45 CET 2011


On Feb 19, 2011, at 10:35 AM, garciap wrote:

>
> Hi to all the people (again),
>
> I'm doing some simulations with the memisc package of an own  
> function, but
> I've the problem that I didn't know how to read the result of such
> simulations. My function is:
>
>> Torre<-function(a1,N1,a2,N2)
> + {Etorre<-(a1*N1)/(1+a1*N1)
> + Efuera<-(a2*N2)/(1+a2*N2)
> + if(Etorre>Efuera)Subir=TRUE
> + if (Etorre<Efuera)Subir=FALSE
> + result<-list(Subir)
> + return(result)
> + }
>
>> Torre<- 
>> Simulate 
>> (Torre 
>> (a1 
>> ,N1 
>> ,a2 
>> ,N2 
>> ),expand 
>> .grid 
>> (a1 
>> = 
>> 3 
>> ,N1 
>> = 
>> 0.5 
>> ,a2 
>> = 
>> c 
>> (0.01,0.02,0.05,0.1 
>> ),N2=0.1),nsim=1000,seed=10000,trace=50,keep.data=TRUE)
>

?Simulate

Try something like this, since I thought your example was far from  
"minimal". After reading that the value was a dataframe, I just used  
standard indexing to access individual rows. I used instead the first  
example from the help page:
require(memisc)
Normal.example <- function(mean=0,sd=1,n=10){
  x <- rnorm(n=n,mean=mean,sd=sd)
  c( Mean=mean(x), Median=median(x), Var=var(x) ) }
Normal.simres <- Simulate(
  Normal.example(mean,sd,n),
  expand.grid( mean=0, sd=c(1,10), n=c(10,100) ), nsim=200, trace=50)
 > Normal.simres[1, ]
   mean sd  n       Mean     Median      Var
1    0  1 10 0.01784074 -0.3662563 1.381853
 > Normal.simres[200, ]
     mean sd  n      Mean    Median      Var
200    0  1 10 0.4004172 0.3280587 1.207104

That object does pose some surprises:
 > str(Normal.simres[, 1])  # regular extraction doesn't return what I  
expect
'data.frame':	800 obs. of  6 variables:
  $ mean  : num  0 0 0 0 0 0 0 0 0 0 ...
  $ sd    : num  1 1 1 1 1 1 1 1 1 1 ...
  $ n     : num  10 10 10 10 10 10 10 10 10 10 ...
  $ Mean  : num  0.0178 -0.0803 0.4951 0.2081 -0.3496 ...
  $ Median: num  -0.36626 -0.29166 0.27018 -0.00887 -0.5638 ...
  $ Var   : num  1.382 0.798 1.18 0.48 0.571 ...
 > is.data.frame(Normal.simres)
[1] FALSE
 > is.data.frame(Normal.simres[1])
[1] TRUE
 > str(Normal.simres[, 1][2])
'data.frame':	800 obs. of  1 variable:
  $ sd: num  1 1 1 1 1 1 1 1 1 1 ...
 > str(Normal.simres[, 1]["Mean"])  # but it is possible to extract  
the "good stuff"
'data.frame':	800 obs. of  1 variable:
  $ Mean: num  0.0178 -0.0803 0.4951 0.2081 -0.3496 ...

So it appears to be something not quite a dataframe.

 > quantile(Normal.simres[]$Mean , c(.25,.5,.75))  # works
         25%         50%         75%
-0.22652591  0.02842814  0.42513166

 > str(Normal.simres$Mean ) # doesn't work
  NULL

> ---------------------------------------------------
>  a1  N1   a2  N2
> 1  3 0.5 0.01 0.1
> Replication  50
> Replication  100
> Replication  150
> Replication  200
> Replication  250
> Replication  300
> Replication  350
> Replication  400
> Replication  450
> Replication  500
> Replication  550
> Replication  600
> Replication  650
> Replication  700
> Replication  750
> Replication  800
> Replication  850
> Replication  900
> Replication  950
> Replication  1000
>
> ---------------------------------------------------
>  a1  N1   a2  N2
> 2  3 0.5 0.02 0.1
> Replication  50
> Replication  100
> Replication  150
> Replication  200
> Replication  250
> Replication  300
> Replication  350
> Replication  400
> Replication  450
> Replication  500
> Replication  550
> Replication  600
> Replication  650
> Replication  700
> Replication  750
> Replication  800
> Replication  850
> Replication  900
> Replication  950
> Replication  1000
>
> ---------------------------------------------------
>  a1  N1   a2  N2
> 3  3 0.5 0.05 0.1
> Replication  50
> Replication  100
> Replication  150
> Replication  200
> Replication  250
> Replication  300
> Replication  350
> Replication  400
> Replication  450
> Replication  500
> Replication  550
> Replication  600
> Replication  650
> Replication  700
> Replication  750
> Replication  800
> Replication  850
> Replication  900
> Replication  950
> Replication  1000
>
> ---------------------------------------------------
>  a1  N1  a2  N2
> 4  3 0.5 0.1 0.1
> Replication  50
> Replication  100
> Replication  150
> Replication  200
> Replication  250
> Replication  300
> Replication  350
> Replication  400
> Replication  450
> Replication  500
> Replication  550
> Replication  600
> Replication  650
> Replication  700
> Replication  750
> Replication  800
> Replication  850
> Replication  900
> Replication  950
> Replication  1000
>> print(Torre)
> A 'default_bucket' object with 5 variables and 4000 observations
>
> When I try print the results I obtain this, but how can I read it?  
> It will
> seem stupid to people, but I can't! even though function  
> summary(Torre):
>
>> summary(Torre)
> Error in object[[i]] : wrong arguments for subsetting an environment
>>
>
>
> Best regards (again) and many thanks for your help
>
> Pablo
> -- 
> View this message in context: http://r.789695.n4.nabble.com/reading-simulations-tp3314281p3314281.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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list