二元运算符的随机森林错误非数字参数
Random Forest error non-numeric argument to binary operator
我正在学习 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
Error in y - ymean : non-numeric argument to binary operator In
addition: Warning messages: 1: In randomForest.default(m, y, ...) :
The response has five or fewer unique values. Are you sure you want
to do regression? 2: In mean.default(y) : argument is not numeric or
logical: returning 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)
我正在学习 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
Error in y - ymean : non-numeric argument to binary operator In addition: Warning messages: 1: In randomForest.default(m, y, ...) :
The response has five or fewer unique values. Are you sure you want to do regression? 2: In mean.default(y) : argument is not numeric or logical: returning 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)