我正在尝试使用泛型函数来构造线性回归的公式。我希望函数创建公式
我可以使用数据框中存在的所有变量创建公式,但我的问题是当我尝试获取用户定义的变量时,我不知道如何获取变量以便稍后使用它们来创建公式。
我到目前为止的功能是:
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)
}
}
请任何暗示将深表赞赏
答案 0 :(得分:2)
唯一改变的是如何获得自变量
如果用户指定了它们,则直接使用该字符向量
否则,你必须采取除因变量之外的所有变量(你已经在做)
注意:正如Roland所说,公式就像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)
希望它有所帮助!