对于使用 ggplot2 在 R 中绘制两条线:在数据框中,each = length() 函数不起作用并且返回错误的值



我得到了错误:Error in data.frame(iter = seq_along(test_error), Error = c(test_error, : arguments imply differing number of rows: 10000, 20000, 3,但test_error应该大于3,并且each=length(test_eerror(应该将所有行设置为相同的长度,对吗?

代码:这是可复制的代码,这是来自谷歌电子表格的数据集

library(Rcpp)
library(RSNNS)
library(ggplot2)
library(plotROC)
library(tidyr)
setwd("**set working directory**")
data <- read.csv("WDBC.csv", header=T)
data <- data[,1:4]
data <- scale(data)  # normalizes the data
numHneurons3 = 3
DecTargets = decodeClassLabels(data[,4])
train.test3 <- splitForTrainingAndTest(data, DecTargets,ratio = 0.50) # split
model3_02 <- mlp(train.test3$inputsTrain, train.test3$targetsTrain,  # build model3
size = numHneurons3, learnFuncParams = c(0.02),maxit = 10000, 
inputsTest = train.test3$inputsTest, 
targetsTest = train.test3$targetsTest)
trainFitTar3_02 <- cbind(fitted.values(model3_02), train.test3$targetsTrain)
predictions = predict(model3_02, train.test3$inputsTest)
#--------------------------------------
#     GGPlots of the Iterative Error:
#--------------------------------------
str(model3_02)
test_error <- model3_02$IterativeTestError
train_error <- model3_02$IterativeFitError
error_df <- data.frame(iter = seq_along(test_error), Error = c(test_error, train_error), type = rep(c("test", "train", each = length(test_error)))) 
ggplot(error_df, aes(iter, Error, color = type, each = length(test_error))) + geom_line()

这是错误:

> error_df <- data.frame(iter = seq_along(test_error), Error = c(test_error, train_error), type = rep(c("test", "train", each = length(test_error)))) 
Error in data.frame(iter = seq_along(test_error), Error = c(test_error,  : 
arguments imply differing number of rows: 10000, 20000, 3

以下是数据的前十行:

> data <- scale(data)  # normalizes the data
> head(data, 10)
PatientID     radius    texture   perimeter
[1,] -0.2361973  1.0960995 -2.0715123  1.26881726
[2,] -0.2361956  1.8282120 -0.3533215  1.68447255
[3,]  0.4313615  1.5784992  0.4557859  1.56512598
[4,]  0.4317407 -0.7682333  0.2535091 -0.59216612
[5,]  0.4318215  1.7487579 -1.1508038  1.77501133
[6,] -0.2361855 -0.4759559 -0.8346009 -0.38680772
[7,] -0.2361809  1.1698783  0.1605082  1.13712450
[8,]  0.4326197 -0.1184126  0.3581350 -0.07280278
[9,] -0.2361759 -0.3198854  0.5883121 -0.18391855
[10,]  0.4329621 -0.4731182  1.1044669 -0.32919213

两个问题:

  • rep中的括号位于错误的位置
  • c(test_error, train_error)组合了两个矢量,因此长度是seq_along(test_error)的两倍

尝试安全

error_df <- data.frame(iter = c(seq_along(test_error),
seq_along(train_error)),
Error = c(test_error, train_error), 
type = c(rep("test", length(test_error)),
rep("train", length(train_error))
))

最新更新