在R

时间:2017-11-01 10:50:42

标签: r function arguments cross-validation

我需要在相同的数据上交叉验证几个glmer模型,所以我已经完成了这个功能(我对预先存在的函数不感兴趣)。我想将一个任意的glmer模型作为唯一的参数传递给我的函数。可悲的是,我无法弄清楚如何做到这一点,并且interwebz不会告诉我。

理想情况下,我想做类似的事情:

model = glmer(y ~ x + (1|z), data = train_folds, family = "binomial"
model2 = glmer(y ~ x2 + (1|z), data = train_folds, family = "binomial"

然后拨打cross_validation_function(model)cross_validation_function(model2)。函数中的训练数据称为train_fold。

但是,我怀疑我需要使用reformulate以不同的方式传递模型公式。

以下是我的功能示例。该项目旨在从行为特征预测自闭症(ASD)。数据变量为da

library(pacman)
p_load(tidyverse, stringr, lmerTest, MuMIn, psych, corrgram, ModelMetrics, 
caret, boot)

cross_validation_function <- function(model){ 

  #creating folds  
  participants = unique(da$participant)
  folds <- createFolds(participants, 10)


  cross_val <- sapply(seq_along(folds), function(x) {

    train_folds = filter(da, !(as.numeric(participant) %in% folds[[x]]))
    predict_fold = filter(da, as.numeric(participant) %in% folds[[x]])

    #model to be tested should be passed as an argument here    
    train_model <-  model



    predict_fold <- predict_fold %>% 
      mutate(predictions_perc = predict(train_model, predict_fold, allow.new.levels = T),
             predictions_perc = inv.logit(predictions_perc),
             predictions = ifelse(predictions_perc > 0.5, "ASD","control"))

    conf_mat <- caret::confusionMatrix(data = predict_fold$predictions, reference = predict_fold$diagnosis, positive = "ASD") 

    accuracy <- conf_mat$overall[1]
    sensitivity <- conf_mat$byClass[1]
    specificity <- conf_mat$byClass[2]


    fixed_ef <- fixef(train_model) 

    output <- c(accuracy, sensitivity, specificity, fixed_ef)

    })

    cross_df <- t(cross_val)
    return(cross_df)
  }

从注释开发的解决方案:使用as.formula字符串可以转换为公式,可以通过以下方式作为参数传递给我的函数:

cross_validation_function <- function(model_formula){
...
train_model <- glmer(model_formula, data = da, family = "binomial") 
...}

formula <- as.formula( "y~ x + (1|z"))
cross_validation_function(formula)

1 个答案:

答案 0 :(得分:1)

如果您的目标是从拟合模型中提取模型公式,则可以使用 attributes(model)$call[[2]]。然后,在使用cv folds拟合模型时,可以使用此公式。

   mod_formula <-  attributes(model)$call[[2]]
   train_model = glmer(mod_formula , data = train_data, 
                family = "binomial")