eval(predvars,data,env)中的错误,循环中的逐步线性回归

时间:2017-10-27 09:32:56

标签: r packages linear-regression

我想在函数中实现逐步线性回归。我的代码示例如下。

test_function <- function(formula, dataset, k, repeats) {

  # k is a number of folds used for cross-validation (CV)
  # repeats represents the number of repeats of CV

  for (m in 1:repeats){

    # randomly shuffle dataset
    dataset <- dataset[sample(nrow(dataset)), ]

    #Create k equally size folds
    folds <- cut(seq(1, nrow(dataset)), breaks = k, labels = FALSE)

  for (j in 1:k){

    #Segement my data by fold using the which() function
    testIndexes <- which(folds == j, arr.ind = TRUE)
    test <- dataset[testIndexes, ]
    train <- dataset[-testIndexes, ]

    MLR <- stats::step(lm(formula, data = train), direction = "both", trace = TRUE)
    test_predicted <- predict(MLR, test)

      }
  }
}

# Example
# install.packages("dendroTools") # to get data used in the example below
library(dendroTools)
data("example_dataset_1") # load the data used in the example bellow

test_function(formula = MVA~., dataset = example_dataset_1, k = 10, repeats = 2)

它完美无缺。但我想在我的R包中实现这个功能。当我运行RCMD检查这个例子。我收到以下错误:

... 166 lines ...
- T_aug_sep  1    0.2973  8.0723 -92.866
<none>                    7.7750 -92.817
- T_APR      1    4.2533 12.0283 -72.127

Step:  AIC=-92.87
MVA ~ T_APR

Error in eval(predvars, data, env) : 
  invalid 'envir' argument of type 'closure'
Calls: test_function ... eval -> eval -> <Anonymous> -> model.frame.default -> eval
Execution halted

看起来,当存在非重要变量时,会出现问题。

0 个答案:

没有答案