-
Notifications
You must be signed in to change notification settings - Fork 7
/
README.Rmd
332 lines (208 loc) · 9.39 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
---
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# priceR <a href='https://github.com/stevecondylios/priceR'><img src='man/figures/priceR.png' align="right" height="139" /></a>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/priceR)](https://cran.r-project.org/package=priceR)
[![R build status](https://github.com/stevecondylios/priceR/workflows/R-CMD-check/badge.svg)](https://github.com/stevecondylios/priceR/actions?workflow=R-CMD-check)
<!-- badges: end -->
`priceR` contains 4 types of capabilties:
- *Exchange Rates* - easily retrieve exchange rates for immediate use
- *Inflation* - easily inflate past (nominal) values into present day (real) prices
- *Regular Expressions* - easily extract common pricing patterns from free text
- *Formatting* - easily handle currencies in written work, including Rmarkdown documents
### Installation
Installation via CRAN `install.packages("priceR")`
```{r message=FALSE}
library(priceR)
library(tidyverse)
options(scipen = 100); options(digits = 6)
```
# Exchange rates
### Setup
Set up only takes a minute and is free for 100 requests per account per calendar month.
Go to https://exchangerate.host/, create a free account, and replace `7e5e3140140bd8e4f4650cc41fc772c0` with your API key in the following, and run once per R session.
```r
Sys.setenv("EXCHANGERATEHOST_ACCESS_KEY"="7e5e3140140bd8e4f4650cc41fc772c0")
```
### Current exchange rates
View the current exchange rates for 170 currencies (see them all by running `currencies()`):
```{r message=FALSE}
exchange_rate_latest("USD") %>%
head(10)
```
### Historical exchange rates
Here's an example of how to get exchange rates for some currency pairs:
```{r message=FALSE}
# Retrieve AUD to USD exchange rates
au <- historical_exchange_rates("AUD", to = "USD",
start_date = "2013-01-01", end_date = "2023-06-30")
# Retrieve AUD to EUR exchange rates
ae <- historical_exchange_rates("AUD", to = "EUR",
start_date = "2013-01-01", end_date = "2023-06-30")
# Combine
cur <- au %>% left_join(ae, by = "date")
head(cur)
```
And to plot the exchange rate data:
```{r message=FALSE}
library(ggplot2)
library(ggthemes)
library(ggrepel)
cur %>%
rename(aud_to_usd = one_AUD_equivalent_to_x_USD,
aud_to_eur = one_AUD_equivalent_to_x_EUR) %>%
pivot_longer(c("aud_to_usd", "aud_to_eur")) %>%
mutate(date = as.Date(date)) %>%
ggplot(aes(x=date, y = value, colour=name)) +
geom_line(size=1) +
scale_color_manual(
breaks = c("aud_to_usd", "aud_to_eur"), # Sets order in legend
labels = c( "AUD to USD", "AUD to EUR"), # Pretty names in legend
values = c("#02506A", "#03A5DC") # Sets line/legend colours
) +
scale_x_date(date_labels = "%b %Y", date_breaks = "6 month") +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, 1.5)
) +
labs(
title = "AUD to USD and EUR 2013 to 2023",
subtitle = "Plotting the Australian Dollar against the USD and Euro",
y = "Exchange Rate"
) +
theme_economist() +
theme(
plot.title = element_text(size = 18, margin=margin(0,0,8,0)),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title.y = element_text(vjust = 3.5),
legend.position="bottom",
legend.title = element_blank()
)
```
```{r message=FALSE}
cur %>%
tail(200) %>%
rename(aud_to_usd = one_AUD_equivalent_to_x_USD,
aud_to_eur = one_AUD_equivalent_to_x_EUR) %>%
mutate(date = as.Date(date)) %>%
ggplot(aes(x = date, y = aud_to_usd, group = 1)) +
geom_line(colour = "#F15B40") +
geom_smooth(method = 'loess', colour="#03A5DC") +
scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
labs(
title = "AUD to USD over last 200 days",
subtitle = "AUD to USD Exchange Rate; Polynomial regression trendline",
y = "Exchange Rate"
) +
theme_economist() +
theme(
plot.title = element_text(size = 18, margin=margin(0,0,8,0)),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title.y = element_text(vjust = 3.5),
legend.position="bottom",
legend.title = element_blank()
)
```
```{r message=FALSE}
cur %>%
tail(365 * 8) %>%
rename(aud_to_usd = one_AUD_equivalent_to_x_USD,
aud_to_eur = one_AUD_equivalent_to_x_EUR) %>%
mutate(date = as.Date(date)) %>%
ggplot(aes(x = date, y = aud_to_eur, group = 1)) +
geom_line() +
geom_smooth(method = 'loess', se = TRUE) +
geom_line(colour = "#02506A") +
geom_smooth(method = 'loess', colour="#03A5DC") +
scale_x_date(date_labels = "%Y", date_breaks = "1 year") +
labs(
title = "AUD to EUR over last 8 years",
subtitle = "AUD to EUR Exchange Rate; Polynomial regression trendline",
y = "Exchange Rate"
) +
theme_economist() +
theme(
plot.title = element_text(size = 18, margin=margin(0,0,8,0)),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title.y = element_text(vjust = 3.5),
legend.position="bottom",
legend.title = element_blank()
)
```
# Inflation
### Adjust prices for inflation
`adjust_for_inflation()` automatically converts between nominal and real dollars,
or in/deflates prices from one year's prices to another's.
It works for 304 countries / areas (see them with all by running `show_countries()`).
```{r message=FALSE}
set.seed(123)
nominal_prices <- rnorm(10, mean=10, sd=3)
years <- round(rnorm(10, mean=2006, sd=5))
df <- data.frame(years, nominal_prices)
df$in_2008_dollars <- adjust_for_inflation(nominal_prices, years, "US", to_date = 2008)
df
```
# Extraction helpers
These helpers let you extract useful *numeric* data from messy free text (character) data.
### Extract salary from free text
`extract_salary()` extracts salaries as useful numeric data from non-standard free text
```{r}
messy_salary_data <- c(
"$90000 - $120000 per annum",
"$90k - $110k p.a.",
"$110k - $120k p.a. + super + bonus + benefits",
"$140K-$160K + Super + Bonus/Equity",
"$200,000 - $250,000 package",
"c$200K Package Neg",
"$700 p/d", # daily
"$120 - $140 (Inc. Super) per hour", # hourly
"Competitive" # nothing useful (will return NA)
)
messy_salary_data %>%
extract_salary(include_periodicity = TRUE,
salary_range_handling = "average")
```
# Formatting helpers
### Neatly format currencies
`format_currency()` makes nicely formats numeric data:
```{r message=FALSE}
format_currency("22500000", "¥")
```
`format_dollars()` is the same but exclusively for dollars:
```{r}
format_dollars(c("445.50", "199.99"), digits = 2)
```
# More about priceR
### Research and academia
Curran-Groome, W., Hino, M., BenDor, T. and Salvesen, D., 2022. [*Complexities and costs of floodplain buyout implementation*](https://www.sciencedirect.com/science/article/abs/pii/S0264837722001557), Land Use Policy, Volume 118, July 2022.
Thomas, C., Shae, W., Koestler, D., DeFor, T., Bahr, N. and Alpern, J., 2022. [*Antifungal drug price increases in the United States 2000–2019*](https://pubmed.ncbi.nlm.nih.gov/35722703/), Mycoses, Online Ahead of Print, June 2022.
Petitbon, A. and Hitchcock, D., 2022. [*What Kind of Music Do You Like? A Statistical Analysis of Music Genre Popularity Over Time*](https://people.stat.sc.edu/hitchcock/jds1040.pdf), Journal of Data Science, Volume 20 (2), April 2022.
Widdicombe, J., Basáñez, M., Entezami, M., Jackson, D., Larrieu, E. and Prada, J., 2022. [*The economic evaluation of Cystic echinococcosis control strategies focused on zoonotic hosts: A scoping review*](https://journals.plos.org/plosntds/article?id=10.1371/journal.pntd.0010568), PLoS Neglected Tropical Diseases, Volume 16 (7), July 2022.
Thielen, F.W., Heine, R.J.S.D., Berg, S. van den, Ham, R.M.T. ten and Groot, C.A.U. (2022). [Towards sustainability and affordability of expensive cell and gene therapies? Applying a cost-based pricing model to estimate prices for Libmeldy and Zolgensma](https://www.sciencedirect.com/science/article/pii/S1465324922007861), Cytotherapy [online] doi:10.1016/j.jcyt.2022.09.002.
### Guides and tutorials
How to [*Convert Between Currencies Using priceR*](https://www.bryanshalloway.com/2022/06/16/converting-between-currencies-using-pricer/) by Bryan Shalloway
### Contributing to priceR
If your research, guide or tutorial uses priceR, please contact the priceR maintainer (or create an [issue](https://github.com/stevecondylios/priceR/issues)) so your work can be included here.
**Issues and Feature Requests**
When reporting an issue, please include:
* Example code that reproduces the **observed** behavior.
* An explanation of what the **expected** behavior is.
For feature requests, raise an issue with the following:
* The desired functionality
* Example inputs and desired output
**Pull Requests**
Pull requests are welcomed. Before doing so, please create an issue or email me with your idea.
Any new functions should follow the conventions established by the the package's existing functions. Please ensure
* Functions are sensibly named
* The __intent__ of the contribution is clear
* At least one example is provided in the documentation