逐步回归无效:作用域

时间:2018-06-26 04:16:34

标签: r

下面的代码是可复制的示例。当我注释掉step函数时,闪亮效果很好。但是当我使用step函数时,闪闪发亮的错误是“找不到对象'tmp.data'”

有人知道如何使step函数可见“ tmp.data”吗?谢谢!

indicators <- mtcars[,c(-1,-6)]
input = list(y='mpg')
tmp.model <- function(){
  tmp.data = cbind(mtcars[input$y], indicators)
  biggest = as.formula(lm(paste(input$y,"~."), tmp.data))
  tmp.model = lm(paste(input$y,"~disp"), tmp.data)
  tmp.model = step(tmp.model, direction="forward", scope=biggest, k=log(nrow(mtcars))) # tmp.data not found
  tmp.model
}
summary(tmp.model())

2 个答案:

答案 0 :(得分:1)

我认为您要解决的问题的简洁版本:

model_fun <- function(df, resp_var, pred_vars) {
  biggest <- as.formula(paste0(resp_var, " ~ ", paste(pred_vars, collapse = " + ")))
  lm_fit <- lm(biggest, data = df)
  step_fit <- step(lm_fit, direction = "forward", scope = biggest, k = log(nrow(df)))
  step_fit
}
model_fun(mtcars, "mpg", c("cyl", "disp", "hp", "drat", "qsec", "vs", "am", "gear", "carb"))

答案 1 :(得分:0)

好吧,我发现了一个非常肮脏的解决方案,可以立即解决,但我仍然希望有人可以给我一种更优雅的方式来解决此范围问题。

indicators <- mtcars[,c(-1,-6)]
input = list(y='mpg')
tmp.model <- function(){
  # directly write into global environment.....
  .GlobalEnv$tmp.data = cbind(mtcars[input$y], indicators)
  biggest = as.formula(lm(paste(input$y,"~."), tmp.data))
  tmp.model = lm(paste(input$y,"~disp"), tmp.data)
  tmp.model = step(tmp.model, direction="forward", scope=biggest, k=log(nrow(mtcars))) # tmp.data not found
  tmp.model
}
summary(tmp.model())

更新:感谢mhammer的回答,现在我们有了一个干净的解决方案

model_fun <- function(df, resp_var, must_include, maybe_include) {
  begin <- as.formula(paste0(resp_var, " ~ ", paste(must_include, collapse = " + ")))
  biggest <- as.formula(paste0(resp_var, " ~ ", paste(c(must_include, maybe_include), collapse = " + ")))
  lm_fit <- lm(begin, data = df)
  step_fit <- step(lm_fit, direction = "forward", scope = biggest, k = log(nrow(df)))
  step_fit
}
model_fun(mtcars, "mpg", "disp", c("cyl", "hp", "drat", "qsec", "vs", "am", "gear", "carb"))