r中函数中的用户定义变量

user defined variables in a function in r

我正在尝试做一个通用函数来构建线性回归的公式。我希望该函数创建公式

我可以使用数据框中存在的所有变量创建公式,但我的问题是当我尝试获取用户定义的变量时,我不知道如何获取变量以便稍后使用它们来创建公式.

我目前的功能是这样的:

lmformula <- function (data, IndepVariable = character, VariableList = TRUE){
  if (VariableList) {
newlist <- list()
newlist <-  # Here is where I do not exactly what to do to extract the variables defined by user
DependVariables <- newlist
f <- as.formula(paste(IndepVariable, "~", paste((DependVariables), collapse = '+')))
 }else {
names(data) <- make.names(colnames(data))
DependVariables <- names(data)[!colnames(data)%in% IndepVariable]
f <- as.formula(paste(IndepVariable,"~", paste((DependVariables), collapse = '+')))
return (f)
 }
}

如有任何提示,我们将不胜感激

唯一改变的是获取自变量的方式

如果用户指定它们,则直接使用该字符向量

否则,您必须获取因变量以外的所有变量(您已经在做)

注意:正如罗兰所说,公式就像 dependentVariable ~ independentVariable1 + independentVariable2 + independentVariable3

# creating mock data
data <- data.frame(col1 = numeric(0), col2 = numeric(0), col3 = numeric(0), col4 = numeric(0))

# the function
lmformula <- function (data, DepVariable, IndepVariable, VariableList = TRUE) {
  if (!VariableList) {
    IndepVariable <- names(data)[!names(data) %in% DepVariable]
  }
  f <- as.formula(paste(DepVariable,"~", paste(IndepVariable, collapse = '+')))
  return (f)
}

# working examples
lmformula(data = data, DepVariable = "col1", VariableList = FALSE)
lmformula(data = data, DepVariable = "col1", IndepVariable = c("col2", "col3"), VariableList = TRUE)

希望对您有所帮助!