-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023_05_16_regression.Rmd
209 lines (154 loc) · 6.38 KB
/
2023_05_16_regression.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
---
title: "2023_05_16_regression"
author: "kim soyeon"
date: "2023-05-16"
output: html_document
---
# my
참고
1. chatGTP
2. https://ayeimanol-r.net/2013/04/22/getting-started-drawing-a-scatterplot-with-a-linear-regression-smoother-edited-title-label-and-theme-for-report/
3. https://www.nature.com/articles/s41598-022-06189-5
4. https://stackoverflow.com/questions/61266084/can-we-neatly-align-the-regression-equation-and-r2-and-p-value
Regression plot with a significant Pearsoncorrelation
```{r}
reg_plot <- function(df, x = x, y = y, Ylab = NULL, Xlab = NULL, Title = NULL, label_pos = "right") {
ggplot(df, aes_string(x, y)) +
geom_point() +
stat_smooth(color = "blue", method="lm") +
stat_poly_eq(formula = as.formula(y ~ x),
label.x = "centre",
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., sep = "~~~")),
color= "red",
parse = TRUE)+
stat_fit_glance(method = 'lm',
method.args = list(formula = y ~ x),
label.x = label_pos,
aes(label = paste("~italic(p) ==", round(..p.value.., digits = 3),
"~italic(R)^2 ==", round(..r.squared.., digits = 2),
sep = "~")),
color= "red",
parse=TRUE)+
theme_minimal() +
labs(title = Title, y = Ylab, x = Xlab)
}
```
## 1. 하나의 taxa와 metadata비교 + R값 P값 추가 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
```{r}
library(phyloseq)
library(ggplot2)
library(dplyr)
# install.packages("ggpmisc")
library(ggpmisc)
ps <- readRDS("./ps.rds")
# phyloseq-class experiment-level object
# otu_table() OTU Table: [ 770 taxa and 34 samples ]
# sample_data() Sample Data: [ 34 samples by 8 sample variables ]
# tax_table() Taxonomy Table: [ 770 taxa by 7 taxonomic ranks ]
# phy_tree() Phylogenetic Tree: [ 770 tips and 768 internal nodes ]
ps.rel <- transform_sample_counts(ps, function(x) x/sum(x) )
otu <- as.data.frame(otu_table(ps.rel))
otu <- otu %>% mutate(Sum = rowSums(.))
otu %>% arrange(-Sum)
otu_table <- as.data.frame(t(otu_table(ps.rel)))
metadata <- as.data.frame(sample_data(ps.rel))
# tax <- as.data.frame(tax_table(ps.rel))
# tax["d29fe3c70564fc0f69f2c03e0d1e5561", "Species"]
otu_meta <- merge(otu_table, metadata, by = "row.names")
reg_plot(otu_meta, "days.since.experiment.start", "d29fe3c70564fc0f69f2c03e0d1e5561",
Ylab = "OTU", Xlab = "Time", Title = "Regression plot 03")
```
## 2. alpha diversity와 metadata비교 VVVVVVVVVVVVVVVVVVVVVVVVVVVVV
```{r}
alpha_div <- estimate_richness(ps.rel, measures = "Shannon")
alpha_meta <- merge(sample_data(ps.rel), alpha_div, by = "row.names")
reg_plot(alpha_meta, "days.since.experiment.start", "Shannon",
Ylab = "Alpha diversity", Xlab = "Time", Title = "Regression plot 02")
```
## 3. metadata 간의 비교 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
```{r}
meta2 <- sample_data(ps.rel) %>% as.data.frame()
meta2$Random <- sample(1:300, 34)
reg_plot(meta2, "days.since.experiment.start", "Random",
Ylab = "Random data", Xlab = "Time", Title = "Regression plot 04")
```
## 4. 각 taxa간 비교 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
```{r}
otu <- as.data.frame(t(otu_table(ps.rel)))
x <- "3c9c437f27aca05f8db167cd080ff1ec" # "Prevotella melaninogenica"
y <- "1d2e5f3444ca750c85302ceee2473331" # Haemophilus parainfluenzae
otu[, "3c9c437f27aca05f8db167cd080ff1ec"]
reg_plot(otu,
x = "`3c9c437f27aca05f8db167cd080ff1ec`", # "Prevotella melaninogenica
y = "`1d2e5f3444ca750c85302ceee2473331`", # Haemophilus parainfluenzae
Ylab = "OTU1", Xlab = "OTU2", Title = "Regression plot 05")
ggplot(otu, aes_string("`3c9c437f27aca05f8db167cd080ff1ec`", "`1d2e5f3444ca750c85302ceee2473331`")) +
geom_point() +
stat_smooth(color = "blue", method="lm") +
stat_poly_eq(formula = as.formula(y ~ x),
label.x = "centre",
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., sep = "~~~")),
color= "red",
parse = TRUE)+
stat_fit_glance(method = 'lm',
method.args = list(formula = y ~ x),
label.x = label_pos,
aes(label = paste("~italic(p) ==", round(..p.value.., digits = 3),
"~italic(R)^2 ==", round(..r.squared.., digits = 2),
sep = "~")),
color= "red",
parse=TRUE)+
theme_minimal() +
labs(title = Title, y = Ylab, x = Xlab)
}
```
## 5. read 수 ~diversity VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
```{r}
alpha_div <- estimate_richness(ps.rel, measures = "Shannon")
otu <- otu_table(ps) %>% t() %>% as.data.frame() %>%
mutate(Sample_read = rowSums(.))
alpha_read <- merge(otu, alpha_div, by = "row.names")
# reg_plot(alpha_read, "Sample_read", "Shannon")
?lm
model <- lm(Sample_read~Shannon, alpha_read)
# Call:
# lm(formula = Sample_read ~ Shannon, data = alpha_read)
#
# Coefficients:
# (Intercept) Shannon
# 1459 1035
summary(model)$coefficients[2,4] # p-value
# 0.202146
summary(model)$r.squared # R squared
# 0.05032406
x <- alpha_read$Sample_read
y <- alpha_read$Shannon
a <-cor.test(x,y)
a
# p-value = 0.2021
a$p.value # 0.202146
a
cor.test(x,y, method = "spearman")
# p-value = 0.1744
ggplot(alpha_read, aes(Sample_read, Shannon)) +
geom_point() +
stat_smooth(color = "blue", method="lm") +
stat_poly_eq(formula = as.formula(y ~ x),
label.x = "centre",
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., sep = "~~~")),
color= "red",
parse = TRUE)+
stat_fit_glance(method = 'lm',
method.args = list(formula = y ~ x),
label.x = label_pos,
aes(label = paste("~italic(p) ==", round(..p.value.., digits = 3),
"~italic(R)^2 ==", round(..r.squared.., digits = 2),
sep = "~")),
color= "red",
parse=TRUE)+
theme_minimal() +
labs(title = "Regression plot ", y = "shannon", x = "Read Number")
```