返回列表 发新帖

R语言求救

[复制链接]

0

好友

1

主题

0

精华

注册会员

积分
352
学分
100
发表于 2018-10-21 19:36:05 | 显示全部楼层 | 阅读模式
请大神查阅帮忙解决以下:
Data <- read.csv("E:/R/model.csv")[,3:6]  # 读入数据
colnames(Data) <- c("increase", "loss", "warning", "is_steal")
Data[, "is_steal"] <- as.factor(Data[, "is_steal"])  # 将目标变量转换成因子型
set.seed(1234)  # 设置随机种子

# 数据集随机抽70%定义为训练数据集,30%为测试数据集
ind <- sample(2, nrow(Data), replace = TRUE, prob = c(0.7, 0.3))
traindata <- Data[ind == 1, ]
testdata <- Data[ind == 2, ]

# 建立决策树模型预测客户是否窃漏电
library(tree)  # 加载决策树的包
tree.model <- tree(is_steal ~ ., data = traindata)  # 建立CART决策树模型
plot(tree.model, type = "uniform")  # 输出决策树图
text(tree.model)

# 预测结果
train_predict <- predict(tree.model,newdata = traindata,type = "class")  # 训练数据集
test_predict <- predict(tree.model, newdata = testdata, type = "class")  # 测试数据集

# 输出训练数据的分类结果
train_predictdata <- cbind(traindata, predictedclass = train_predict)
# 输出训练数据的混淆矩阵
(train_confusion <- table(actual = traindata$is_steal, predictedclass = train_predict))
# 输出测试数据的分类结果
test_predictdata <- cbind(testdata, predictedclass = test_predict)
# 输出测试数据的混淆矩阵
(test_confusion <- table(actual = testdata$is_steal, predictedclass = test_predict))
library(ROCR)
library(gplots)

# 预测结果
train_predict <- predict(tree.model, newdata = traindata)  # 训练数据集
test_predict <- predict(tree.model, newdata = testdata)  # 测试数据集
par(mfrow = c(1, 2))
# ROC曲线
# 训练集
predi <- prediction(train_predict$posterior[, 2], traindata$is_steal)
perfor <- performance(predi, "tpr", "fpr")
plot(perfor, col = "red", type = "l", main = "ROC曲线", lty = 1)  # 训练集的ROC曲线
# 测试集
predi2 <- prediction(test_predict$posterior[, 2], testdata$is_steal)
perfor2 <- performance(predi2, "tpr", "fpr")
par(new = T)
plot(perfor2, col = "blue", type = "l", pch = 2, lty = 2)  # 测试集的ROC曲线
abline(0, 1)
legend("bottomright", legend = c("训练集", "测试集"), bty = "n",
       lty = c(1, 2), col = c("red", "blue"))  # 图例


上面这段程序运行后报以下错误,开始第一个提示错误是关于$符号的使用。关于$符号使用数据类型,做过修改,将数据类型转化成list,但是还是同样的错误。
# ROC曲线
> # 训练集
> predi <- prediction(train_predict$posterior[, 2], traindata$is_steal)
Error in train_predict$posterior :
  $ operator is invalid for atomic vectors
> perfor <- performance(predi, "tpr", "fpr")
Error in performance(predi, "tpr", "fpr") : object 'predi' not found
> plot(perfor, col = "red", type = "l", main = "ROC曲线", lty = 1)  # 训练集的ROC曲线
Error in plot(perfor, col = "red", type = "l", main = "ROC曲线", lty = 1) :
  object 'perfor' not found
> # 测试集
> predi2 <- prediction(test_predict$posterior[, 2], testdata$is_steal)
Error in test_predict$posterior :
  $ operator is invalid for atomic vectors
> perfor2 <- performance(predi2, "tpr", "fpr")
Error in performance(predi2, "tpr", "fpr") : object 'predi2' not found
> par(new = T)
> plot(perfor2, col = "blue", type = "l", pch = 2, lty = 2)  # 测试集的ROC曲线
Error in plot(perfor2, col = "blue", type = "l", pch = 2, lty = 2) :
  object 'perfor2' not found
> abline(0, 1)
> legend("bottomright", legend = c("训练集", "测试集"), bty = "n",
+        lty = c(1, 2), col = c("red", "blue"))  # 图例


快速回复 返回顶部 返回列表