【问题标题】:adding n to variable name in R将n添加到R中的变量名
【发布时间】:2021-05-19 14:17:32
【问题描述】:

我正在尝试跟踪我的迭代,以便比较模型选择的结果。

我的代码目前如下所示:

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
}

理想情况下,我希望变量 fit.mod 通过写入新变量 fit.modn 来跟踪 n 的相应值 所以我想以新变量fit.mod2fit.mod3fit.mod4 等结尾。

【问题讨论】:

  • 您可以使用命名列表(可能更好)或assign 函数。

标签: r variables naming


【解决方案1】:

您可以将模型的每次迭代存储在 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)
}

您可以这样命名列表条目:

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

或者,您可以使用lapply()purrr::map() 代替for 循环。他们已经生成(命名)列表作为输出,而且您不需要初始化 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)
})

此外,tidymodels 提供了一个明确的框架,用于迭代同一模型的变体。貌似depmixS4目前不支持,但值得一看。

【讨论】:

    【解决方案2】:

    使用命名对象列表的解决方案:

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

    列表results现在是:

    $iteration_1
      some_value
    1          1
    
    $iteration_2
      some_value
    1          1
    2          2
    
    $iteration_3
      some_value
    1          1
    2          2
    3          3
    

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 1970-01-01
      • 2016-11-06
      • 2013-01-13
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多