-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathks_00_timeseries_traitimpute.Rmd
126 lines (96 loc) · 3.96 KB
/
ks_00_timeseries_traitimpute.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
---
title: "ks_00_traitimpute"
author: "Kate Sheridan"
date: "3/21/2022"
output: html_document
---
This script does imputation from the functional traits csvs with mice.
This differs from previous scripts in that it doesn't make a dissimilarity matrix, cluster, or do any further analysis. It simply removes NAs by imputing traits with their most probable replacement.
To run the matrix/cluster/etc from the previous script, use ks_03_timeseries_matrix or ks_04_timeseries_cluster-tsne.
But our current analysis uses only MCA/HCPC
```{r setup, include=FALSE}
library(readr)
library(dplyr)
library(mice)
library(here)
```
```{r loadin}
#be sure to use na.strings or imputation will fail!
# algae traits not ready
algae_raw <- read.csv(here("data", '20220525_shoals_algae_traits.csv'),
na.strings = c("","NA", 'N/A')) %>% filter(!(is.na(species)))
animal_raw <- read.csv(here("data", '20220320_shoals_animal_traits.csv'),
na.strings = c("","NA", 'N/A')) %>% filter(!(is.na(species)))
```
# Select columns
Only columns that will be used later; remove notes, references, etc.
Also remove any columns known a priori to be collinear, or move them to the front to be masked during imputations.
```{r filter}
algae_raw <- algae_raw %>%
select(species, phylum, group, common_name_division_bgr, invasive_gom,
intertidal_gom, subtidal_gom, autotroph,
# s-d morphology, expanded
erect_morphology_growth_form, foliose_morphology_growth_form,
filamentous_morphology_growth_form, crustose_morphology_growth_form,
globular_morphology_growth_form, leathery_external_morphology,
#articulated_external_morphology,
branched_external_morphology, saccate_external_morphology,
corticated_internal_anatomy,
#physical_defenses_calcification,
# canopy/subcanopy/turf
space_use_canopy_gom, space_use_subcanopy_gom, space_use_turf_gom,
# life history traits
potential_asexual_reproduction, perennial_gom,
#size_categories,
# where/on what does it grow
epilithic_env_position,
#epiphytic_env_position, epizoic_env_position,
epibiotic_env_position_gom
) %>%
# Make everything a factor
mutate(across(.fns = ~ as.factor(.)))
animal_raw <- animal_raw %>%
select(species, phylum, common_name_division, # for filtering later
# subtidal/intertidal for filtering later
subtidal, intertidal,
# basic traits
solitary_colonial, adult_body_size_bin,
calcareous, motility_adult,
# trophic traits
herbivore, predator, omnivore, deposit_feeder,
filter_suspension, scavenger, trophic_level,
# habitat traits
benthic, pelagic, epibiotic
) %>%
# holdover from past script, not there's still whitespace issues tbh
mutate(trophic_level = str_trim(trophic_level)) %>%
# make everything a factor
mutate(across(.fns = ~ as.factor(.)))
```
## Impute with Mice
```{r mice}
## algae
# run mice on select columns
algae_imp <- mice(algae_raw[,9:ncol(algae_raw)], m=5, method = 'cart', print = F)
algae_imp2 <- complete(algae_imp) # necessary!
# reattach taxonomy/filtering columns
algae_imp2 <- cbind(algae_raw[,1:8],algae_imp2)
#to see logged events
algae_imp$loggedEvents
## animals
# run mice on select columns
animal_imp <- mice(animal_raw[,6:ncol(animal_raw)], m=5, method = 'cart', print = F)
animal_imp2 <- complete(animal_imp) #necessary!
# reattach taxonomy/filtering columns
animal_imp2 <- cbind(animal_raw[,1:5],animal_imp2)
#to see logged events
animal_imp$loggedEvents
```
```{r save}
# write csv for imputed data
write.csv(algae_imp2, file = here('data', 'clean','20220525_algae_imputed.csv'))
write.csv(animal_imp2, file = here('data', 'clean','20220321_animal_imputed.csv'))
# if you want .RData versions
#save(algae_imp2, file = here('data','algae_imputed.RData'))
#save(animal_imp2, file = here('data','animals_imputed.RData'))
```