我写了一个代码来计算观察到的数据和模拟数据之间的RMSE误差。但是我只想在一月份进行。文本文件的数据在第一列中包含日期,在第二列中包含模拟数据,在第三列中包含观察数据。
数据格式如下:
DATE cout rout coub cinf
UNITS m3/s m3/s m3/s m3/s
1981-01-01 292.234 305 0 292.234
1981-01-02 293.152 320 0 293.152
1981-01-03 293.985 324 0 293.985
1981-01-04 295.115 308 0 295.115
1981-01-05 296.579 326 0 296.579
1981-01-06 298.266 344 0 298.266
1981-01-07 300.084 342 0 300.084
1981-01-08 301.945 329 0 301.945
1981-01-09 303.747 357 0 303.747
1981-01-10 305.437 351 0 305.437
1981-01-11 306.967 352 0 306.967
1981-01-12 308.281 382 0 308.28
下面的代码用于计算整个数据集的RMSE,与日期无关:
# Function that returns Root Mean Squared Error
# set the working directory
setwd("D:\\Results\\")
# Get the header 1st line of the data
header <-scan("4001968.txt", nlines=1, what =character())
#Define number of lines to skip, which is 2
y <- read.table("4001968.txt",skip=2,header=F,sep="\t")
# Add the character vector header on as the names component
names(y) <- header
#Function for calculating RMSE
rmse <- function(error)
{
sqrt(mean(error^2))
}
# Convert characater to numeric
y$cout <- as.numeric(as.character(y$cout))
y$rout <- as.numeric(as.character(y$rout))
actual <- y$cout
predicted <- y$rout
# Calculate error
error <- actual - predicted
# Invocation of functions
rmse(error)
输出将仅是一月份的单个值。
答案 0 :(得分:0)
我发现data.table和lubridate软件包对于处理此类问题非常有用:
# libraries
library(data.table)
library(lubridate)
# Function that returns Root Mean Squared Error
# set the working directory
setwd("D:\\Results\\")
# Get the header 1st line of the data
header <-scan("4001968.txt", nlines=1, what =character())
#Define number of lines to skip, which is 2
y <- read.table("4001968.txt",skip=2,header=F,sep="\t")
# Add the character vector header on as the names component
names(y) <- header
#Function for calculating RMSE
rmse <- function(error)
{
sqrt(mean(error^2))
}
# Convert characater to numeric
y$cout <- as.numeric(as.character(y$cout))
y$rout <- as.numeric(as.character(y$rout))
y <- as.data.table(y)
# Calculate error
error <- y[month(DATE)==1, cout-rout]
# Invocation of functions
rmse(error)