FunWithR / MonteCarlo

An R package for simulation studies.
33 stars 15 forks source link

Scalar Error #8

Closed carldister closed 6 years ago

carldister commented 6 years ago

I tried the sample Montecarlo example on my version of R and it worked perfectly. Then, I made a much larger project with many more variables and received this error:

Error in MonteCarlo(func = runfunction, nrep = 1000, param_list = param_list) : Scalars in list components cannot be named.

I tried to make sure that the variable types matched the examples. Is there a limit to how many variables and how many combinations, or is that simply CPU execution time limited?

carldister commented 6 years ago

Also, I tried tweaking the example slightly and received and error - here is my slight modification and the error follows:

1. Define the function the inputs will apply to...in this example three input variables

ttest<-function(n,loc,scale){ stat<-nlocscale

get test decision:

decision<-abs(stat)>1.96

return result:

return(list("decision"=decision))

}

2. Define parameter grid: (input variables you would like to vary)

n_grid<-c(50,100,250,500) loc_grid<-c(0,1,0.2) scale_grid<-c(1,2)

collect parameter grids in list:

param_list=list("n"=n_grid, "loc"=loc_grid, "scale"=scale_grid)

Now, finally, run the monte carlo

MC_result<-MonteCarlo(func=ttest, nrep=1000, param_list=param_list)

Error in MonteCarlo(func = ttest, nrep = 1000, param_list = param_list) : object 'packages' not found

carldister commented 6 years ago

Thank you for any advice you may have on these issues

FunWithR commented 6 years ago

Hi! The error "Scalars in list components cannot be named." refers to the list returned by your runfunction. The scalars in this list are not allowed to be named. As an example

test_func<-function(n,loc,scale){ sample<-rnorm(n, loc, scale) stat<-sqrt(n)*mean(sample)/sd(sample) decision<-abs(stat)>1.96 names(decision)<-"decision" return(list("decision"=decision)) } will reproduce your error. But this will work:

test_func<-function(n,loc,scale){ sample<-rnorm(n, loc, scale) stat<-sqrt(n)*mean(sample)/sd(sample) decision<-abs(stat)>1.96 names(decision)<-"decision" return(list("decision"=unname(decision))) }

FunWithR commented 6 years ago

With regard to your modification of the example. There is a known bug that appears if the function to simulate is a simple deterministic one. This will be fixed soon with the next version of the package. But obviously, trying to put deterministic functions in there doesn't really make sense. Why would you want to repeat calculating that function many times if the outcome is always the same?

carldister commented 6 years ago

The solution of using the "unname" worked great! Thanks so much for your help