【问题标题】:R eval(predvars, data, env) object not found by passing a pameter in a function通过在函数中传递参数找不到R eval(predvars,data,env)对象
【发布时间】:2022-12-11 23:18:16
【问题描述】:

我的可重现示例如下;

请不要理会计算的潜在含义(实际上没有),因为它只是我真实数据集的摘录;

train <- structure(list(no2 = c(25.5, 31.2, 33.4, 29.9, 31.8),
                        vv_scal = c(1.3, 1.3, 0.8, 1.1, 0.9), 
                        temp = c(-0.7, -2, 1.5, 0.4, 1.1), 
                        prec = c(0, 11, 9, 3, 0), 
                        co = c(1.6, 2.9, 3.2, 2.6, 3)), 
                        row.names = c(NA, -5L), 
                        class = c("tbl_df", "tbl", "data.frame"))


test <- structure(list(no2 = c(41.6, 41.4, 46.6, 44.7, 43.2), 
                       vv_scal = c(1.2, 1.2, 1.2, 1, 1), 
                       temp = c(0.9, 1, 0.1, 1.6, 3.8), 
                       prec = c(0, 0, 0, 0, 0), 
                       co = c(4.3, 4.3, 4.9, 4.7, 4.5)), 
                       row.names = c(NA, -5L), 
                       class = c("tbl_df", "tbl", "data.frame"))
                       
                       

forest_ci <- function(B, train_df, test_df, var_rf){
  
  # Initialize a matrix to store the predicted values
  predictions <- matrix(nrow = B, ncol = nrow(test_df))
  
  # bootstrapping predictions
  for (b in 1:B) {
    
    # Fit a random forest model
    model <- randomForest::randomForest(var_rf~., data = train_df) # not working
    #model <- randomForest::randomForest(no2~., data = train_df)   # working
    
    # Store the predicted values from the resampled model
    predictions[b, ] <- predict(model, newdata = test_df)
    
  }
  
  predictions
  
}

predictions <- forest_ci(B=2, train_df=train, test_df=test, var_rf = no2)

我收到以下错误消息:

Error in eval(predvars, data, env) : object 'no2' not found

我认为理解错误与“非标准评估”和“捕获表达式”的概念有某种关系

http://adv-r.had.co.nz/Computing-on-the-language.html

根据一些线程的建议,这里遵循其中的一些:

how do I pass a variable name to an argument in a function

Passing a variable name to a function in R

我一直在尝试使用不同的功能组合: 替换(),评估(),引用() 但没有太大的成功;

我知道这个主题已经在这里讨论过,但到目前为止我找不到合适的解决方案;

我的目标是在函数参数中传递变量的名称,以便在随机森林模型提供的回归(和预测)中进行评估

谢谢

【问题讨论】:

    标签: r function non-standard-evaluation


    【解决方案1】:

    尝试使用来自 rlangensym()inject()

    forest_ci <- function(B, train_df, test_df, var_rf){
      
      y = rlang::ensym(var_rf)
      
      # Initialize a matrix to store the predicted values
      predictions <- matrix(nrow = B, ncol = nrow(test_df))
      
      # bootstrapping predictions
      for (b in 1:B) {
        
        # Fit a random forest model
        model <- rlang::inject(randomForest::randomForest(!!y~., data = train_df)) # not working
        #model <- randomForest::randomForest(no2~., data = train_df)   # working
        
        # Store the predicted values from the resampled model
        predictions[b, ] <- predict(model, newdata = test_df)
        
      }
      
      predictions
      
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 2018-11-05
      • 2020-09-21
      • 2020-04-12
      • 1970-01-01
      • 2022-01-11
      • 2020-04-02
      • 2019-01-31
      相关资源
      最近更新 更多