dbW_imputeWeather: Impute missing weather values

View source: R/swWeatherGenerator.R

dbW_imputeWeatherR Documentation

Impute missing weather values

Description

Impute missing weather values first by the weather generator (for supported variables, see weatherGenerator_dataColumns()) and second, if any missing values remain, using one of the imputation types provided by rSW2utils::impute_df() for each variable separately.

Usage

dbW_imputeWeather(
  weatherData,
  use_wg = TRUE,
  seed = NULL,
  method_after_wg = c("interp", "locf", "mean", "none", "fail"),
  nmax_run = Inf,
  return_weatherDF = FALSE
)

Arguments

weatherData

A list of elements of class swWeatherData or a data frame as returned by dbW_weatherData_to_dataframe().

use_wg

A logical value. Should the weather generator be used first?

seed

An integer value or NULL. See base::set.seed().

method_after_wg

A string. The imputation type passed to rSW2utils::impute_df(), if any missing values remain after the weather generator.

nmax_run

An integer value. Runs (sets of consecutive missing values) that are equal or shorter to nmax_run are imputed; longer runs remain unchanged. Passed to rSW2utils::impute_df().

return_weatherDF

A logical value. See section "Value".

Value

An updated copy of weatherData where missing values are imputed. If return_weatherDF is TRUE, then a data frame where columns represent weather variables is returned. If return_weatherDF is FALSE, then the result is converted to a a list of elements of class swWeatherData.

Details

The weather generator (see dbW_generateWeather()) produces new values for all implemented variables weatherGenerator_dataColumns() on days where at least one of these variables is missing; this is to maintain physical consistency among those variables. This differs from the approach employed by method_after_wg which imputes missing variables for each variable separately (see rSW2utils::impute_df()).

See Also

rSW2utils::impute_df(), dbW_generateWeather()

Examples

# Load example data
path_demo <- system.file("extdata", "example1", package = "rSOILWAT2")
dif <- c(rep(TRUE, 3L), rep(FALSE, 11L))
dif[13L] <- TRUE # ACTUAL_VP
dif[14L] <- TRUE # SHORT_WR, desc_rsds = 2
wdata <- getWeatherData_folders(
  LookupWeatherFolder = file.path(path_demo, "Input"),
  weatherDirName = "data_weather_daymet",
  filebasename = "weath",
  startYear = 1980,
  endYear = 1981,
  dailyInputFlags = dif,
  method = "C"
)
x0 <- x <- dbW_weatherData_to_dataframe(wdata)
dif0 <- calc_dailyInputFlags(x0)

# Set June-August of 1980 as missing
ids_missing <- x[, "Year"] == 1980 & x[, "DOY"] >= 153 & x[, "DOY"] <= 244
x[ids_missing, -(1:2)] <- NA

x1 <- dbW_imputeWeather(x, return_weatherDF = TRUE)
x2 <- dbW_imputeWeather(x, method_after_wg = "none", return_weatherDF = TRUE)
x3 <- dbW_imputeWeather(
  x,
  use_wg = FALSE,
  method_after_wg = "locf",
  return_weatherDF = TRUE
)

if (requireNamespace("graphics")) {
  ## Compare original vs imputed values for May-Sep of 1980
  ip <- x[, "Year"] == 1980 & x[, "DOY"] >= 123 & x[, "DOY"] <= 274
  doy <- x[ip, "DOY"]

  vars <- names(dif0)[dif0]
  nr <- ceiling(sqrt(length(vars)))

  par_prev <- graphics::par(mfrow = c(nr, ceiling(length(vars) / nr)))

  for (k in seq_along(vars)) {
    graphics::plot(doy, x0[ip, vars[[k]]], ylab = vars[[k]], type = "l")
    graphics::lines(doy, x1[ip, vars[[k]]], col = "red", lty = 2L)
    graphics::lines(doy, x2[ip, vars[[k]]], col = "purple", lty = 3L)
    graphics::lines(doy, x3[ip, vars[[k]]], col = "darkgreen", lty = 3L)
    graphics::lines(doy, x0[ip, vars[[k]]], col = "gray")
  }

  graphics::par(par_prev)
}


DrylandEcology/rSOILWAT2 documentation built on Jan. 12, 2024, 9:06 p.m.