-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
341 lines (239 loc) · 7.37 KB
/
README.Rmd
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dev = "ragg_png"
)
library(psych)
library(GPArotation)
library(tidyverse)
library(WJSmisc)
```
# WJSmisc
<!-- badges: start -->
<!-- badges: end -->
The WJSmisc package is set of functions I find convenient to have readily available to me.
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("wjschne/WJSmisc")
```
## Plot area under normal curve
I often need to create a normal distribution with a shaded region below a point.
```{r example}
library(WJSmisc)
library(tidyverse)
plotnorm(95, mu = 100, sigma = 15)
```
# Correlation heat maps
```{r, fig.height=6, fig.width=6}
library(simstandard)
model <- "
A =~ 0.71 * A_1 + 0.91 * A_2 + 0.85 * A_3
B =~ 0.65 * B_1 + 0.90 * B_2 + 0.75 * B_3
A ~~ -0.2 * B
"
d <- sim_standardized(
model,
latent = FALSE,
error = FALSE)
cor_heat(d, margins = 0.1)
```
# Parallel Analysis Plot
```{r}
parallel_analysis(d)
```
# Factor Analysis Loading Plot
```{r, fig.height=7}
psych::fa(d, nfactors = 2, fm = "pa") %>%
plot_loading(factor_names = c("A", "B"))
```
# Composite covariance
```{r}
# Create covariance matrix
Sigma <- matrix(0.6, nrow = 5, ncol = 5)
diag(Sigma) <- 1
# Create weight matrix
w <- matrix(0, nrow = 5, ncol = 2)
w[1:2,1] <- 1
w[3:5,2] <- 1
w
# covariance of weighted sums
composite_covariance(Sigma, w)
```
# Correlation Ellipse
```{r}
cor_ellipse(0.75) %>%
ggplot(aes(x,y)) +
geom_polygon(alpha = 0.5) +
coord_fixed()
```
Split at x = 1
```{r}
cor_ellipse(0.75, split_x = 1) %>%
ggplot(aes(x,y)) +
geom_polygon(aes(fill = group), alpha = 0.5) +
coord_fixed()
```
Split at x = 1 and y = 0
```{r}
cor_ellipse(0.75, split_x = 1, split_y = 0) %>%
ggplot(aes(x,y)) +
geom_polygon(aes(fill = group), alpha = 0.5) +
coord_fixed()
```
# Every combination of 2 or more vectors
```{r}
cross_vectors(c("a", "b"),
c("x", "y"),
c(1,2),
sep = "_")
```
# z-score
Like the `scale` function except that it returns a plain vector instead of a matrix with attributes. It can also return z-scores based on a user-specified means and standard deviations.
```{r}
x <- rnorm(100, mean = 100, sd = 15)
# z-score with sample mean and sample sd
x2z(x) %>%
qplot(bins = 10) +
geom_rug()
# z-score with user-specified population mean and sd
x2z(x, mu = 100, sigma = 15) %>%
qplot(bins = 10) +
geom_rug()
# Will center score at sample mean if sigma = 1
x2z(x, sigma = 1) %>%
qplot(bins = 10) +
geom_rug()
```
# Attach function argument defaults to global environment
When debugging a function with many default arguments, it is useful to assign the default values to the variables in the global environment.
```{r}
my_function <- function(x = 1, y = 2) {x + y}
attach_function(my_function)
x
y
```
# Convert an angle to ggplot2 `hjust` and `vjust` parameters
Control placement of labels with the angular position by converting an angle to `hjust` and `vjust` parameters.
```{r}
tibble(degrees = seq(0, 345, 15),
radians = degrees * pi / 180,
x = cos(radians),
y = sin(radians),
hjust = angle2hjust(radians),
vjust = angle2vjust(radians)) %>%
ggplot(aes(x, y)) +
geom_segment(aes(x = 0, y = 0, xend = x, yend = y), size = .1) +
geom_label(aes(label = degrees,
hjust = hjust,
vjust = vjust),
label.padding = unit(1, "mm"),
label.size = 0) +
geom_point() +
coord_fixed(1, clip = "off") +
theme_void()
```
I use these functions to make sure that labels on a curve are perpendicular to the curve:
```{r}
# Small change in x
dx <- .000001
plot_ratio <- 16
tibble(x = seq(-4,4),
y = dnorm(x),
l = WJSmisc::prob_label(pnorm(x), digits = 2),
slope = plot_ratio * (dnorm(x + dx) - y) / dx,
angle = atan(slope) + pi / 2,
hjust = angle2hjust(angle),
vjust = angle2vjust(angle)) %>%
ggplot(aes(x, y, label = l)) +
geom_point() +
stat_function(fun = dnorm) +
geom_label(aes(hjust = hjust,
vjust = vjust),
label.size = 0) +
coord_fixed(plot_ratio, clip = "off") +
theme_minimal()
```
# Lower triangle to correlation matrix
```{r}
tri2cor(c(.2,.3,.4))
tri2cor(.5)
```
# Formatting probability values
Probabilities near 0 and 1 are rounded differently.
```{r}
p <- c(0,.0012, .025, .5, .99, .994, .99952, 1)
prob_label(p, digits = 2)
prob_label(p, accuracy = .01)
proportion_round(p)
proportion2percentile(p, add_percent_character = TRUE)
```
# Sizing text in ggplot2
Text size in geom_text and geom_label does not use the same units as the rest of ggplot2.
I use the `ggtext_size` function so that text from `geom_text` will be the same size as the axis labels.
```{r}
mytextsize <- 24
tibble(x = 1:5, y = x) %>%
ggplot(aes(x, y)) +
geom_text(aes(label = x), size = ggtext_size(mytextsize)) +
theme_gray(base_size = mytextsize) +
coord_equal()
```
# Random beta distributions with specific means and standard deviations.
Sometimes I need random variables with values between 0 and 1. To get a beta distribution that I want, there is less trial-and-error if I specify the mean and standard deviation rather than 2 shape parameters. Note that not all combinations of means and standard deviations are possible.
```{r}
rbeta_ms(10000, .7, .1) %>%
qplot(bins = 30) +
coord_cartesian(xlim = c(0, 1))
```
# Formatting numeric values
R has great formatting functions like `format` and `formatC`. I find `scales::number` to be particularly useful. However, I often have particular preferences that I do not want to keep specifying every time I need to format a number.
## Remove leading zeroes
For numbers between -1 and 1, leading zeroes are removed.
```{r}
remove_leading_zero(c(-2, -0.051, 0.05, 2))
```
## Formatting probabilities
The `prob_label` function formats probabilities according to my preferences:
1. 0 is `0` unless `round_zero_one` is `FALSE`.
2. 1 is `1` unless `round_zero_one` is `FALSE`.
3. Other probabilities are rounded to the nearest .01 with the leading removed.
```{r}
prob_label(seq(0,1,0.2))
```
Setting the `digits` argument to 2 will round to 2 significant digits with the exception that probabilities near 1 are rounded to the first number that is not 9.
```{r}
prob_label(c(.00122, .0122, .122, .99112, .999112), digits = 2)
```
The `proportion_round` rounds .
```{r}
proportion_round(c(0,.0011,.5,.991, .99991, 1))
```
## Formatting percentiles
I like to round percentiles to nearest integer unless they are close to 0 or 100.
```{r}
tibble(z_scores = -4:4,
proportions = pnorm(z_scores),
percentiles = proportion2percentile(proportions,
add_percent_character = TRUE)
)
```
## Formatting correlations
I like to round correlations to the nearest .01 with leading zeroes removed. The diagonals are just 1s.
```{r}
tri2cor(c(.4,.5,.66544)) %>%
cor_text()
```
If any correlation in the matrix is negative, the positive correlations get a leading space (to make the correlations easier to align in a plot or table).
```{r}
tri2cor(c(.4,-.5,.66544)) %>%
cor_text()
```