sambofra / bnstruct

R package for Bayesian Network Structure Learning
GNU General Public License v3.0
17 stars 11 forks source link

Error in .local(x, ...) : There should be the same number of variables in each time step. #10

Closed OliPerkins1987 closed 5 years ago

OliPerkins1987 commented 5 years ago

Hi there,

I am trying to use your great package for learning dbn structures. When I run the learn.dynamic.network function on my DBN with #timesteps set to 1, it works fine. However, when set this to any number other than 1, it produces the following error:

Error in .local(x, ...) : There should be the same number of variables in each time step.

if I leave the argument to default, the error is returned -

Error in .local(x, ...) : promise already under evaluation: recursive default argument reference or earlier problems?


Here is my data -

new("BNDataset", name = "BNDataset", header.file = "", data.file = "", variables = c("Year", "Price", "Yield", "Production", "Ethanol", "Blender.cost.of.ethanol.with.credit2", "Gasoline", "Fuel.ethanol.use", "Feed.and.residual.use", "Exports", "Ethanol.share.of.total.use", "Corn.Ethanol.Cap", "Import.Tariff..0.01....gallon.", "General.tax.credit", "Processing.Cap"), node.sizes = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 2L, 2L), num.variables = 15L, discreteness = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), num.items = 28L, has.raw.data = TRUE, has.imputed.data = FALSE, raw.data = structure(c(5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 1, 1, 1, 2, 1, 2, 2, 2, 5, 5, 5, 5, 1, 2, 5, 5, 2, 4, 4, 3, 4, 4, 4, 4, 3, 3, 3, 3, 5, 5, 1, 5, 2, 5, 1, 1, 5, 1, 2, 2, 5, 2, 3, 2, 3, 3, 3, 4, 3, 2, 1, 4, 4, 4, 4, 4, 5, 5, 3, 5, 4, 5, 5, 3, 3, 3, 4, 3, 5, 4, 1, 4, 4, 2, 1, 2, 1, 1, 4, 2, 2, 1, 2, 2, 2, 3, 1, 5, 5, 1,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.of.total.use", "Corn.Ethanol.Cap", "Import.Tariff..0.01....gallon.", "General.tax.credit", "Processing.Cap" ))), imputed.data = structure(0, .Dim = c(1L, 1L)), has.boots = FALSE, boots = list(NULL), has.imputed.boots = FALSE, imp.boots = list( NULL), num.boots = 0, num.time.steps = 28)

and here is my code -

Corn.BND <- BNDataset(Corn.discrete, discreteness = rep(TRUE, times = ncol(Corn)), variables = colnames(Corn), node.sizes = vals, num.time.steps = 28)

test <- learn.dynamic.network(Corn.BND, num.time.steps = 28)

Any thoughts on the possible issue would be warmly received.

Oli

albertofranzin commented 5 years ago

Hi Oli,

you are confusing (the representations of) observations and time steps.

You have a system with 15 variables, and you declare a matrix with 15 columns (variables) and 28 rows (observations). This works fine with num.time.steps = 1 because bnstruct considers each row to belong to the same instant.

But if your observations refer to subsequent instants, then what you actually want to declare is one single observation (= 1 row) of a system (of 15 variables) that evolves over 28 time steps. In bnstruct you have to represent by "expanding horizontally" the observation, that is, placing one instant next to the previous one, not below. So, your data has to be formatted as a table with 1 row and 15x28 columns, because you don't have 28 separate observations but one single observation of the entire evolving system.

Please take a look at section 4.1.2 of the vignette and the example files provided in the package.

I hope this clarifies a bit.

OliPerkins1987 commented 5 years ago

Thanks for clarifying this - I had not understood that.

Oli