简体   繁体   English

R eval(predvars, data, env) object 未通过在 function 中传递参数找到

[英]R eval(predvars, data, env) object not found by passing a pameter in a function

My reproducible example is as follows;我的可重现示例如下;

please do not bother at all the underlying meaning of the calculations (none, actually) because it is just an extract of my real dataset;请不要理会计算的潜在含义(实际上没有),因为它只是我真实数据集的摘录;

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)

I've got the following error message:我收到以下错误消息:

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

I think understanding the error has somehow to do with the concept of "non-standard evaluation" and the "capturing expressions"我认为理解错误与“非标准评估”和“捕获表达式”的概念有某种关系

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

Following the suggestion of some threads, here follows some of them:根据一些线程的建议,这里遵循其中的一些:

how do I pass a variable name to an argument in a function 如何将变量名传递给 function 中的参数

Passing a variable name to a function in R 将变量名传递给 R 中的 function

I've been trying the use of different combinations of the functions: substitute(), eval(), quote() but without much success;我一直在尝试使用函数的不同组合:substitute()、eval()、quote() 但没有取得多大成功;

I know the subject has already been covered here but I could not find a proper solution so far;我知道这个主题已经在这里讨论过,但到目前为止我找不到合适的解决方案;

my objective is to pass the name of a variable inside a function argument to be evaluated inside the regression (and prediction) provided by the Random Forest model我的目标是在 function 参数中传递一个变量的名称,以便在随机森林 model 提供的回归(和预测)中进行评估

Thanks谢谢

Try using ensym() and inject() from rlang :尝试使用 rlang 中的rlang ensym()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
  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:eval(predvars,data,env)中的错误:找不到对象&#39;x&#39; - R: Error in eval(predvars, data, env) : object 'x' not found eval(predvars,data,env)中的错误:找不到对象 - Error in eval(predvars, data, env) : object not found eval 中的错误(predvars、data、env):object 'Example' not found in RandomForest function - Error in eval(predvars, data, env) : object 'Example' not found in RandomForest function object '...' not found in R Functions with lm --&gt;&gt; (Error in eval(predvars, data, env): object '...' not found) - object '…' not found in R Functions with lm -->> (Error in eval(predvars, data, env) : object '…' not found) R中的非线性最小二乘:eval(predvars,data,env)中的错误:找不到对象 - Non-linear least squares in R: Error in eval(predvars, data, env) : object not found eval(predvars, data, env) 中的错误:找不到对象“下水道” - Error in eval(predvars, data, env) : object 'Sewer' not found R 中的奇怪分类和 eval 中的错误(predvars,data,env):object [...] 未找到错误(没有错字!) - Strange classification in R and Error in eval(predvars, data, env) : object […] not found error (no typo!) 回归:评估错误(predvars,数据,env):未找到 object 'volt' - regression: Error in eval(predvars, data, env) : object 'volt' not found predict.tree function 将其包装到 ZC1C425268E68385D1AB5074C17A94F14 - predict.tree function throws “eval(predvars, data, env) : object 'y' not found” when wrapping it into a function 在逻辑回归中使用预测函数时遇到“ eval(predvars,data,env)中的错误:找不到对象&#39;Age&#39;” - Running into 'Error in eval(predvars, data, env) : object 'Age' not found' while using predict function in logistic regression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM