-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_code.R
89 lines (71 loc) · 2.91 KB
/
test_code.R
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
# if not already done, install ckpt2r
devtools::install_github('https://github.com/Peter-T-Ruehr/ckpt2r')
remove.packages("ckpt2r")
# load ckpt2r
library(ckpt2r)
# read all landmark files from folder without considering potential subfolders.
folder.with.landmarks <- ckpt2r_examples()
landmarks_df <- read_checkpoint(folder.with.landmarks,
recursive = FALSE,
pattern = NULL)
print(landmarks_df)
# We have several two landmarks 'antenna_prox_L' and 'antenna_prox_R' marked as
# missing (defined = M). So let's remove these landmark lines:
landmarks_df <- landmarks_df[landmarks_df$defined != "M",]
# now we will convert the table into an array 2D
array_2D <- array_2D_from_df(landmarks_df,
LM_column = "LM",
specimen_column = "file_name",
X_column = "X",
Y_column = "Y",
Z_column = "Z")
# In our case, we have several landmarks that are not defined for all
# species. Keeping these in the array_2D may cause problems in downstream
# analyses. So let's remove all landmarks that contain NA values
array_2D <- array_2D[, - which(colSums(is.na(array_2D)) > 0)]
# convert array_2D to data.frame, add column names and remove specimen column
array_2D <- as.data.frame(array_2D)
rownames(array_2D) <- array_2D$specimen
array_2D$specimen <- NULL
dim(array_2D)
# returns: n.specimens, n.landmarks*n.dimensions
# in the example file case: (15, 51 [=17*3])
# get names of landmarks that are still in array_2D
LMs_present = unique(gsub("_\\w{1}$", "", colnames(array_2D)))
print(LMs_present)
# turn 2D array into 3D array
require(geomorph)
array.3D <- arrayspecs(A = array_2D,
p = (ncol(array_2D)/3),
k = 3,
sep = ".")
dim(array.3D)
# n.landmarks, n.dimenions, specimens
# in the example file case: (17, 3, 15)
# Procrustes alignment
gpa.results <- gpagen(array.3D)
# !!! if this returned:
# Error in gpagen(array.3D) :
# Data matrix contains missing values. Estimate these first (see 'estimate.missing').
# !!! then check if you still have NA values in your array 2D.
# this should be fine now
summary(gpa.results$coords)
# plot all LM points of all specimens
for(i in 1:length(landmarks_df)){
if(i == 1){
plot(gpa.results$coords[,,i], pch = 16, cex = 0.5, col="gray80")
} else {
points(gpa.results$coords[,,i], pch = 16, cex = 0.5, col="gray80")
}
}
# plot consensus of all LM points of all specimens
points(gpa.results$consensus, pch=16)
text(gpa.results$consensus, labels = LMs_present,
pos = 4, cex = 0.75, srt=-30)
# run PCA
pca.results <- gm.prcomp(A = gpa.results$coords)
# print and plot PCA results
summary(pca.results)
plot(pca.results, pch = 16)
text(pca.results$x[, 1:2], labels = rownames(pca.results$x),
pos = 4, cex = 0.75, srt=-0)