How can I pass back a Large Mids object?

Folks:

I am doing a capstone project. As such I have two datasets to work with; training data and testing data. Both datasets have missing values. My goal is to do that with mice. When preparing the training data, I:

Init = mice(data = Values, maxit = 0) 
Imputed = mice(data = Values, method = 'cart', predictorMatrix = Init$predictorMatrix, m = 1)
Imputed = complete(Imputed)
Values = Imputed

This works great. Subsequent to this code I can build my regression model no issues.

My problem is - I am doing this in a function that:

Output = list(TrainingSet, TestingSet, Init)
return(Output)

When the function (called from my main module) completes - I attempt to unpack this list via:

TrainingSet <<- data.frame(TrainingData[1])
TestingSet <<- data.frame(TrainingData[2])
Init <<- as.mids(TrainingData[3])

The two datasets unpack successfully - but the init object does not. I get Error in data[, .imp] : incorrect number of dimensions.

When I do a str(TrainingData[3]) I get a list of 1 containing a list of 17. I get the same thing if I str(TrainingData[3][1]). The init object - when I create it in the function - is a single list of 17.

How can I pass this object back from a function?

My end goal - if it matters - is to use the predictorMatrix created when Imputing the training data when I am imputing the testing data - which also contains missing variables.

Any help will be greatly appreciated. Thank You.

I don't fully understand your question, but you might want to use double brackets [[1]] to get the list items themselves, otherwise they will be embedded in a list. You can also reference list items using their name TrainingData$TrainingSet, TrainingData$TestingSet, TrainingData$Init

Also,the double arrow <<- is used to set a variable in the parent environment. You don't need it if you're returning your results via a list.

Also, you might want as.data.frame rather than data.frame

Thanks for the reply. I used the <<- in the function. The goal of my function is to take the training data from .csv to preprocessed Training and Testing sets. Having the function leave those in the environment (as opposed to passing them back to Main in a list) is much easier.

Probably best if you supply a fully reproducable example (reprex). Hard to understand your intent from these little code snippets.

You can also use get and assign to work with variables in other environments (e.g. outside a function)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.