https://github.com/tom-n-walker/uphill-plants-soil-carbon
Revision bd5cb92172fcaeb792dba4b479620ccb3e1a8569 authored by Tom Walker on 07 September 2021, 06:32:17 UTC, committed by Tom Walker on 07 September 2021, 06:32:17 UTC
1 parent 84beafa
Tip revision: bd5cb92172fcaeb792dba4b479620ccb3e1a8569 authored by Tom Walker on 07 September 2021, 06:32:17 UTC
Added and validated analysis scripts for soil C loss, field soil pools, glasshouse soil variables.
Added and validated analysis scripts for soil C loss, field soil pools, glasshouse soil variables.
Tip revision: bd5cb92
apply_mice.R
################################################################################
#### Project: General functionality
#### Title: Function | Small function | Apply MICE imputation
#### Author: Tom Walker (thomas.walker@usys.ethz.ch)
#### Date: 15 July 2020
#### ---------------------------------------------------------------------------
apply_mice <- function(matrix, iter){
# get column names
cols <- colnames(matrix)
colnames(matrix) <- paste0("C", 1:ncol(matrix))
# apply mice imputation
imp <- mice(
matrix,
iter
)
# generate list and add each imputation round to a bin
imp_list <- list()
for(i in 1:iter){
imp_list[[i]] <- as.matrix(
complete(imp, i)
)
}
# take means of these iterations
out <- apply(
simplify2array(imp_list),
1:2,
mean,
na.rm = T
) %>% as.data.frame
colnames(out) <- cols
# return
return(out)
}

Computing file changes ...