From 7cd25960683e6d4bba28b667a6153ec6c67db53a Mon Sep 17 00:00:00 2001 From: saskiaotto <saskia.otto@uni-hamburg.de> Date: Sun, 21 Feb 2021 10:00:28 +0100 Subject: [PATCH] fixed issue with global environments --- NAMESPACE | 5 +++++ R/calc_vif.R | 6 +++--- R/explore_na.R | 9 ++++---- man/calc_vif.Rd | 29 +++++++++++++++++++++++++ man/explore_na.Rd | 52 +++++++++++++++++++++++++++++++++++++++++++++ man/impute.Rd | 30 ++++++++++++++++++++++++++ man/test_tac.Rd | 28 ++++++++++++++++++++++++ man/trafficlight.Rd | 2 +- 8 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 man/calc_vif.Rd create mode 100644 man/explore_na.Rd create mode 100644 man/impute.Rd create mode 100644 man/test_tac.Rd diff --git a/NAMESPACE b/NAMESPACE index b8ed91e..8f96663 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,10 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(calc_vif) +export(explore_na) +export(impute) +export(test_tac) export(trafficlight) +import(graphics) importFrom(magrittr,"%>%") diff --git a/R/calc_vif.R b/R/calc_vif.R index 05f4cea..0aeb589 100644 --- a/R/calc_vif.R +++ b/R/calc_vif.R @@ -26,9 +26,9 @@ calc_vif <- function(x) { for (v in 1:ncol(x)) { v.name <- colnames(x)[v] other.v.names <- colnames(x)[-v] - mod.formula <- as.formula(paste(v.name, "~", paste(other.v.names, collapse = "+"))) - mod <- lm(mod.formula, data = x) - R2 <- summary(mod) $ r.squared + mod.formula <- stats::as.formula(paste(v.name, "~", paste(other.v.names, collapse = "+"))) + mod <- stats::lm(mod.formula, data = x) + R2 <- summary(mod)$r.squared result[v, "Rsquared"] <- R2 result[v, "Tolerance"] <- 1 - R2 result[v, "VIF"] <- 1 / (1 - R2) diff --git a/R/explore_na.R b/R/explore_na.R index 75dcc9f..570bf87 100644 --- a/R/explore_na.R +++ b/R/explore_na.R @@ -18,6 +18,7 @@ #' @param respect logical; this argument controls whether a unit column-width is the #' same physical measurement on the device as a unit row-height (default is TRUE). #' +#' @import graphics #' @export #' @author Saskia A. Otto #' @examples @@ -26,10 +27,10 @@ #' colnames(mat) <- paste0("variable", 1:10) #' explore_na(x = mat, time = 1981:2000) #' colnames(mat) <- LETTERS[1:10] -#' explore_na(x = mat, time = 1981:2000, madj_y = -1.5, hadj_y = 2, respect = FALSE) +#' explore_na(x = mat, time = 1981:2000, madj_y = -2.5, hadj_y = 2, respect = FALSE) explore_na <- function(x, time = NULL, cex_x = 1, cex_y = 1, - madj_y = 0, hadj_y = 1, respect = TRUE,...){ + madj_y = 0, hadj_y = 1, respect = TRUE){ if(!is.null(time)) { rownames(x) <- time @@ -48,7 +49,7 @@ explore_na <- function(x, time = NULL, cex_x = 1, cex_y = 1, x <- x[reverse,] # Data Map - layout(matrix(c(1,1,2,1,1,2,3,3,4), ncol=3, byrow = TRUE), widths = c(2,2,1.5), + layout(matrix(c(1,1,2,1,1,2,3,3,4), ncol=3, byrow = TRUE), widths = c(2.5,2,1.5), heights = c(2,1,1.5), respect = respect) # graphics::layout(matrix(c(1,1,1,1), ncol = 2), widths = c(3.5,3.5), heights = c(2,2), # respect = respect) @@ -68,7 +69,7 @@ explore_na <- function(x, time = NULL, cex_x = 1, cex_y = 1, title(xlab="Number of years", line=2, cex.lab=1.2) #barplot stations - par(mar = c(4, 5+madj_y, 1.5, .5)) + par(mar = c(4, 7+madj_y, 1.5, .5)) barplot(apply(x>0, 2, sum), ylab = "", las = 2, cex.axis = cex_y, space = 0, col = "#db7170", xaxs = "i") title(ylab="Number of variables", line=2.5, cex.lab=1.2) diff --git a/man/calc_vif.Rd b/man/calc_vif.Rd new file mode 100644 index 0000000..99c504d --- /dev/null +++ b/man/calc_vif.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calc_vif.R +\name{calc_vif} +\alias{calc_vif} +\title{Calculate the variance inflation factors} +\usage{ +calc_vif(x) +} +\arguments{ +\item{x}{matrix or data frame containing only numerical variables.} +} +\value{ +The function returns a matrix with 3 columns: +\describe{ + \item{\code{Rsquared}}{} + \item{\code{Tolerance}}{} + \item{\code{VIF}}{} + } +} +\description{ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy +eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam +voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet +clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. +} +\examples{ +x <- data.frame(a = 1:20, b = 1:20*2 + rnorm(20) ) +calc_vif(x) +} diff --git a/man/explore_na.Rd b/man/explore_na.Rd new file mode 100644 index 0000000..6c395de --- /dev/null +++ b/man/explore_na.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/explore_na.R +\name{explore_na} +\alias{explore_na} +\title{Create multiple plots to explore gaps in the time series} +\usage{ +explore_na( + x, + time = NULL, + cex_x = 1, + cex_y = 1, + madj_y = 0, + hadj_y = 1, + respect = TRUE +) +} +\arguments{ +\item{x}{matrix or data frame containing time series of multiple variables.} + +\item{time}{vector of time units that will be used for the x ax} + +\item{cex_x}{double; the magnification to be used for the x labels relative to the +current setting of cex.} + +\item{cex_y}{double; the magnification to be used for the y labels relative to the +current setting of cex.} + +\item{madj_y}{double; adjustment of left margin relative to the +current setting for longer or short variable names in the image plot and right barplot.} + +\item{hadj_y}{double; horizontal adjustment of the y labels in the image plot.} + +\item{respect}{logical; this argument controls whether a unit column-width is the +same physical measurement on the device as a unit row-height (default is TRUE).} +} +\description{ +This function creates an image plot where available values for the different variables and years +are indicated in gray and missing values in white. At the right side and bottom, two barplots +are added showing the frequency of available variables per year and the available years per +variable. +} +\examples{ +mat <- matrix(1:200, ncol = 10) +mat[sample(1:200, sample(10:80, 1))] <- NA +colnames(mat) <- paste0("variable", 1:10) +explore_na(x = mat, time = 1981:2000) +colnames(mat) <- LETTERS[1:10] +explore_na(x = mat, time = 1981:2000, madj_y = -2.5, hadj_y = 2, respect = FALSE) +} +\author{ +Saskia A. Otto +} diff --git a/man/impute.Rd b/man/impute.Rd new file mode 100644 index 0000000..ed98d9b --- /dev/null +++ b/man/impute.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/impute.R +\name{impute} +\alias{impute} +\title{Imputation of missing values by local mean substitution} +\usage{ +impute(x, n = 2) +} +\arguments{ +\item{x}{numeric vector containing NAs.} + +\item{n}{integer; the number of previous and following values to be included in the mean. +The default is 2 so the mean is based on a 5yr-period (including the year of the missing +value).} +} +\value{ +The function returns the same numeric input vector, but with replaced missing values. +} +\description{ +The function substitutes missing values in time series with a mean of the \emph{n} previous and following years +(or less depending on the position of NAs, whether it is at the beginning or end of the time series, and +the presence of further NAs in the selected time period). +} +\examples{ +x <- c(NA, 1, 4, 2, 5, NA, 9, NA, 12, 11, NA) +impute(x) +} +\author{ +Saskia A. Otto +} diff --git a/man/test_tac.Rd b/man/test_tac.Rd new file mode 100644 index 0000000..eedc42c --- /dev/null +++ b/man/test_tac.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/test_tac.R +\name{test_tac} +\alias{test_tac} +\title{Compute (partial) autocorrelation functions and test for significance} +\usage{ +test_tac(x) +} +\arguments{ +\item{x}{numeric; an evenly spaced time vector which should be tested for temporal +autocorrelation. Temporal gaps should be included as NAs.} +} +\value{ +The function returns a list with 2 components: +\describe{ + \item{\code{tac}}{TRUE if any lag in the (partial) autocorrelation functions + is significantly correlated (if correlation value > 0.4), else FALSE.} + \item{\code{max_lag}}{The maximal lag that is correlated.} + } +} +\description{ +Compute (partial) autocorrelation functions and test for significance +} +\examples{ +test_tac(x = 1:20) +test_tac(x = rnorm(20)) + +} diff --git a/man/trafficlight.Rd b/man/trafficlight.Rd index 7c1be8e..29d9f16 100644 --- a/man/trafficlight.Rd +++ b/man/trafficlight.Rd @@ -63,7 +63,7 @@ numeric vector of probabilities with values in [0,1]. (Values up to \item{quantile_type}{an integer between 1 and 9 selecting one of the nine quantile algorithms detailed below to be used. Default is 7 (see also \code{\link{quantile}}).} -\item{intervals}{logical; number of evenly-spaced intervals. Default is 5.} +\item{intervals}{logical; number of evenly spaced intervals. Default is 5.} \item{cols}{a character vector with colors for each quantile or interval.} -- GitLab