繁体   English   中英

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

[英]Pass model formula as argument in 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。

但是,我怀疑我需要使用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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM