forked from uvastatlab/statistical_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_derivatives.Rmd
More file actions
74 lines (57 loc) · 1.95 KB
/
plot_derivatives.Rmd
File metadata and controls
74 lines (57 loc) · 1.95 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
---
title: "first and second derivatives"
author: "Clay Ford"
date: "5/17/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Import data
Just get data for IS481. Not sure this is what you want.
```{r message=FALSE}
library(readxl)
library(tidyr)
library(dplyr)
d <- read_excel(path = '21.04.29 Probe Concentration Comparison.xlsx', sheet = 2,
range = 'A1:P44')
# reshape to long format to help with plotting
dL <- pivot_longer(d, cols = -1, names_to = "conc", values_to = "signal")
```
Using the `smooth.spline` and `predict` functions in base R. The `smooth.spline` function fits a cubic smoothing spline to the data. The `predict` function uses the fit to predict a spline at new points and returns the derivative specified.
```{r}
nms <- unique(dL$conc)
mL <- lapply(nms, function(x)smooth.spline(x = dL$`Time (min)`[dL$conc == x],
y = dL$signal[dL$conc == x]))
# first derivative
D1 <- lapply(mL, function(x)predict(x, x = seq(0,19, length.out = 200),
deriv = 1))
# second derivative
D2 <- lapply(mL, function(x)predict(x, x = seq(0,19, length.out = 200),
deriv = 2))
# name elements in D2 and D2
names(D1) <- names(D2) <- nms
D1 <- bind_rows(D1,.id = "conc")
D2 <- bind_rows(D2,.id = "conc")
D1$conc <- factor(D1$conc, labels = nms)
D2$conc <- factor(D2$conc, labels = nms)
```
## Create plots
First derivative
```{r}
library(ggplot2)
ggplot(D1) +
aes(x, y, color = conc) +
geom_line() +
labs(title = "IS481 Probe Concentration Comparison\n[First Derivative]",
x = "Time (min)", y = "Δ Fluoresence/Δ Time (F.U./min)")
```
Second derivative
```{r}
ggplot(D2) +
aes(x, y, color = conc) +
geom_line() +
coord_cartesian(xlim = c(0,6)) +
labs(title = "IS481 Probe Concentration Comparison\n[Second Derivative]",
x = "Time (min)", y = "Δ Fluoresence/Δ Time (F.U./min)")
```