-
Notifications
You must be signed in to change notification settings - Fork 0
/
dilution_series.qmd
122 lines (87 loc) · 2.46 KB
/
dilution_series.qmd
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
# Regressions for many lipids
```{r}
#| label: setup
#| echo: false
#| message: false
source("_common.R")
library(knitr)
```
## Libraries
```{r}
#| label: load-lib
#| echo: true
#| message: false
library(here)
library(tidyverse)
library(broom)
library(ggpmisc)
```
## Overview lm and 'broom' package
```{r}
#| label: intro
#| echo: true
#| message: false
d_test <- tibble(InjVol = c(0,0.2, 0.4, 0.6, 0.8,1),
Response = c(12, 23, 34,44, 89, 101))
d_test
# Linear model
model <- lm(formula = Response ~ InjVol, data = d_test)
# Get result summary
summary(model)
# Get r^2 only
summary(model)$r.squared
# Using broom functions to summarize model results into a table
broom::glance(model)
broom::tidy(model)
```
## Import Datasets
```{r}
#| label: read-data
#| echo: true
#| message: false
d_orig <- read_csv(here("data/Testdata_Lipidomics_flat_wide_annotated_V1.csv"))
```
## Prepare Data
```{r select-rename-columns}
#| label: prepare-data
#| echo: true
#| message: false
# Convert to long format
# Convert to long format
d_long <- d_orig |>
pivot_longer(cols = -DataFileName:-InjVol,
names_to = "Lipid" ,
values_to = "Area")
# Get a table with RQCs only and sort by Lipid
d_rqc <- d_long |>
filter(QCtype == "RQC") |>
arrange(Lipid)
```
## Run regression for each lipid
In this example a logistic regression is used. The output of `glm()` is converted to a tidy table using the `broom::tidy()` function.
```{r filter-tables}
#| label: reg-analysis
#| echo: true
#| message: false
#| tbl-cap: "Model results"
#| tbl-colwidths: [60,40]
model <- as.formula("Area ~ InjVol")
d_res <- d_rqc %>%
group_by(Lipid) %>%
nest() %>%
mutate(
models = map(data, function(x) lm(model, data = x)),
#mandel = map(data, \(x) DCVtestkit::calculate_mandel(x, "InjVol", "Area")),
#ppa = map(data, \(x) DCVtestkit::calculate_pra_linear(x, "InjVol", "Area")),
tidy = map(models, function(x) broom::glance(x))) |>
unnest(c(tidy)) |>
dplyr::select(-data, -models)
# Fix DCVtestkit::calculate_pra_linear currently returning a list instead vector
# d_res$ppa <- unlist(d_res$ppa)
```
The results contain the combined estimates, errors, and *P* values for each term for each lipid species.
```{r}
#| label: tbl_results
#| echo: false
kable(head(d_res, n = 10),digits = 6)
```