r - 二元运算符的随机森林错误非数字参数
问题描述
我在 R studio 中学习教程,我无法在 https://github.com/dataprofessor/rshiny_freecodecamp/blob/main/3-play-golf/app.R编译代码
以下代码行不起作用,我不明白为什么。请帮忙。
model <- randomForest(play ~ ., data = weather, ntree = 500, mtry = 4, importance = TRUE)# Save model to RDS file
y - ymean 中的错误:二元运算符的非数字参数此外:警告消息:1:在 randomForest.default(m, y, ...) 中:
响应具有五个或更少的唯一值。您确定要进行回归吗?2:在 mean.default(y) 中:参数不是数字或逻辑:返回 NA
解决方案
您被 R 4.x0 和 R3.x 处理 data.frames 创建的方式的不同所吸引。在 R4.x 之前以及创建上述示例时,字符串会自动转换为因子。这在 R4.0 中的更改是默认设置是在创建 data.frame 时不将字符串转换为因子。
解决您的代码:设置stringsAsFactors
为TRUE
# Read data
url <- "https://raw.githubusercontent.com/dataprofessor/data/master/weather-weka.csv"
weather <- read.csv(file = url, stringsAsFactors = TRUE)
# Build model
model <- randomForest(play ~ ., data = weather, ntree = 500, mtry = 4, importance = TRUE)
或者自己创建一个因子:
# Read data
url <- "https://raw.githubusercontent.com/dataprofessor/data/master/weather-weka.csv"
weather <- read.csv(file = url)
weather$play <- as.factor(weather$play)
# Build model
model <- randomForest(play ~ ., data = weather, ntree = 500, mtry = 4, importance = TRUE)
推荐阅读
- javascript - 如何使用 React Navigation Drawer 切换屏幕
- python - 我想以表格格式显示变量,这些变量应该在 python 中完全对齐
- jekyll - 无法在本地运行 github 页面
- java - 我的计算器应用程序不适用于括号?
- azure - 使用本地 Cosmos DB 模拟器调试 Azure Functions
- informix - 如何列出informix数据库中存在的所有角色?
- c++ - 为什么不在 stl 的迭代器中传递星号(*)
- javascript - 查找奇偶校验异常值 Javascript
- bash - 基于 CTRL 的键绑定,如 `CX` 或 `Cn` 是否总是不区分大小写?
- asp.net-mvc - 当我包含默认值时,为什么 Bootstrap Select 不显示所选值?