0

I am trying to keep track of my iterations so I can compare the outcomes for model selection.

My code currently looks like this:

library(depmixS4) 
n <- 2
for (i in 1:11) {
  model <- mix(list(DCRF$Specify_SaO2 ~ 1, DCRF$Haematocrit_1 ~ 1, DCRF$Glasgow_Coma_Score ~ 1),
               family = list(gaussian("identity"),  # For every corresponding 
                             gaussian("identity"),  #    indicator a family of distribution 
                             multinomial("identity")), #    should be indicated in the list.
               data = DCRF,        
               nstates = n,        # This is the number of classes
               initdata = DCRF     
  )
  fit.mod <- fit(model)   
  
  print(fit.mod)

  n <- n + 1
}

Ideally, I would like variable fit.mod to keep track of the corresponding value of n, by writing to a new variable fit.modn So I want to end up with the new variables fit.mod2, fit.mod3, fit.mod4, and so on.

Bram
  • 1

2 Answers2

1

You can store each iteration of the model in a list.

library(depmixS4) 

fit.mods <- list() # we initialize an empty list

for (i in 1:11) {

  model <- mix(list(DCRF$Specify_SaO2 ~ 1, DCRF$Haematocrit_1 ~ 1, DCRF$Glasgow_Coma_Score ~ 1),
               family = list(gaussian("identity"),  # For every corresponding 
                             gaussian("identity"),  #    indicator a family of distribution 
                             multinomial("identity")), #    should be indicated in the list.
               data = DCRF,        
               nstates = i + 1,        # This is the number of classes
               initdata = DCRF     
  )
  fit.mods[i] <- fit(model)   # using bracket indexing we write each fitted model to a separate list entry
  
  print(fit.mod)
}

You can name the list entries like this:

names(fit.mods) <- paste0("fit.mod", 1:11)

Alternatively you could use lapply() or purrr::map() instead of a for loop. They already produce (named) lists as outputs and also you don't need to initialize the list.

fit.mods <- lapply(1:11, function(x) {
  model <-
    mix(
      list(
        DCRF$Specify_SaO2 ~ 1,
        DCRF$Haematocrit_1 ~ 1,
        DCRF$Glasgow_Coma_Score ~ 1
      ),
      family = list(
        gaussian("identity"),
        # For every corresponding
        gaussian("identity"),
        #    indicator a family of distribution
        multinomial("identity")
      ),
      #    should be indicated in the list.
      data = DCRF,
      nstates = x + 1,
      # This is the number of classes
      initdata = DCRF
    )
  fit(model)
})

Also the tidymodels provide an explicit framework for iterating through variations of the same model. It looks like depmixS4 is currently not supported, but it is worth taking a look at it.

Till
  • 3,845
  • 1
  • 11
  • 18
0

Solution using named lists of objects:

results <- list()

for (i in 1:3) {
  results[[paste0("iteration_", i)]] <- data.frame(some_value = 1:i)
}

The list results is now:

$iteration_1
  some_value
1          1

$iteration_2
  some_value
1          1
2          2

$iteration_3
  some_value
1          1
2          2
3          3
dario
  • 6,415
  • 2
  • 12
  • 26