如何修复“."在公式中,没有“数据"参数"什么时候使用 randomForest 函数? [英] How to fix "'.' in formula and no 'data' argument" when using randomForest function?

查看:53
本文介绍了如何修复“."在公式中,没有“数据"参数"什么时候使用 randomForest 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用我的数据构建一个随机森林回归模型.对于第一个参数,我编写了公式,然后指定了数据,最后编写了所需树的数量.

I am trying to build a random forest regression model with my data. For the first argument I wrote the formula, then I specified the data and lastly wrote the number of desired trees.

rf_model = randomForest(targetVar ~., data = train, ntree = 50)

我在执行这行代码时得到的错误是:

The Error that I am getting when executing this line of code is:

Error in terms.formula(formula, data = data) : 
'.' in formula and no 'data' argument

是什么导致了错误?它是随机森林回归模型的一个非常简单直接的实现.此外,阅读错误并没有产生任何有效的解决方案.

What is causing the error? it is a very simple and straightforward implementation of the random forest regression model. Also, reading about the error hasn't yield to any valid solution.

推荐答案

问题不在于 randomForest 函数.它与 train 数据集以及您如何指示 randomForest 处理它有关.

The issue is not with the randomForest function. It's with the train dataset and how you instruct randomForest to process it.

  1. 您必须指示 randomForest() 如何处理 NA 值.例如,您必须设置na.action=na.omit.
  2. 您的数据集有太多变量 - randomForest 抱怨 53 个变量太多.
  1. You have to instruct randomForest() to how to handle NA values. For example, you have to set na.action=na.omit.
  2. Your dataset has too many variables - randomForest complains 53 variables is too many.

作为一个例子,我将向您展示如何让它与 2 个变量一起工作.

So as an example, I'll show you how to get it working with a 2 variables.

train <- read.csv('<path to>/Train.csv', header=TRUE, sep = ",")
sales <- randomForest(Item_Outlet_Sales~Item_MRP+Item_Weight, data = train, ntree=50, na.action=na.omit)
sales

na.omit 不是唯一可用的功能.有关更多 NA 处理功能,请参阅文档.

The na.omit is not the only function available. Please see the documentation for more NA handling functions.

您必须查看要使用的变量.希望这会有所帮助.

You have to see what variables you want to use. Hope this helps.

这篇关于如何修复“."在公式中,没有“数据"参数"什么时候使用 randomForest 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆