-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgp_analyzer.R
6472 lines (2882 loc) · 200 KB
/
gp_analyzer.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### This script was used in Karbstein et al. (2021, https://onlinelibrary.wiley.com/doi/10.1111/mec.15919) to perform (i) data import of flow cytometric (FC) and flow cytometric seed screening (FCSS) data, (ii) descriptive statistics, (iii) mode of reproduction per ploidy level analyses, (iv) multivariate modeling (path analysis) comprising sexuality (sexual to asexual seeds per population), genome-wide heterozygosity, ploidy level, mode of reproduction, habitat type, and flower features, (v) modeling and mapping of sexuality and genetic diversity across Europe, (vi) area calculations per reproduction mode, and (vii) genetic structure analyses (sNMF).
#### Flow Cytometric (FC) Data ####
#### read data ####
#install.packages("openxlsx")
library(openxlsx)
dat<-read.xlsx("00_final_masterfile_final.xlsx", sheet="FC")
str(dat)
dat$pop<-as.factor(dat$pop)
dat$pop_ID<-as.factor(dat$pop_ID)
dat$ploidy_all_levels<-as.factor(dat$ploidy_all_levels)
dat$PK1<-as.numeric(dat$PK1)
dat$counts_leaf<-as.numeric(dat$counts_leaf)
dat$counts_total<-as.numeric(dat$counts_total)
str(dat)
#### sample size and population means ####
sample_size_pop_FC<-tapply(dat$PK1, dat$pop, length)#there are no NAs
#write.csv(sample_size_pop_FC, "sample_size_pop_FC.csv")
#min
mean_pop_min_FC<-tapply(dat$PK1, dat$pop, min)#there are no NAs
#write.csv(mean_pop_min_FC, "mean_pop_min_FC.csv")
#max
mean_pop_max_FC<-tapply(dat$PK1, dat$pop, max)#there are no NAs
#write.csv(mean_pop_max_FC, "mean_pop_max_FC.csv")
#### Flow Cytomotric Seed Screening (FCSS) data ####
# read data
#install.packages("openxlsx")
library(openxlsx)
dat<-read.xlsx("00_final_masterfile_final.xlsx", sheet="FCSS")
str(dat)
dat$pop<-as.factor(dat$pop)
dat$pop_ID<-as.factor(dat$pop_ID)
dat$pop_ID_nr<-as.factor(dat$pop_ID_nr)
dat$ploidy_embryo<-as.factor(dat$ploidy_embryo)
dat$sex<-as.factor(dat$sex)
dat$asex<-as.factor(dat$asex)
dat$sexuality_simple<-as.factor(dat$sexuality_simple)
dat$PI<-as.numeric(dat$PI)
dat$counts_total<-as.numeric(dat$counts_total)
str(dat)
#
dat2<-read.xlsx("00_final_masterfile_final.xlsx", sheet="Tabelle4")
str(dat2)
dat2$pop_ID <- as.factor(dat2$pop_ID)
res <- tapply(dat2$mean_embryo, dat2$pop_ID, mean, na.rm=TRUE)
write.csv(res, "res.csv")
#### sample size and population means ####
#ind per pop
sample_size_ind_pop_FCSS<-tapply(dat$ID, dat$pop, FUN = function(x) length(unique(x)))#there are no NAs
#write.csv(sample_size_ind_pop_FCSS, "sample_size_ind_per_pop_FCSS.csv")
#seeds per pop
sample_size_seeds_pop_FCSS<-tapply(dat$pop_ID_nr, dat$pop, length)#there are no NAs
#write.csv(sample_size_seeds_pop_FCSS, "sample_size_seeds_per_pop_FCSS.csv")
#min_embryo
min_embryo_pop_FCSS<-tapply(dat$mean_embryo, dat$pop, min, na.rm = TRUE)
write.csv(min_embryo_pop_FCSS, "min_embryo_pop_FCSS.csv")
#max_embryo
max_embryo_pop_FCSS<-tapply(dat$mean_embryo, dat$pop, max, na.rm = TRUE)
#write.csv(max_embryo_pop_FCSS, "max_embryo_pop_FCSS.csv")
#mean_embryo_per_ind
mean_embryo_ind_FCSS<-tapply(dat$mean_embryo, dat$pop_ID, mean, na.rm=TRUE)
#write.csv(mean_embryo_ind_FCSS, "mean_embryo_ind_FCSS2.csv")
#mean_embryo_per_pop
mean_embryo_pop_FCSS<-tapply(dat$mean_embryo, dat$pop, mean, na.rm=TRUE)
#write.csv(mean_embryo_pop_FCSS, "mean_embryo_pop_FCSS2.csv")
#ploidy_levels_embryo
ploidy_levels_embryo_pop_FCSS<-table(dat$pop, dat$ploidy_embryo)
#write.csv(ploidy_levels_embryo_pop_FCSS, "ploidy_levels_embryo_pop_FCSS.csv")
#sexuality_endosperm
sexuality_endosperm_pop_FCSS<-table(dat$pop, dat$sexuality)
#write.csv(sexuality_endosperm_pop_FCSS, "sexuality_endosperm_pop_FCSS_pop_FCSS.csv")
#peak_index_min
peak_index_min_pop_FCSS<-tapply(dat$PI, dat$pop, min, na.rm=TRUE)
#write.csv(peak_index_min_pop_FCSS, "peak_index_min_pop_FCSS_pop_FCSS.csv")
#peak_index_max
peak_index_max_pop_FCSS<-tapply(dat$PI, dat$pop, max, na.rm=TRUE)
#write.csv(peak_index_max_pop_FCSS, "peak_index_max_pop_FCSS_pop_FCSS.csv")
#pi_pop
peak_index_max_pop_FCSS<-tapply(dat$PI, dat$pop, max, na.rm=TRUE)
#write.csv(peak_index_max_pop_FCSS, "peak_index_max_pop_FCSS_pop_FCSS.csv")
#sexuality_pop
mean_sexual_pop_FCSS<-tapply(dat$sexual, dat$pop, mean, na.rm=TRUE)
#write.csv(mean_sexual_pop_FCSS, "mean_sexuality_pop_FCSS.csv")
#reviser_pop
reviser_pop_FCSS<-table(dat$pop, dat$reviser)
write.csv(reviser_pop_FCSS, "reviser_pop_FCSS.csv")
#sexuality_ind
mean_sexual_ind_FCSS<-tapply(dat$sexual, dat$pop_ID, mean, na.rm=TRUE)
#write.csv(mean_sexual_ind_FCSS, "mean_sexuality_ind_FCSS.csv")
#ploidy_levels_embryo_ind
ploidy_levels_embryo_ind_FCSS<-table(dat$pop_ID, dat$ploidy_embry_total)
write.csv(ploidy_levels_embryo_ind_FCSS, "ploidy_levels_embryo_ind_FCSS.csv")
# repro mode per pop
res <- table(dat$pop, dat$sexuality_exact)
#write.csv(res, "repro_mode_per_pop.csv")
####reproduction modes per ploidy level####
par(mfrow=c(2,2))
#subsets
# 3D Exploded Pie Chart
#install.packages("plotrix")
library(plotrix)
dat_2n<-subset(dat, dat$ploidy_embry_total=="diploid")
str(dat_2n$ploidy_embry_total)
table(dat_2n$ploidy_embry_total, dat_2n$sexuality_simple)
slices <- c(99.0,1.0)
lbls <- c("99.0% sexual seeds", "1.0% asexual seeds")
pie3D(slices,labels=lbls,explode=0.1, col=c("orange", "dodgerblue"))
#?pie3D
dat_3n<-subset(dat, dat$ploidy_embry_total=="triploid")
table(dat_3n$ploidy_embry_total, dat_3n$sexuality_simple)
slices <- c(100)
lbls <- c("100% asexual seeds")
pie3D(slices,labels=lbls,explode=0.1, col=c( "dodgerblue"))
dat_4n<-subset(dat, dat$ploidy_embry_total=="tetraploid")
table(dat_4n$ploidy_embry_total, dat_4n$sexuality_simple)
slices <- c(4.5,95.5)
lbls <- c("4.5% sexual seeds", "95.5% asexual seeds")
pie3D(slices,labels=lbls,explode=0.1, col=c("orange", "dodgerblue"))
dat_6n<-subset(dat, dat$ploidy_embry_total=="hexaploid")
table(dat_6n$ploidy_embry_total, dat_6n$sexuality_simple)
slices <- c(6.7, 93.3)
lbls <- c("6.7% sexual seeds", "93.3% asexual seeds")
pie3D(slices,labels=lbls,explode=0.1, col=c("orange", "dodgerblue"))
# Simple Pie Chart
par(mfrow=c(2,2))
slices1 <- c(99.0,1.0) # 2n
slices2 <- c(100) # 3n
slices3 <- c(4.1,95.9) #4n
slices4 <- c(6.3, 93.7) #6m
pie(slices1)
pie(slices2)
pie(slices3)
pie(slices4)
# different apomictic pathways
tapply(dat$pop, dat$sexuality_exact, length)
# sex per individual
res <- tapply(dat$sexual, dat$pop_ID, mean)
write.csv(res, "sexuality_per_ind.csv")
#### quality checks and variable selection genetic diversity ####
library(openxlsx)
dat_genetics<-read.xlsx("00_final_masterfile_final.xlsx", sheet="genetic_diversity_ISC_BSC")
str(dat_genetics)
het_sites_pop<-tapply(dat_genetics$het_sites, dat_genetics$pop, mean)
#write.csv(het_sites_pop, "het_sites_pop.csv")
#differences between two randomly sampled individuals of a population
boxplot(dat_genetics$het_sites_1_2~dat_genetics$ind_1_2)
dat_genetics_1<-subset(dat_genetics, ind_1_2=="1")
shapiro.test(dat_genetics_1$het_sites_1_2)#nope
dat_genetics_2<-subset(dat_genetics, ind_1_2=="2")
shapiro.test(dat_genetics_2$het_sites_1_2)#nope
wilcox.test(dat_genetics$het_sites_1_2~dat_genetics$ind_1_2)#significant
### individual sexuality to individual genetic diversity?
# read data
#install.packages("openxlsx")
library(openxlsx)
dat_gen_ind<-read.xlsx("00_final_masterfile_final.xlsx", sheet="FCSS_pop_ind")
str(dat_gen_ind)
mod_gen_ind<-glm(sexuality_ind~gen_ind, family = "quasibinomial", data = dat_gen_ind)
summary(mod_gen_ind)#sign.
plot(sexuality_ind~gen_ind, data = dat_gen_ind)
#without obligate apomicts (too many)
mod_gen_ind_2<-glm(sexuality_ind_wt_zeroes~gen_ind, family = "quasibinomial", data = dat_gen_ind)
summary(mod_gen_ind_2)#sign.
plot(sexuality_ind_wt_zeroes~gen_ind, data = dat_gen_ind)#non-sign.
####review: path analysis#####
#install.packages("openxlsx")
library(openxlsx)
dat_summary<-read.xlsx("00_final_masterfile_final.xlsx", sheet="locations_FC_FCSS_meassurements")
str(dat_summary)
dat_summary$pop_ID<-as.factor(dat_summary$pop_ID)
dat_summary$taxon<-as.factor(dat_summary$taxon)
dat_summary$DNA_ploidy<-as.factor(dat_summary$DNA_ploidy)
dat_summary$no_individuals_FC<-as.factor(dat_summary$no_individuals_FC)
dat_summary$no_individuals_FC<-as.factor(dat_summary$no_individuals_FCSS)
dat_summary$no_seeds<-as.factor(dat_summary$no_seeds)
dat_summary$ploidy_embryo<-as.factor(dat_summary$ploidy_embryo)
dat_summary$ploidy_embryo_simple<-as.factor(dat_summary$ploidy_embryo_simple)
dat_summary$reproductive_pathway<-as.factor(dat_summary$reproductive_pathway)
dat_summary$reproductive_pathway_simple<-as.factor(dat_summary$reproductive_pathway_simple)
dat_summary$habitat<-as.factor(dat_summary$habitat)
dat_summary$habitat_simple<-as.factor(dat_summary$habitat_simple)
dat_summary$seed_development_in_situ<-as.factor(dat_summary$seed_development_in_situ)
dat_summary$corolla_leaves_max<-as.numeric(dat_summary$corolla_leaves_max)
dat_summary$genetic_group_Nnet<-as.factor(dat_summary$genetic_group_Nnet)
dat_summary$genetic_group_snmf<-as.factor(dat_summary$genetic_group_snmf)
str(dat_summary)
#install.packages("lavaan")
library(lavaan)
#specification of model using auto.var argument
model <-'
#equation where sexuality is predicted by habitat and ploidy
sexuality_2 ~ ploidy_embryo_simple + habitat_simple
#equation where petals is predicted by sexuality
corolla_leaves_mean ~ sexuality_2
#equation where heterozygosity is predicted by habitat, ploidy, reproduction mode, sexuality
genetic_diversity_2_prop ~ ploidy_embryo_simple + habitat_simple + reproductive_pathway_ordered + sexuality_2
#estimating the variances of the exogenous variables (ploidy_embryo_simple, habitat_simple, reproduction mode)
ploidy_embryo_simple~~ploidy_embryo_simple
habitat_simple~~habitat_simple
reproductive_pathway_ordered~~reproductive_pathway_ordered
#estimating the covariances of the exogenous variables
ploidy_embryo_simple~~habitat_simple+reproductive_pathway_ordered
habitat_simple~~reproductive_pathway_ordered
#The auto.var argument when fitting the model can be used so that you do not have to directly request estimation of residual variances'
fit <- lavaan(model, data=dat_summary, auto.var=TRUE)
#problem: Error in lav_data_full(data = data, group = group, cluster = cluster, :
#lavaan ERROR: unordered factor(s) detected; make them numeric or ordered: ploidy_embryo_simple habitat_simpl
# no glm supported
#only beta
#install.packages("piecewiseSEM")
#library(piecewiseSEM)
devtools::install_github("jslefche/piecewiseSEM@devel")
library(piecewiseSEM)#version 2.2.0
#install developmental branch
#install Rtools before
library(devtools)
install_github("jslefche/piecewiseSEM@devel", build_vignette = TRUE, force=TRUE)
# Load library
library(piecewiseSEM)
# Read vignette
vignette("piecewiseSEM")
#install.packages('multcompView')
library(multcompView)
#Piecewise SEM (or confirmatory path analysis) expands upon traditional SEM by introducing a flexible mathematical framework that can incorporate a wide variety of model structures, distributions, and assumptions. These include: interactions and non-normal responses, random effects and hierarchical models, ...
#... and alternate correlation structures (including phylogenetic, spatial, and temporal).
### exclude rows with missing data in sexuality_2, ploidy_embryo_simple, habitat_simple, reproductive_pathway, genetic_diversity_2_prop, corolla_leaves_max and exclude triploids
dat_summary_without_3n<-dat_summary[-c(1,2,3,5,87,104,112,166,205,214,215,233, 47, 64, 102, 103, 108, 110, 229, 230, 233, 218, 219, 215, 214, 206, 194, 172, 170, 167, 162, 123, 2, 3, 95, 89, 87, 79, 72, 64, 57, 53, 48, 39, 35, 31, 19, 18, 15, 12, 1, 107, 109, 242),]
boxplot(dat_summary_without_3n$genetic_diversity_2 ~ dat_summary_without_3n$ploidy_embryo_simple)
###### subset exclude triploid sample, without interactions, without phylogenetic distances
#ret <- handleCategoricalCoefs(ret, model, dat_summary_without_3n, test.statistic = "F", test.type = "II")
model <- psem(glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, data = dat_summary_without_3n, family = quasibinomial), glm(corolla_leaves_max ~ sexuality_2, data = dat_summary_without_3n, family = quasipoisson), glm(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway, data = dat_summary_without_3n, family = quasibinomial))# indep claims --> many significances (relationships have to be removed because make no biological sense), goodness of fit --> highly significant
summary(model, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")#makes no difference to Menard.OE, for categorcial variables, estimates are means (generated using emmeans package; see also multcomp::cld package for significance codes)
#specify claims as correlated errors (which make no biological sense)
basisSet(model, direction=NULL)
summary(update(model, ploidy_embryo_simple %~~% corolla_leaves_max), test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model2 <- update(model, habitat_simple %~~% corolla_leaves_max)
summary(model2, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model3 <- update(model2, corolla_leaves_max %~~% genetic_diversity_2_prop)
summary(model3, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model4 <- update(model3, reproductive_pathway %~~% corolla_leaves_max)
summary(model4, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model5 <- update(model4, corolla_leaves_max %~~% genetic_diversity_2_prop)
summary(model5, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model6 <- update(model5, corolla_leaves_max %~~% ploidy_embryo_simple)
summary(model6, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
###### subset exclude triploid sample, without interactions, with genetic groups ######
#install.packages("lme4")
library(lme4)
#install.packages("lmerTest")
library(lmerTest)
str(dat_summary_without_3n$sexuality_2)
#underdispersion in models with sexuality (too conservate results, not so problematic like overdispersion)
?glmmPQL
library(MASS)
library(nlme)
#summary(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, random = list(genetic_group = ~1), family=quasibinomial, data= dat_summary_without_3n, correlation=corAR1(0, form = ~ 1 | pop_ID)))
#summary(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, random = list(genetic_group = ~1), family=quasibinomial, data= dat_summary_without_3n))
#summary(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, random = list(genetic_group = ~1), family=binomial, data= dat_summary_without_3n))
# no difference in p values
#summary(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, random = list(genetic_group = ~1), family=quasipoisson, data= dat_summary_without_3n))
#summary(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, random = ~1|genetic_group, family=quasibinomial, data= dat_summary_without_3n))
#summary(glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, family=quasibinomial, data= dat_summary_without_3n))
#glmer with many errors
#model <- psem(glmer(sexuality_2 ~ ploidy_embryo_simple + habitat_simple + 1|genetic_group, family=binomial, data = dat_summary_without_3n), glmer(corolla_leaves_max ~ sexuality_2 + 1|genetic_group, family=poisson, data = dat_summary_without_3n), glmer(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway + 1|genetic_group, family=binomial, data = dat_summary_without_3n))# indep claims --> many significances (relationships have to be removed because make no bioogical sense), goodness of fit --> highly significant
#summary(model, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")#makes no difference to Menard.OE, for categorcial variables, estimates are means (generated using emmeans package; see also multcomp::cld package for significance codes)
### without "quasi" (makes only difference in R2 calculations) --> likelihood estimations can handle under- and overdispersion
#glmmPQL
### log genetic_diversity_2_prop (see below)
dat_summary_without_3n$genetic_diversity_2_log <- log(dat_summary_without_3n$genetic_diversity_2)
dat_summary_without_3n$genetic_diversity_2_log <- dat_summary_without_3n$genetic_diversity_2_log + 10
#due to gaussian model, arcsin transformation of sexuality
qqnorm(dat_summary_without_3n$sexuality_2)
qqline(dat_summary_without_3n$sexuality_2)
shapiro.test(dat_summary_without_3n$sexuality_2)
library(base)
shapiro.test(asin(dat_summary_without_3n$sexuality_2))
#why R2 of genetic div response so low?? y standardization??
#?rsquared
#rsquared(model)
#rsquared(model, method = "delta")
#rsquared(model, method = "lognormal")
#rsquared(model, method = "trigamma")
#model <- psem(glmmPQL(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway, random = ~1|genetic_group, family=gaussian, data = dat_summary_without_3n))# indep claims --> many significances (relationships have to be removed because make no bioogical sense), goodness of fit --> highly significant
#summary(model, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")#
#the issue is the low range of genetic_diversity_2_prop (widely below 0.1), for binomial 0-1 would be optimal (like sexuality)
#shapiro.test(log(dat_summary_without_3n$genetic_diversity_2_prop))#yes
#qqnorm(dat_summary_without_3n$genetic_diversity_2_prop)
#qqline(dat_summary_without_3n$genetic_diversity_2_prop)
#hist(dat_summary_without_3n$genetic_diversity_2_prop)
model <- psem(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, random = ~1|genetic_group, family=binomial, data = dat_summary_without_3n), glmmPQL(corolla_leaves_max ~ sexuality_2, random = ~1|genetic_group, family=poisson, data = dat_summary_without_3n), glmmPQL(genetic_diversity_2_log ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway, random = ~1|genetic_group, family=gaussian, data = dat_summary_without_3n))# indep claims --> many significances (relationships have to be removed because make no bioogical sense), goodness of fit --> highly significant
summary(model, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")#
### specify claims as correlated errors (which make no biological sense)
basisSet(model, direction=NULL)
model2 <- update(model, habitat_simple %~~% corolla_leaves_max)
summary(model2, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model3 <- update(model2, corolla_leaves_max %~~% genetic_diversity_2_log)
summary(model3, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model4 <- update(model3, reproductive_pathway %~~% corolla_leaves_max)
summary(model4, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
#model5 <- update(model4, corolla_leaves_max %~~% genetic_diversity_2_log)
#(model5, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model5 <- update(model4, corolla_leaves_max %~~% ploidy_embryo_simple)
summary(model5, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale", test.type="II")
plot(genetic_diversity_2 ~ sexuality_2, data=dat_summary_without_3n)
summary(glm(genetic_diversity_2_log ~ sexuality_2, data=dat_summary_without_3n, gaussian))
coefs(model6, standardize = "scale")
###### subset exclude triploid sample, with interactions, with genetic groups ######
model_int <- psem(glmmPQL(sexuality_2 ~ ploidy_embryo_simple + habitat_simple + habitat_simple_ploidy_embryo_simple, random = ~1|genetic_group, family=binomial, data = dat_summary_without_3n), glmmPQL(corolla_leaves_max ~ sexuality_2, random = ~1|genetic_group, family=poisson, data = dat_summary_without_3n), glmmPQL(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway, random = ~1|genetic_group, family=gaussian, data = dat_summary_without_3n))# indep claims --> many significances (relationships have to be removed because make no biological sense), goodness of fit --> highly significant
summary(model_int, intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")#
### specify claims as correlated errors (which make no biological sense)
basisSet(model, direction=NULL)
model_int2 <- update(model_int, habitat_simple %~~% corolla_leaves_max)
summary(model2, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model_int3 <- update(model_int2, corolla_leaves_max %~~% genetic_diversity_2_prop)
summary(model_int3, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model_int4 <- update(model_int3, reproductive_pathway %~~% corolla_leaves_max)
summary(model_int4, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model_int5 <- update(model_int4, corolla_leaves_max %~~% genetic_diversity_2_prop)
summary(model_int5, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
model_int6 <- update(model_int5, corolla_leaves_max %~~% ploidy_embryo_simple)
summary(model_int6, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
############ tries for correlation matrix based on genetic distances
dat_genetic_distances <- read.xlsx("genetic_distancles_all_97_min30_old_without_preplate.xlsx", sheet="final_rev")
dat_genetic_distances$pop_ID <- as.factor(dat_genetic_distances$pop_ID)
str(dat_genetic_distances$pop_ID)
dat_genetic_distances <- as.matrix(dat_genetic_distances[, -c(1)])
#install.packages("corpcor")
library(corpcor)
make.positive.definite()
obj <- cov(dat_genetic_distances)
obj <- cov2cor(dat_genetic_distances)
write.csv(obj, "correlation_matrix.csv")
#average columns and rows per pop manually
# import correlation matrix
dat_genetic_correlations <- read.xlsx("genetic_distancles_all_97_min30_old_without_preplate.xlsx", sheet="final_corr_rev_means_sel_w")
dat_genetic_correlations <- as.matrix(dat_genetic_correlations)
dat_genetic_correlations_pos2 <- make.positive.definite(dat_genetic_correlations)
#normalize matrix (0-1)
#dat_genetic_correlations_normalized = (dat_genetic_correlations-min(dat_genetic_correlations))/(max(dat_genetic_correlations)-min(dat_genetic_correlations))
library(nlme)
?corMatrix
?corCAR1
?corSymm
#obj_corSymm <- corSymm(value=dat_genetic_correlations)
obj_corSymm <- corSymm(value=dat_genetic_correlations[col(dat_genetic_correlations) < row(dat_genetic_correlations)], form = ~ 1)
#obj_corSymm <- corSymm(value=dat_genetic_correlations[col(dat_genetic_correlations) < row(dat_genetic_correlations)], form = ~ 1 | as.factor(pop_ID)) # factor specified here for further modeling
#dat_genetic_correlation_matrix <- data.matrix(dat_genetic_correlations, rownames = " ")
library(nlme)
?gls
#component models
psem(glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, data = dat_summary_without_3n, family = quasibinomial), glm(corolla_leaves_max ~ sexuality_2, data = dat_summary_without_3n, family = poisson), glm(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway, data = dat_summary_without_3n, family = quasibinomial))
model_component_1 <- gls(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, data = dat_summary_without_3n, correlation=obj_corSymm)
model_component_2 <- gls(corolla_leaves_max ~ sexuality_2, data = dat_summary_without_3n, correlation=obj_corSymm)
model_component_3 <- gls(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway, data = dat_summary_without_3n, correlation=obj_corSymm)
model <- psem(glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple, data = dat_summary_without_3n, family = quasibinomial), glm(corolla_leaves_max ~ sexuality_2, data = dat_summary_without_3n, family = poisson), glm(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway + 1 genetic distances, data = dat_summary_without_3n, family = quasibinomial))
summary(model, test.statistic = "F", test.type="II", intercepts = FALSE, standardize.type = "latent.linear", standardize="scale")
#### subset exclude triploid sample, with interactions, without phylogenetic distances
#create interaction terms
int_ploidy_habitat <- interaction(dat_summary_without_3n$ploidy_embryo_simple:dat_summary_without_3n$habitat_simple) # create an interaction variable
levels(int_ploidy_habitat)
contrasts(int_ploidy_habitat)
#show all collinearities
mod <- glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple + habitat_simple_ploidy_embryo_simple
, data = dat_summary_without_3n, family = quasibinomial)
summary(mod)
mod <- psem(glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple + ploidy_embryo_simple:habitat_simple, data = dat_summary_without_3n, family = quasibinomial))
summary(mod, test.statistic = "F", test.type="III")
mod2 <- glm(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway + ploidy_embryo_simple:habitat_simple + ploidy_embryo_simple:reproductive_pathway + reproductive_pathway:habitat_simple, data = dat_summary_without_3n, family = quasibinomial)
summary(mod2)
mod2 <- update(mod, ~. - ploidy_embryo_simple6N:habitat_simpleopen_habitats)
summary(mod2)
#remove collinear level interactions: The easiest way might be to manipulate the model matrix to remove the unwanted columns
int_ploidy_habitat <- model.matrix(sexuality_2 ~ ploidy_embryo_simple:habitat_simple, data = dat_summary_without_3n)
omit <- grep("[:]fs[^ploidy_embryo_simple6N:habitat_simpleopen_habitats]", colnames(int_ploidy_habitat))
xx <- xx[, -omit]
lm(y ~ 0 + xx)
model <- psem(glm(sexuality_2 ~ ploidy_embryo_simple + habitat_simple + ploidy_embryo_simple:habitat_simple, data = dat_summary_without_3n, family = quasibinomial), glm(corolla_leaves_max ~ sexuality_2, data = dat_summary_without_3n, family = poisson), glm(genetic_diversity_2_prop ~ sexuality_2 + ploidy_embryo_simple + habitat_simple + reproductive_pathway + ploidy_embryo_simple:habitat_simple + ploidy_embryo_simple:reproductive_pathway + reproductive_pathway:habitat_simple, data = dat_summary_without_3n, family = quasibinomial))
contrasts(dat_summary_without_3n$reproductive_pathway:dat_summary_without_3n$habitat_simple)
summary(glm(genetic_diversity_2_prop ~ reproductive_pathway:habitat_simple, singular.ok = TRUE, data =
dat_summary_without_3n, family = quasibinomial))
summary(model)
model <- psem(glm(genetic_diversity_2_prop ~ reproductive_pathway:habitat_simple, singular.ok = TRUE, data = dat_summary_without_3n, family = quasibinomial))
summary(model, test.statistic = "F", test.type="III", singular.ok = TRUE)
sum <- contrasts(dat_summary_without_3n$reproductive_pathway:dat_summary_without_3n$habitat_simple)
# subset exclude triploid sample, with interactions, with phylogenetic distances
#### basic regressions among sexuality, ploidy level, repro pathway, corrola leaves and genetic diversity ####
# calculate phylogenetic distances
library(ape)
my_tree <- read.tree('Figure_T13_raxml_min10.tre.txt') # For Newick format trees
my_tree$tip.label # Check the tip labels of your tree
my_tree2<-as.phylo(my_tree)
object <- corBrownian(1, phy = my_tree2)
## S3 method for class 'corBrownian'
coef(object, unconstrained = TRUE)
## S3 method for class 'corBrownian'
corMatrix(object, covariate = getCovariate(object), corr = TRUE)
#install.packages("adephylo")
library(adephylo)
distTips(my_tree2, tips = "all", method = c("patristic"), useC = TRUE)
#rownames(my_data) <- gsub(' ', '_', my_data$species_name_with_spaces)
#rownames(my_tree) <- my_tree$species_name
# read data
#install.packages("openxlsx")
library(openxlsx)
dat_summary<-read.xlsx("00_final_masterfile_final.xlsx", sheet="locations_FC_FCSS_meassurements")
str(dat_summary)
dat_summary$pop_ID<-as.factor(dat_summary$pop_ID)
dat_summary$taxon<-as.factor(dat_summary$taxon)
dat_summary$DNA_ploidy<-as.factor(dat_summary$DNA_ploidy)
dat_summary$no_individuals_FC<-as.factor(dat_summary$no_individuals_FC)
dat_summary$no_individuals_FC<-as.factor(dat_summary$no_individuals_FCSS)
dat_summary$no_seeds<-as.factor(dat_summary$no_seeds)
dat_summary$ploidy_embryo<-as.factor(dat_summary$ploidy_embryo)
dat_summary$ploidy_embryo_simple<-as.factor(dat_summary$ploidy_embryo_simple)
dat_summary$reproductive_pathway<-as.factor(dat_summary$reproductive_pathway)
dat_summary$reproductive_pathway_simple<-as.factor(dat_summary$reproductive_pathway_simple)
dat_summary$habitat<-as.factor(dat_summary$habitat)
dat_summary$habitat_simple<-as.factor(dat_summary$habitat_simple)
dat_summary$seed_development_in_situ<-as.factor(dat_summary$seed_development_in_situ)
str(dat_summary)
# pops per reproduction mode
tapply(dat_summary$pop_ID, dat_summary$reproductive_pathway, length)
# sexuality per reproduction mode
tapply(dat_summary$sexuality, dat_summary$reproductive_pathway, mean)
sample_size_per_taxon<-tapply(dat_summary$pop_ID, dat_summary$taxon_2020_garden_plants, FUN = function(x) length(unique(x)))#there are no NAs
min(sample_size_per_taxon)
max(sample_size_per_taxon)
####1. genetic diversity####
par(mfrow=c(1,3))
#genetic diversity and ploidy levels
#subset exclude triploid sample
dat_summary_without_3n<-dat_summary[-c(107, 109, 242),]
boxplot(dat_summary_without_3n$genetic_diversity_2 ~ dat_summary_without_3n$ploidy_embryo_simple)
#install.packages("ggplot2")
library(ggplot2)
# Basic violin plot
dat_summary_without_3n$ploidy_embryo_simple<-as.factor(dat_summary_without_3n$ploidy_embryo_simple)# group variable has to be a factor
p <- ggplot(dat_summary_without_3n, aes(x=ploidy_embryo_simple, y=genetic_diversity_2)) +
geom_violin()
p
# Rotate the violin plot
p + coord_flip()
# Set trim argument to FALSE
ggplot(dat_summary_without_3n, aes(x=ploidy_embryo_simple, y=genetic_diversity_2)) + geom_violin(trim=FALSE)
p + geom_boxplot(width=0.1)# trimmed with boxplot
p <- ggplot(dat_summary_without_3n, aes(x=ploidy_embryo_simple, y=genetic_diversity_2)) + geom_violin(trim=FALSE, fill="orange")# if more than one color --> fill=c(col1, col2)
p + geom_boxplot(width=0.1)# not trimmed with boxplot
tapply(dat_summary_without_3n$genetic_diversity_2, dat_summary_without_3n$ploidy_embryo_simple, FUN = function(x) length(x[!is.na(x)]))
dat_summary_2n<-subset(dat_summary, dat_summary$ploidy_embryo_simple=="2N")
shapiro.test(dat_summary_2n$genetic_diversity_2)#no
dat_summary_4n<-subset(dat_summary, dat_summary$ploidy_embryo_simple=="4N")
shapiro.test(dat_summary_4n$genetic_diversity_2)#no
dat_summary_6n<-subset(dat_summary, dat_summary$ploidy_embryo_simple=="6N")
shapiro.test(dat_summary_6n$genetic_diversity_2)#yes
#modxx<-aov(dat_summary_without_3n$genetic_diversity ~ dat_summary_without_3n$ploidy_embryo_simple)
#summary(modxx)#no
kruskal.test(dat_summary_without_3n$genetic_diversity_2 ~ dat_summary_without_3n$ploidy_embryo_simple)#sign. differences
pairwise.wilcox.test(dat_summary_without_3n$genetic_diversity, dat_summary_without_3n$ploidy_embryo_simple, p.adjust.method = "fdr")
plot(dat_summary_without_3n$genetic_diversity_2 ~ dat_summary_without_3n$ploidy_embryo_simple)
tapply(dat_summary_without_3n$genetic_diversity_2, dat_summary_without_3n$ploidy_embryo_simple, mean, na.rm=TRUE)# small differences
tapply(dat_summary$genetic_diversity_2, dat_summary$ploidy_embryo_simple, mean, na.rm=TRUE)# small differences
kruskal.test(dat_summary$genetic_diversity_2 ~ dat_summary$ploidy_embryo_simple)#sign. differences
pairwise.wilcox.test(dat_summary$genetic_diversity, dat_summary$ploidy_embryo_simple)
tapply(dat_summary_without_3n$genetic_diversity_2, dat_summary_without_3n$ploidy_embryo_simple, FUN = function(x) length(x[!is.na(x)]))
#genetic diversity and reproductive pathway
boxplot(dat_summary$genetic_diversity_2 ~ dat_summary$reproductive_pathway)