Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在单独的数据框中根据选择标准创建大量统计模型_R_Loops_Dataframe_Formula - Fatal编程技术网

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

R 在单独的数据框中根据选择标准创建大量统计模型,r,loops,dataframe,formula,R,Loops,Dataframe,Formula,我希望根据数据帧中指定的选择标准执行一定数量的统计模型。使用一个基本的例子,假设我有两个回答变量和两个解释变量: #######################Data Input############################ Responses <- as.data.frame(matrix(sample(0:10, 1*100, replace=TRUE), ncol=2)) colnames(Responses) <- c("A","B") Explanatorie

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

#######################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")
数据输入############################
回答这不是最漂亮的解决方案,但它似乎适用于您的示例:

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

Models这不是最漂亮的解决方案,但它似乎适用于您的示例:

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

Models这是
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)
库(tidyverse)
##将两个数据集绑定为一个
我的_数据%
全部变异(如字符)
##创建一个函数,在给定参数的情况下估计模型
##注意:函数参数的命名必须与列完全相同
##在模型中,选择df

make_model这是
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)
库(tidyverse)
##将两个数据集绑定为一个
我的_数据%
全部变异(如字符)
##创建一个函数,在给定参数的情况下估计模型
##注意:函数参数的命名必须与列完全相同
##在模型中,选择df
制造模型