Skip to content

Commit dd93d07

Browse files
committed
😐 LDA in R (capitulation)
1 parent 8adc3c7 commit dd93d07

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

β€Žcode/Topology.Rβ€Ž

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# libraries
2+
library(ggpubr)
3+
library(here)
4+
library(effectsize)
5+
library(ggrepel)
6+
library(ggtext)
7+
library(MASS)
8+
library(patchwork)
9+
library(rstatix)
10+
library(tidyverse)
11+
library(vegan)
12+
13+
# set path to code sub dir
14+
setwd(here())
15+
16+
# import data
17+
18+
df <- read_csv("data/outputs/topology.csv") %>%
19+
dplyr::select(-c(richness))
20+
21+
dep_vars <- as.matrix(df[2:ncol(df)])
22+
23+
24+
fit <- manova(dep_vars ~ model, data = df)
25+
summary(fit)
26+
27+
#get effect size
28+
effectsize::eta_squared(fit)
29+
30+
post_hoc <- lda(model~., df)
31+
post_hoc
32+
33+
# plot
34+
plot_lda <- data.frame(model = df$model,
35+
lda = predict(post_hoc)$x)
36+
37+
plot_arrow <- as.data.frame(post_hoc[["scaling"]]) %>%
38+
mutate(var = str_replace(row.names(.), "dep_vars", ""),
39+
lda.LD1 = scale(LD1),
40+
lda.LD2 = scale(LD2))
41+
42+
ggplot(plot_lda) +
43+
geom_point(aes(x = lda.LD1,
44+
y = lda.LD2,
45+
colour = model),
46+
size = 3,
47+
alpha = 0.3) +
48+
#= geom_segment(data = plot_arrow,
49+
#= aes(x = 0,
50+
#= y = 0,
51+
#= xend = lda.LD1,
52+
#= yend = lda.LD2)) +
53+
#= geom_text_repel(data = plot_arrow,
54+
#= aes(label = var,
55+
#= x = lda.LD1,
56+
#= y = lda.LD2),
57+
#= max.overlaps = getOption("ggrepel.max.overlaps", default = 100)) +
58+
coord_cartesian(clip = "off") +
59+
guides(color = guide_legend(override.aes = list(alpha = 1))) +
60+
labs(x = "LDA 1",
61+
y = "LDA 2") +
62+
theme_classic() +
63+
theme(panel.border = element_rect(colour = 'black',
64+
fill = "#ffffff00"),
65+
panel.grid.minor = element_blank(),
66+
panel.grid.major.x = element_blank(),
67+
axis.line = element_blank(),
68+
axis.ticks = element_line(colour = colorspace::darken("#dddddd", 0.1),
69+
linewidth = 0.3),
70+
plot.background = element_rect(fill = "white", colour = NA),
71+
panel.background = element_rect(fill = "white", colour = NA),
72+
legend.background = element_rect(fill = "white", colour = NA),
73+
legend.key = element_blank(),
74+
text = element_text(color = "#5e5e5e"),
75+
plot.margin = margin(10, 5, 5, 10),
76+
legend.margin = margin(1, 2, 1, 2)
77+
)
78+
79+
ggsave("../figures/lda.png",
80+
width = 5000,
81+
height = 4000,
82+
units = "px",
83+
dpi = 700)

β€Žcode/code.Rprojβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX

β€Žfigures/lda.pngβ€Ž

1.38 MB
Loading

0 commit comments

Comments
Β (0)