-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivatives-in-R.Rmd
87 lines (76 loc) · 1.69 KB
/
derivatives-in-R.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
---
title: "Derivatives in R"
author: "Marie Rivers"
date: "8/3/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(palmerpenguins)
```
## Section 3: Derivatives in R
```{r}
# First Order univariate derivatives
fx <- expression(x ^ 2)
df_dx <- D(expr = fx, 'x')
df_dx
x <- 3
eval(expr = df_dx)
Pa <- expression(2 * (((3 * a) + 2) ^ 4) - 5)
dP_da <- D(expr = Pa, 'a')
dP_da
a <- 1.2
eval(expr = dP_da)
gt <- expression((-4 * t) / (((t ^ 2) + 1) ^ 3))
dg_dt <- D(expr = gt, 't')
dg_dt
t <- 0
eval(expr = dg_dt)
```
```{r}
# Higher order univariate derivatives
Gz <- expression(4.6 - 1.9 * (z^3))
dG_dz <- D(expr = Gz, 'z')
dG_dz
d2G_dz2 <- D(expr = dG_dz, 'z')
d2G_dz2
```
```{r}
# partial derivatives
f_xyz <- expression(2*x^2*y - 3*x + 1 + 4*z^4*x)
df_dx <- D(expr = f_xyz, 'x')
df_dx
df_dy <- D(expr = f_xyz, 'y')
df_dy
dg_dz <- D(expr = f_xyz, 'z')
dg_dz
```
## Section 4: Make Some Plots
```{r}
eq_fx <- function(x) {
(2.4 * (exp(-0.05*x)) * sin(x))
}
ggplot(data = data.frame (x = c(-.5, .5)), aes(x=x)) +
stat_function(fun = eq_fx)
```
```{r}
#### Penguin bill length vs. bill depth
ggplot(data = penguins, aes(x = bill_depth_mm, y = bill_length_mm)) +
geom_point(aes(color = species)) +
labs (x = "bill length (mm)",
y = "bill depth (mm)",
title = "Palmer penguins bill size",
caption = "data collected by KB Gorman ey al. at Palmer Station",
color = "species")
```
```{r}
ggplot(data = penguins, aes(x = species, y = flipper_length_mm)) +
geom_jitter() +
labs (x = "species",
y = "flipper length (mm)",
title = "penguin flipper length by species")
```
```{r}
print(hello)
```