Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision 951e73295060ad88bddf5d48a3aa4b9d90475312 authored by Tom Walker on 07 September 2021, 14:32:36 UTC, committed by Tom Walker on 07 September 2021, 14:32:36 UTC
Merge branch 'main' of github.com:tom-n-walker/uphill-plants-soil-carbon into main
2 parent s 05e1672 + 5c297ab
  • Files
  • Changes
  • 2bb633e
  • /
  • analysis_code
  • /
  • field_soil_carbon_loss.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:951e73295060ad88bddf5d48a3aa4b9d90475312
directory badge Iframe embedding
swh:1:dir:8cafe5307d20fe509d5d742c142d86d1d1a1b039
content badge Iframe embedding
swh:1:cnt:c1d731a3018bf18bfa466518d357eefe0af50d3e

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
field_soil_carbon_loss.R
################################################################################
#### Project: Lowland plant migrations alpine soil C loss
#### Title:   Main soil carbon loss effect
#### Author:  Tom Walker (thomas.walker@usys.ethz.ch)
#### Date:    26 May 2021
#### ---------------------------------------------------------------------------


#### PROLOGUE ------------------------------------------------------------------

## Options ----
# remove objects from global environment
rm(list = ls())
# R session options (no factors, bias against scientific #s)
options(
  stringsAsFactors = F,
  scipen = 6
)

## Libraries ----
# standard library set
library(tidyverse)
library(data.table)
library(nlme)
library(emmeans)


#### DATA ----------------------------------------------------------------------

## Load from Drake plan ----
allData <- drake::readd(field_data)

## Basic combine and unnest ----
soil <- allData %>%
  select(site, treatments, soil_pools) %>% 
  unnest(cols = c(treatments, soil_pools)) %>%
  as.data.frame %>%
  # make site-level blocking factor
  mutate(site_block = tolower(paste0(substr(site, 1, 1), block)))

## Duplicate high site control for 2nd analysis ----
# build dataset with C and W
soilCW <- soil %>%
  filter(treatment == "C" | treatment == "W") %>%
  # account for duplicated control
  mutate(type = "warmed")
# build dataset with C and I
soilCI <- soil %>%
  filter(treatment == "C" | treatment == "I") %>%
  # account for duplicated control
  mutate(type = "warmed+invaded")
# bind them together (duplicates control)
soilCCWI <- bind_rows(soilCW, soilCI) %>%
  select(site, site_block, type, treatment, Soil.temp, Csoil)


#### ANALYSE -------------------------------------------------------------------

## Main soil carbon effect ----
# build model
m1 <- lme(
  Csoil ~ treatment * site, 
  random = ~ 1 | site_block, 
  data = soil,
  na.action = "na.exclude",
  method = "ML"
)
# diagnose model
r1 <- residuals(m1, type = "normalized")
par(mfrow = c(1, 3))
plot(r1 ~ fitted(m1))
boxplot(r1 ~ soil$treatment)
hist(r1)
# test main effects
m2 <- update(m1, ~.- treatment:site)
anova(m1, m2)
anova(m2, update(m2, ~.- treatment))
anova(m2, update(m2, ~.- site))
# post-hoc
m1reml <- update(m1, method = "REML")
m2reml <- update(m2, method = "REML")
emmeans(m1reml, pairwise ~ treatment | site)
emmeans(m2reml, pairwise ~ treatment | site)

## Soil carbon on temperature ----
# build model
m4 <- lme(
  Csoil ~ Soil.temp * type,
  random = ~ site | site_block,
  data = soilCCWI,
  method = "ML",
  na.action = "na.exclude"
  )
# diagnose model
r4 <- residuals(m4, type = "pearson")
par(mfrow = c(1, 3))
plot(r4 ~ fitted(m4))
boxplot(r4 ~ soilCCWI$treatment)
hist(r4)
# test main effects
m5 <- update(m4, ~.- Soil.temp:type)
anova(m4, m5)

## Calculate magnitude of soil carbon loss ----
# calculating errors following standard practice of proliferating error:
# http://www.met.rdg.ac.uk/~swrhgnrj/combining_errors.pdf
# update best model for REML
m4reml <- update(m4, method = "REML")
# coefficients
m4coefs <- intervals(m4reml, which = "fixed")$fixed
# calculate error 
ciWarm <- (m4coefs[2, 3] - m4coefs[2, 1]) / 2
ciWaLo <- (m4coefs[4, 3] - m4coefs[4, 1]) / 2
# calculate estimates
estWarm <- m4coefs[2, 2]
estWaLo <- estWarm + m4coefs[4, 2]
# relative differences
diffAll <- (estWaLo / estWarm - 1) * 100
ciAll <- sqrt(((ciWarm/estWarm)^2) + ((ciWaLo/estWaLo)^2)) * diffAll
# print excess soil C loss ± 95%
diffAll
ciAll


#### BASIC PLOTS ---------------------------------------------------------------

## Soil C loss on treatment ----
# summarise data
soilPlotData <- soil %>%
  group_by(site, treatment) %>%
  summarise(mean = mean(Csoil, na.rm = T),
            se = sd(Csoil, na.rm = T)/sqrt(n())) %>%
  ungroup %>%
  mutate(treatment = fct_relevel(treatment, "C", "W", "I"))
# plot
mainPlot <- ggplot(soilPlotData) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  coord_cartesian(ylim = c(10, 20)) +
  aes(x = treatment, y = mean, ymax = mean + se, ymin = mean - se) +
  geom_errorbar(width = 0.2) +
  geom_bar(stat = "identity", col = "black", fill = "white") +
  facet_wrap(~site) +
  labs(y = "Soil C", x = "") 

## Soil C loss per ºC ----
siPlot <- ggplot(soilCCWI) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  aes(x = Soil.temp, y = Csoil, linetype = type, col = site) +
  geom_point() +
  geom_smooth(method = "lm", se = F) +
  guides(col = "none") +
  labs(y = "Soil C", x = "Soil ºC") 

## Combine ----
cowplot::plot_grid(mainPlot, siPlot)
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API