【发布时间】:2021-07-21 01:22:15
【问题描述】:
我试图预测 r 中二手车数据的价格。我已经完成了所有的预处理并将数据分为训练集和测试集。这里我使用回归树。当我试图获得准确性时,我得到了这个错误。
library(rpart)
library(tidyverse)
library(dplyr)
dput(head(train.df, 5))
reg_tree <- rpart(price ~ .,
data = train.df,
method = "anova", minbucket = 1, maxdepth = 30, cp = 0.001)
accuracy(predict(reg_tree, train.df), train.df$price)
structure(list(price = 33990, year = 2018L, manufacturer = structure(1L, .Label = c("acura",
"alfa-romeo", "aston-martin", "audi", "bmw", "buick", "cadillac",
"chevrolet", "chrysler", "datsun", "dodge", "ferrari", "fiat",
"ford", "gmc", "harley-davidson", "honda", "hyundai", "infiniti",
"jaguar", "jeep", "kia", "land rover", "lexus", "lincoln", "mazda",
"mercedes-benz", "mercury", "mini", "mitsubishi", "nissan", "pontiac",
"porsche", "ram", "rover", "saturn", "subaru", "tesla", "toyota",
"volkswagen", "volvo"), class = "factor"), condition = structure(4L, .Label = c("excellent",
"fair", "good", "like new", "new", "salvage"), class = "factor"),
cylinders = structure(6L, .Label = c("10 cylinders", "12 cylinders",
"3 cylinders", "4 cylinders", "5 cylinders", "6 cylinders",
"8 cylinders", "other"), class = "factor"), fuel = structure(3L, .Label = c("diesel",
"electric", "gas", "hybrid", "other"), class = "factor"),
odometer = 22267, title_status = structure(1L, .Label = c("clean",
"lien", "missing", "parts only", "rebuilt", "salvage"), class = "factor"),
transmission = structure(1L, .Label = c("automatic", "manual",
"other"), class = "factor"), drive = structure(2L, .Label = c("4wd",
"fwd", "rwd"), class = "factor"), size = structure(3L, .Label = c("compact",
"full-size", "mid-size", "sub-compact"), class = "factor"),
type = structure(4L, .Label = c("bus", "convertible", "coupe",
"hatchback", "mini-van", "offroad", "other", "pickup", "sedan",
"SUV", "truck", "van", "wagon"), class = "factor"), paint_color = structure(10L, .Label = c("black",
"blue", "brown", "custom", "green", "grey", "orange", "purple",
"red", "silver", "white", "yellow"), class = "factor")), row.names = 31113L, class = "data.frame")
Error in UseMethod("accuracy") :
no applicable method for 'accuracy' applied to an object of class "c('double', 'numeric')"
谁能帮帮我。
提前致谢。
【问题讨论】:
-
您可以发布示例数据吗?请使用
dput(train.df)的输出编辑问题。或者,如果dput(head(train.df, 20))的输出太大。请在脚本开头调用library()加载您正在使用的包。
标签: r regression rpart