VicaYang
VicaYang

Reputation: 554

Step regression not work: scoping

The code below is a reproducible example. When I comment out the step function, the shiny works well. But when I use step function, the shiny gives the error "object 'tmp.data' not found"

Does anyone have the idea how to make 'tmp.data' visible to the step function? Thank you!

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())

Upvotes: 0

Views: 249

Answers (2)

VicaYang
VicaYang

Reputation: 554

Well, I found a very very dirty solution to solve it right now, but I still hope someone can give me a more elegant way to solve this scope problem.

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())

Update: Thanks to mhammer's answer, now we have a clean solution

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"))

Upvotes: 0

MHammer
MHammer

Reputation: 1314

A slightly cleaner version of what I think you're trying to solve:

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"))

Upvotes: 1

Related Questions