将模型公式作为R中的参数传递

我需要在相同的数据上交叉验证几个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.

但是,我怀疑我需要使用重新制定以不同的方式传递模型公式.

这是我的功能的一个例子.该项目旨在从行为特征预测自闭症(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)

最佳答案 如果您的目标是从拟合模型中提取模型公式,则可以使用

属性(模型)$通话[[2]].然后,您可以在使用cv折叠拟合模型时使用此公式.

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