-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.Rmd
More file actions
153 lines (116 loc) · 4.87 KB
/
Copy pathREADME.Rmd
File metadata and controls
153 lines (116 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.align = 'center',
fig.dim = c(7, 7),
out.width = "50%"
)
library("oeli")
set.seed(1)
```
# Utilities for developing R code <a href="https://loelschlaeger.de/oeli/"><img src="man/figures/logo.png" align="right" height="138" /></a>
<!-- badges: start -->
[](https://CRAN.R-project.org/package=oeli)
[](https://CRAN.R-project.org/package=oeli)
[](https://github.com/loelschlaeger/oeli/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/loelschlaeger/oeli)
<!-- badges: end -->
This [R](https://CRAN.R-project.org) package provides helper functions I found useful when developing R code - perhaps you will too! The released package version can be installed via:
``` r
install.packages("oeli")
```
The following shows some demos. Click the headings for references on all available helpers in each category.
### [Data](https://loelschlaeger.de/oeli/reference/index.html#data)
The `hermann` data contains historical information on editions of the [Hermannslauf](https://de.wikipedia.org/wiki/Hermannslauf), including the date, temperature, and winning times for men and women:
```{r, hermann}
hermann
```
### [Distributions](https://loelschlaeger.de/oeli/reference/index.html#distribution)
The package has density, distribution, and sampling functions for some distributions not included in base R, like the Dirichlet:
```{r, dirichlet}
ddirichlet(x = c(0.2, 0.3, 0.5), concentration = 1:3)
rdirichlet(concentration = 1:3)
```
Or the mixture of Gaussian distributions:
```{r, mixnorm}
x <- c(0, 0)
mean <- matrix(c(1, 1, -1, -1), ncol = 2) # means in columns
Sigma <- matrix(c(diag(2), 0.1 * diag(2)), ncol = 2) # vectorized covariances in columns
proportions <- c(0.7, 0.3)
dmixnorm(x = x, mean = mean, Sigma = Sigma, proportions = proportions)
pmixnorm(x = x, mean = mean, Sigma = Sigma, proportions = proportions)
rmixnorm(n = 1000, mean = mean, Sigma = Sigma, proportions = proportions) |>
as.data.frame() |>
ggplot2::ggplot() + ggplot2::geom_point(ggplot2::aes(x = V1, y = V2))
```
### [Function helpers](https://loelschlaeger.de/oeli/reference/index.html#functional)
Retrieving default arguments of a `function`:
```{r, function_defaults}
f <- function(a, b = 1, c = "", ...) { }
function_defaults(f)
```
### [Indexing helpers](https://loelschlaeger.de/oeli/reference/index.html#indexing)
Create all possible permutations of vector elements:
```{r, permutations}
permutations(LETTERS[1:3])
```
### [Package helpers](https://loelschlaeger.de/oeli/reference/index.html#packaging)
Quickly have a basic logo for your new package:
```{r, package_logo}
logo <- package_logo("my_package", brackets = TRUE)
print(logo)
```
How to print a `matrix` without filling up the entire console?
```{r, print_matrix}
x <- matrix(rnorm(10000), ncol = 100, nrow = 100)
print_matrix(x, rowdots = 4, coldots = 4, digits = 2, label = "what a big matrix")
```
And what about a `data.frame`?
```{r, print_data.frame}
x <- data.frame(x = rnorm(1000), y = LETTERS[1:10])
print_data.frame(x, rows = 7, digits = 0)
```
### [Simulation helpers](https://loelschlaeger.de/oeli/reference/index.html#simulation)
Let's simulate correlated regressor values from different marginal distributions:
```{r, simulate_regressors}
labels <- c("P", "C", "N1", "N2", "U")
n <- 100
marginals <- list(
"P" = list(type = "poisson", lambda = 2),
"C" = list(type = "categorical", p = c(0.3, 0.2, 0.5)),
"N1" = list(type = "normal", mean = -1, sd = 2),
"U" = list(type = "uniform", min = -2, max = -1)
)
correlation <- matrix(
c(1, -0.3, -0.1, 0, 0.5,
-0.3, 1, 0.3, -0.5, -0.7,
-0.1, 0.3, 1, -0.3, -0.3,
0, -0.5, -0.3, 1, 0.1,
0.5, -0.7, -0.3, 0.1, 1),
nrow = 5, ncol = 5
)
data <- correlated_regressors(
labels = labels, n = n, marginals = marginals, correlation = correlation
)
head(data)
cor(data)
```
### [Transformation helpers](https://loelschlaeger.de/oeli/reference/index.html#transformation)
The `group_data.frame()` function groups a given `data.frame` based on the values in a specified column:
```{r, group_data.frame}
df <- data.frame("label" = c("A", "B"), "number" = 1:10)
group_data.frame(df = df, by = "label")
```
### [Validation helpers](https://loelschlaeger.de/oeli/reference/index.html#validation)
Is my matrix a proper transition probability matrix?
```{r, check_transition_probability_matrix}
matrix <- diag(4)
matrix[1, 2] <- 1
check_transition_probability_matrix(matrix)
```