-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lack of support for data.table? #10
Comments
Ok, well a basic hack is simply to convert the data.frame to a data.table, but that's a little annoying:
|
OK, the problem gets stranger because while the above works, if I use basic functionalities of data.table it stops working:
Remove the line |
Correction, it looks like the same problem applies if you use dplyr and data.frames, so ... |
Hi! you can circumvent the issue if you add data.table as an argument to export_also. Here is an example. library(MonteCarlo) N_pop <- 100 func_table <- function(N_pop, N_samp){ func_table(N_pop, 10) MC <- MonteCarlo(func = func_table, nrep = 100, Your code does something that is a little unusual, because you create all the random variables in advance and then sample from them again inside of the func_table function. You then sample again inside of func_table and use properties of the data.table object that is created outside of it. MonteCarlo would therefore have to analyze the objects as well as the functions. That would probably be a nice feature if I manage to do a new iteration. Do you want to use the package for bootstrapping? Otherwise the way to structure it that you have chosen does not make too much sense. If you want to do a simple Monte Carlo simulation you can do it like this: ` rm(list = ls()) library(MonteCarlo) func_table <- function(N_samp){ func_table(10) MC <- MonteCarlo(func = func_table, nrep = 100, ` |
Hi, Concerning your solution I don't think that the problem was the incorrect import of data.table, I think for some reason because of the syntax of the data.table package (but as I've said you have the same issue with the maybe more standard tidyverse) where you use names of columns of the data.table without adding dat$ (which is a big plus of these packages). For example:
This code returns the error:
And it does so independently of whether the export_also option is used. Anyway, thank you for your answer and efforts! |
I have the same issue as @simon-lowe . In my case, I use a data.table to store the bounds on uniformly distributed random numbers, which are then sampled in the function using runif(). I've pasted my code below, but unfortunately cannot attach the input file needed for it to run. `library(Smisc) db = dbConnect(RSQLite::SQLite(), "LWR.sqlite") dta = dbGetQuery(db, "SELECT Time, Sender, Receiver, Commodity, sum(Quantity)*MassFrac as 'U235' From Compositions inner join (SELECT Time, Sender, Receiver, Commodity, Quantity, QualId from resources inner join (SELECT Time, Commodity, Sender, Prototype as 'Receiver', Resourceid, Commodity From AgentEntry inner join (SELECT Time, Commodity, Prototype as 'Sender', Receiverid, Resourceid, Commodity From transactions INNER Join AgentEntry on AgentEntry.AgentId = transactions.SenderId) as ij1 on AgentEntry.AgentId = ij1.ReceiverId) as ij2 on resources.Resourceid = ij2.Resourceid) as ij3 on Compositions.QualId = ij3.QualId where (Compositions.NucId = 922350000) group by Time, Receiver, Commodity") dbDisconnect(db) init_inv_Time = 2 assign("bounds", new_table[, K = 10.0 cusum_test <- function(K, CL){ init_inv = meas[Time==init_inv_Time,Inventory] y = cusum(MUF[,MUF], K, CL) K_grid = numeric(10)+K The code gives the following error:
Simon mentioned that simply exporting the data.table solved his problem. How would I do that? Cheers. |
I've answered my own question, I think. The "data" element in export_also parameter list can be used to export the data.table I want to access in the function. Using export_also = list("data"=bounds,"packages"=data.table" changes the error to
I don't know how 1:359 is being seen as a variable. Maybe it has something to do with referencing rows in one of the data.tables, since there are 359 rows in the 'bounds' data.table. |
I got it working by putting in explicit references to column names using $ and setting export_also = list("data"="bounds","packages"=data.table") I think it would improve the montecarlo package a lot if you could make it work better with data.tables. |
Hi,
I think I like this package, but it does seem to not support data.table.
The code below does identical things, except that once it uses data.table and once data.frame. The latter works while the former does not.
The text was updated successfully, but these errors were encountered: