Skip to content

Commit

Permalink
Merge pull request #48 from philsf/release-0.6.2
Browse files Browse the repository at this point in the history
Release 0.6.2
  • Loading branch information
philsf authored Oct 16, 2018
2 parents c8f3a7c + cc96fc0 commit 23b7e34
Show file tree
Hide file tree
Showing 30 changed files with 404 additions and 480 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Package: philsfmisc
Type: Package
Title: philsf's miscellaneous R functions
Version: 0.6.1
Version: 0.6.2
Authors@R: person("Felipe", "Figueiredo", email = "[email protected]", role = c("aut", "cre"))
Description: Miscellaneous R functions for convenient data analyses.
License: file LICENSE
Encoding: UTF-8
LazyData: true
URL: https://github.com/philsf/philsfmisc
Depends: R (>= 3.3.0)
RoxygenNote: 6.1.0
18 changes: 16 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
importFrom("stats", "qt", "sd")
exportPattern("^[[:alpha:]]+")
# Generated by roxygen2: do not edit by hand

export(cv2logsd)
export(cv2logvar)
export(format.float)
export(format.interval)
export(format.pct)
export(geocv)
export(geomean)
export(geosd)
export(is.within)
export(logsd2cv)
export(logvar2cv)
export(predint)
importFrom(stats,qt)
importFrom(stats,sd)
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# philsfmisc 0.6.2

- Now uses `roxygen2` for function documentation creation ( #37 )
- Styles updated for all templates ( #45 )

# philsfmisc 0.6.1

- Styles updated #41
Expand Down
15 changes: 15 additions & 0 deletions R/cv2logsd.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#' Convert the CV from a log-normal variable to the log-transformed standard deviation.
#'
#' @description
#'
#' @param x A numeric value, vector or list.
#'
#' @return Returns log-transformed sd
#'
#' @examples
#' cv2logsd(50)
#' # [1] 0.2051523
#'
#' @author Felipe Figueiredo
#' @export

cv2logsd <- function(x) {
SD <- sqrt( cv2logvar(x) )
SD
Expand Down
15 changes: 15 additions & 0 deletions R/cv2logvar.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#' Convert the CV from a log-normal variable to the log-transformed variance.
#'
#' @description
#'
#' @param x A numeric value, vector or list.
#'
#' @return Returns the log-transformed variance.
#'
#' @examples
#' cv2logvar(50)
#' # [1] 0.04208748
#'
#' @author Felipe Figueiredo
#' @export

cv2logvar <- function( x ) {
VAR <- (log((x/100)^2 + 1))/(log(10)^2)
VAR
Expand Down
28 changes: 28 additions & 0 deletions R/format.float.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
#' Format numbers for printing
#'
#' @description Format numbers, given a `digits` argument as significant decimal places.
#' Numbers are always assumed to be \code{\link[base]{double}}.
#'
#' @param x Value to be formatted.
#' Input can be a numeric or character value.
#'
#' @param digits Number of fixed digits, after decimal point
#'
#' @return Returns a character value.
#'
#' @examples
#' format.float(0.5)
#' format.float(0.672, 4)
#' format.float(1.4927, 1)
#' format.float(c(1.756, 1.823))
#' # Input is character
#' format.float("1.12543")
#' format.float(c("1.756", "1.823"))
#'
#' # Useful for data frames
#' DF <- data.frame(num = c(1.756, 1.823), char = c("1.756", "1.823"), stringsAsFactors = FALSE)
#' transform(DF, num = format.float(num), char = format.float(char))
#'
#' @author Felipe Figueiredo
#' @export format.float

format.float <- function(x, digits = 2) {
x <- suppressWarnings(as.numeric(x))
x <- formatC(x, format = "f", digits = digits)
Expand Down
21 changes: 21 additions & 0 deletions R/format.interval.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
#' Format intervals for printing
#'
#' @description Format intervals, given a `digits` argument as significant decimal places.
#'
#' @param interval Must be a vector of two values.
#' Values can be either numeric or character.
#'
#' @param digits Number of fixed digits, after decimal point.
#'
#' @return Returns a character value.
#'
#' @examples
#' format.interval(c(1.756, 1.823))
#' # format.interval(1.5, 1) # error: 1.5 is not an interval
#'
#' # Input is character
#' format.interval(c("1.756", "1.823"))
#'
#' @author Felipe Figueiredo
#' @export format.interval

format.interval <- function(interval, digits = 2) {
if (is.null(interval)) return(NULL) # if no argument passed, return
if (anyNA(interval)) return(NA) # return NA in case of incomplete interval
Expand Down
22 changes: 22 additions & 0 deletions R/format.pct.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
#' Convenience function to format proportions as percentages
#'
#' @description
#'
#' @param p a numeric vector assumed to be a proportion.
#' @param digits the number of decimal digits to be used in the output.
#' @param pct.symbol (logical) whether or not to include the percent sign in the output.
#'
#' @return A character value formatted as percentages
#'
#' @examples
#' format.pct(.1)
#' format.pct(c(.42, .99), 0)
#'
#' m <- 10*matrix(c(1, 1.1, .9, 2), nrow = 2)
#' format.pct(fisher.test(m)$p.value)
#' format.pct(fisher.test(5*m)$p.value)
#' format.pct(fisher.test(m)$conf.int)
#'
#' @author Felipe Figueiredo
#' @export format.pct

format.pct <- function(p, digits = 1, pct.symbol = FALSE) {
symbol <- "%"
if (!pct.symbol) symbol <- NULL
Expand Down
18 changes: 18 additions & 0 deletions R/geocv.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
#' Compute the CV of log-normally distributed data.
#'
#' @description
#'
#' @param x A numeric value, vector or list.
#' @param na.rm (logical) whether or not to remove \code{NA} values from computation
#'
#' @return Returns the geometric CV (as percentage)
#'
#' @examples
#' d <- c(10,100,1000)
#' geocv(d)
#' geocv(d) == logsd2cv(1)
#'
#' @author Felipe Figueiredo
#' @export
#' @importFrom stats sd

geocv <- function(x, na.rm = TRUE) {
logsd <- sd(log10(x), na.rm = na.rm)
logsd2cv(logsd)
Expand Down
18 changes: 18 additions & 0 deletions R/geomean.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
#' Compute the geometric mean
#'
#' @description
#'
#' @param x A numeric value, vector or list.
#'
#' @details If any null values are present in \code{x} they are first replaced with \code{NA}.
#'
#' @return Returns the geometric mean of \code{x}.
#'
#' @examples
#' d <- c(10,100,1000)
#' geomean(d)
#' geomean(c(d,0)) # na.rm is TRUE by default for sd()
#'
#' @author Felipe Figueiredo
#' @export

geomean <- function(x) {
x[(x == 0)] <- NA # discard null values
10^mean(log10(x), na.rm = T)
Expand Down
18 changes: 18 additions & 0 deletions R/geosd.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
#' Compute the "geometric standard deviation" of data
#'
#' @description The "geometric standard deviation" is not as well-defined as the geometric mean.
#' This function returns the analogous concept of \code{sd} in the same manner of the geometric mean.
#'
#' @param x A numeric value, vector or list.
#'
#' @return The anti-log of the \code{sd} of log-transformed data `x`.
#' Base 10 is assumed as a fixed default in this version.
#'
#' @examples
#' d <- c(10,100,1000)
#' geosd(d)
#'
#' @author Felipe Figueiredo
#' @export
#' @importFrom stats sd

geosd <- function(x) {
10^sd(log10(x))
}
21 changes: 21 additions & 0 deletions R/is.within.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
#' Check if number is within given interval
#'
#' @description
#'
#' @param value A numeric value, vector or list.
#' @param interval The (numeric) interval within which \code{value} is being tested.
#' @param open.interval logical: should \code{interval} be regarded as an open interval?
#'
#' @return The default method for \code{is.within} applied to an atomic vector returns
#' a logical vector of the same length as its argument x, containing TRUE for those elements contained within \code{interval}, and FALSE otherwise.
#'
#' @examples
#' is.within(1.5, c(0,5)) # TRUE
#' is.within(6, c(0,5)) # FALSE
#'
#' is.within(c(1.5, 6), c(0,5)) # vector: TRUE, TRUE
#' is.within(list(1.5, 6), c(0,5)) # list: TRUE, FALSE
#'
#' @author Felipe Figueiredo
#' @export is.within

is.within <- function(value, interval, open.interval = FALSE) {
if (open.interval) return( value > interval[1] & value < interval[2] )
value >= interval[1] & value <= interval[2]
Expand Down
16 changes: 16 additions & 0 deletions R/logsd2cv.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#' Convert the log-transformed standard deviation from a log-normal variable to the CV.
#'
#' @description
#'
#' @param x A numeric value, vector or list.
#'
#' @return Returns the geometric CV (as percentage)
#'
#' @examples
#' d <- c(10,100,1000)
#' sd(log10(d)) # 1
#' logsd2cv(1) # 1413.21\%
#'
#' @author Felipe Figueiredo
#' @export

logsd2cv <- function(x) {
sqrt(exp((log(10)^2)*x^2) - 1)*100
}
16 changes: 16 additions & 0 deletions R/logvar2cv.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#' Convert the log-transformed variance from a log-normal variable to the CV.
#'
#' @description
#'
#' @param x A numeric value, vector or list.
#'
#' @return Returns the geometric CV (as percentage)
#'
#' @examples
#' d <- c(10,100,1000)
#' var(log10(d)) # 1
#' logvar2cv(1) # 1413.21\%
#'
#' @author Felipe Figueiredo
#' @export

logvar2cv <- function(x) {
sqrt(exp((log(10)^2)*x) - 1)*100
}
20 changes: 20 additions & 0 deletions R/predint.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
#' Prediction Intervals for given data
#'
#' @description Compute a prediction interval of a vector, which is assumed
#' normally distributed with unknown mean and variance.
#'
#' @param datavector A numeric value, vector or list.
#' @param alpha the significance level required
#'
#' @return a vector of two values
#' A vector with values giving lower and upper prediction limits for each parameter.
#'
#' @examples
#' x <- rnorm(30)
#' predint(x)
#'
#' @author Felipe Figueiredo
#' @export
#' @importFrom stats sd
#' @importFrom stats qt

predint <- function(datavector, alpha = 0.05) {
datavector <- datavector[!is.na(datavector)] # discard NA values
N <- length(datavector)
Expand Down
Binary file modified inst/rmarkdown/templates/Relatorio/skeleton/misc/style.docx
Binary file not shown.
5 changes: 2 additions & 3 deletions inst/rmarkdown/templates/Relatorio/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ output:
fig_caption: yes
fig_height: 6
fig_width: 6
keep_md: yes
number_sections: yes
toc: yes
pdf_document:
Expand All @@ -18,7 +19,7 @@ output:
fig_width: 6
reference_docx: misc/style.docx
toc: yes
subtitle: 'CÓDIGO: analise_dados_XX_aaaa'
subtitle: 'RELATÓRIO: analise_dados_XX_aaaa'
toc-title: "Sumário"
---

Expand All @@ -29,8 +30,6 @@ options(scipen = 999)
library(pander)
library(knitr)
library(philsfmisc)
```

---
Expand Down
Loading

0 comments on commit 23b7e34

Please sign in to comment.