代码之家  ›  专栏  ›  技术社区  ›  James White

在单独的数据框中根据选择标准创建大量统计模型

  •  0
  • James White  · 技术社区  · 7 年前

    我希望根据数据帧中指定的选择标准执行一定数量的统计模型。使用一个基本的例子,假设我有两个回答变量和两个解释变量:

    #######################Data Input############################
    Responses <- as.data.frame(matrix(sample(0:10, 1*100, replace=TRUE), ncol=2))
    colnames(Responses) <- c("A","B")
    
    Explanatories <- as.data.frame(matrix(sample(20:30, 1*100, replace=TRUE), ncol=2))
    colnames(Explanatories) <- c("x","y")
    

    然后,我定义了我想要运行的统计模型,这些模型可以包括响应/解释变量的不同组合和不同的统计函数:

    ###################Model selection#########################
    Function <- c("LIN","LOG","EXP") ##Linear, Logarithmic (base 10) and exponential - see the formula for these below
    Respo <- c("A","B","B")
    Explan <- c("x","x","y")
    Model_selection <- data.frame(Function,Respo,Explan)
    

    然后如何根据这些选择标准执行模型列表?下面是我想根据模型选择数据框的输入创建的模型示例。

    ####################Model creation#########################
    Models <- list(
    lm(Responses$A ~ Explanatories$x),
    lm(Responses$B ~ log10(Explanatories$x)),
    lm(Responses$B ~ exp(Explanatories$y))
    )
    

    我猜需要某种循环函数,看看周围的情况,也许也需要粘贴?提前感谢您在这方面的帮助

    2 回复  |  直到 7 年前
        1
  •  1
  •   thothal    7 年前

    这是应用程序的完美用例 tidyverse

    library(tidyverse)
    
    ## cbind both data sets into one
    my_data <- cbind(Responses, Explanatories)
    
    ## use 'mutate' to change function names to the existing function names
    ## mutate_all to transform implicit factors to characters
    ## NB this step could be ommitted if Function would already use the proper names
    model_params <- Model_selection %>%
       mutate(Function = case_when(Function == "LIN" ~ "identity",
                                   Function == "LOG" ~ "log10",
                                   Function == "EXP" ~ "exp")) %>%
       mutate_all(as.character)
    
    ## create a function which estimates the model given the parameters
    ## NB: function params must be named exactly like columns 
    ## in the model_selection df
    make_model <- function(Function, Respo, Explan) {
      my_formula <- formula(paste0(Respo, "~", Function, "(", Explan, ")"))
      my_mod <- lm(my_formula, data = my_data)
      ## syntactic sugar: such that we see the value of the formula in the print
      my_mod$call$formula <- my_formula
      my_mod
    }
    
    ## use purrr::pmap to loop over the model params
    ## creates a list with all the models
    pmap(model_params, make_model)
    
        2
  •  1
  •   bobbel    7 年前

    Models <- list()
    idx <- 1L
    for (row in 1:nrow(Model_selection)){
    
      if (Model_selection$Function[row]=='LOG'){
        expl <- paste0('LOG', Model_selection$Explan[row])
        Explanatories[[expl]] <- log10(Explanatories[[Model_selection$Explan[row]]])
        Models[[idx]] <- lm(Responses[[Model_selection$Respo[row]]] ~ Explanatories[[expl]])
      }
      if (Model_selection$Function[row]=='EXP'){
        expl <- paste0('EXP', Model_selection$Explan[row])
        Explanatories[[expl]] <- exp(Explanatories[[Model_selection$Explan[row]]])
        Models[[idx]] <- lm(Responses[[Model_selection$Respo[row]]] ~ Explanatories[[expl]])
      }
      if (Model_selection$Function[row]=='LIN'){
        expl <- paste0('LIN', Model_selection$Explan[row])
        Explanatories[[expl]] <- Explanatories[[Model_selection$Explan[row]]]
        Models[[idx]] <- lm(Responses[[Model_selection$Respo[row]]] ~ Explanatories[[expl]])
      }
      names(Models)[idx] <- paste(Model_selection$Respo[row], '~', expl)
      idx <- idx+1L
    }
    Models