代码之家  ›  专栏  ›  技术社区  ›  user6985

每月计算观测和模拟数据之间的RMSE误差

  •  0
  • user6985  · 技术社区  · 5 年前

    我编写了一个代码来计算观测数据和模拟数据之间的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)
    

    产量仅为1月份的单个值。

    1 回复  |  直到 5 年前
        1
  •  0
  •   LocoGris    5 年前

    我发现处理此类问题的包装数据、表格和润滑油非常有用:

    # 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)