0

I am working on developing and optimizing a linear model using the lm() function and subsequently the step() function for optimization. I have added a variable to my dataframe by using a random generator of 0s and 1s (50% chance each). I use this variable to subset the dataframe into a training set and a validation set If a record is not assigned to the training set it is assigned to the validation set. By using these subsets I am able to estimate how good the fit of the model is (by using the predict function for the records in the validation set and comparing them to the original values). I am interested in the coefficients of the optimized model and in the results of the KS-test between the distributions of the predicted and actual results.

All of my code was working fine, but when I wanted to test whether my model is sensitive to the subset that I chose I ran into some problems. To do this I wanted to create a for (i in 1:10) loop, each time using a different random subset. This turned out to be quite a challenge for me (I have never used a for loop in R before).

Here's the problem (well actually there are many problems, but here is one of them):

I would like to have separate dataframes for each run in the loop with a unique name (for example: Run1, Run2, Run3). I have been able to create a variable with different strings using paste(("Run",1:10,sep=""), but that just gives you a list of strings. How do I use these strings as names for my (subsetted) dataframes?

Another problem that I expect to encounter: Subsequently I want to use the fitted coefficients for each run and export these to Excel. By using coef(function) I have been able to retrieve the coefficients, however the number of coefficients included in the model may change per simulation run because of the optimization algorithm. This will almost certainly give me some trouble with pasting them into the same dataframe, any thoughts on that?

Thanks for helping me out.

2 Answers 2

7

For your first question:

You can create the strings as before, using

df.names <- paste(("Run",1:10,sep="")

Then, create your for loop and do the following to give the data frames the names you want:

for (i in 1:10){
   d.frame <- # create your data frame here
   assign(df.name[i], d.frame)
}

Now you will end up with ten data frames with ten different names.

For your second question about the coefficients:

As far as I can tell, these don't naturally fit into your data frame structure. You should consider using lists, as they allow different classes - in other words, for each run, create a list containing a data frame and a numeric vector with your coefficients.

3
  • 1
    An easier alternative may even be to just create the names within your loop, using assign(paste("run", i, sep=""), d.frame). This will avoid any issues if you decide to change the number of runs but forget to change the number everywhere.
    – Edward
    Jul 24, 2012 at 19:58
  • Actually I managed to solve the first and second problem both at once (without the need to create 10 different dataframes). What I did was export the coefficients of the different runs to a dataframe and then merge that dataframe to an empty dataframe (in the first run) like this: Jul 25, 2012 at 15:04
  • dataframe<- data.frame() START LOOP loop.frame<- stack(model$coef) Then I transposed the loop.frame to get a frame with just 1 data row and multiple columns. Then (within the loop) dataframe<- merge(dataframe, loopframe, all.x=TRUE, all.y=TRUE) gave me a frame with all the values for all the coefficients in the rows (giving NA for runs that do not include that variable) and the names of the coefficients in the columns. Jul 25, 2012 at 15:13
4

Don't create objects with numbers in their names, and then try and access them in a loop later, using get and paste and assign. The right way to do this is to store your elements in an R list object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.