Last updated: 2019-10-22

Checks: 7 0

Knit directory: BloomSail/

This reproducible R Markdown analysis was created with workflowr (version 1.4.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20191021) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/TinaV/
    Ignored:    data/_merged_data_files/
    Ignored:    data/_summarized_data_files/

Untracked files:
    Untracked:  docs/figure/merging_interpolation.Rmd/

Unstaged changes:
    Modified:   code/Workflowr_project_managment.R

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd efcafd1 jens-daniel-mueller 2019-10-22 Added data base, merging, and RT determination
html 1595fe9 jens-daniel-mueller 2019-10-21 Build site.
html a059c41 jens-daniel-mueller 2019-10-21 Build site.
Rmd eff54ce jens-daniel-mueller 2019-10-21 Added CTD read-in
html 32ec4f7 jens-daniel-mueller 2019-10-21 Build site.
Rmd b2d2bbb jens-daniel-mueller 2019-10-21 Structured data base and response time Rmd
html bafa88f jens-daniel-mueller 2019-10-21 Build site.
Rmd 53ad162 jens-daniel-mueller 2019-10-21 Structured data base and response time Rmd
html 076a36b jens-daniel-mueller 2019-10-21 Build site.
Rmd 3e8a32e jens-daniel-mueller 2019-10-21 Structured data base and response time Rmd
html b2d0164 jens-daniel-mueller 2019-10-21 Build site.
Rmd 53ae361 jens-daniel-mueller 2019-10-21 Added data base and response time Rmd

library(tidyverse)
library(seacarb)
library(data.table)
library(broom)
library(lubridate)

Sensitivity considerations

A change in DIC of 1 µmol kg-1 corresponds to a change in pCO2 of around 1 µatm, in the Central Baltic Sea at a pCO2 of around 100 µatm (summertime conditions).

df <- data.frame(cbind(
  (c(1720)),
  (c(7))))

Tem <- seq(5,25,5)
pCO2<-seq(50,500,20)

df<-merge(df, Tem)
names(df) <- c("AT", "S", "Tem")  

df<-merge(df, pCO2)
names(df) <- c("AT", "S", "Tem", "pCO2")  

df<-data.table(df)
df$AT<-df$AT*1e-6

df$DIC<-carb(flag=24, var1=df$pCO2, var2=df$AT, S=df$S, T=df$Tem, k1k2="m10", kf="dg", pHscale="T")[,16]
df$pCO2.corr<-carb(flag=15, var1=df$AT, var2=df$DIC, S=df$S, T=df$Tem, k1k2="m10", kf="dg", pHscale="T")[,9]

df$pCO2.2<-df$pCO2.corr + 25
df$DIC.2<-carb(flag=24, var1=df$pCO2.2, var2=df$AT, S=df$S, T=df$Tem, k1k2="m10", kf="dg", pHscale="T")[,16]


df$ratio<-(df$pCO2.2-df$pCO2.corr)/(df$DIC.2*1e6-df$DIC*1e6)

df %>% 
  ggplot(aes(pCO2, ratio, col=as.factor(Tem)))+
  geom_line()+
  scale_color_viridis_d(option = "C",name="Tem [°C]")+
  labs(x=expression(pCO[2]~(µatm)), y=expression(Delta~pCO[2]~"/"~Delta~DIC~(µatm~µmol^{-1}~kg)))+
  scale_y_continuous(limits = c(0,8), breaks = seq(0,10,1))
pCO~2~ sensitivity to changes in DIC.

pCO2 sensitivity to changes in DIC.

rm(df, Tem, pCO2)

HydroC sensor settings

The sensor was first run with a low power pump (1W), later and for most parts of the expedition with a stronger (8W) pump. Pumps were switched between recordings (data file: SD_datafile_20180718_170417CO2-0618-001.txt):

  • 2018-07-17;13:08:34
  • 2018-07-17;13:08:35

Logging frequency for all measurement modes (Measure, Zero, Flush) was set to:

10 sec for all recordings including SD_datafile_20180714_073641CO2-0618-001.txt

Increase to 2 sec in SD_datafile_20180717_121052CO2-0618-001.txt at:

  • 2018-07-14;07:52:02
  • 2018-07-14;07:52:12
  • 2018-07-14;07:52:14

Increase to 2 sec in SD_datafile_20180718_170417CO2-0618-001 at:

  • 2018-07-17;12:27:25
  • 2018-07-17;12:27:27
  • 2018-07-17;12:27:28

Response time determination

Response times were determined by fitting a nonlinear least-squares model as described here by Douglas Watson.

  • Flush period length: 300 sec
# Read and prepare data

df <- read_csv(here::here("data/_merged_data_files",
                          "BloomSail_CTD_HydroC.csv"),
               col_types = cols(ID = col_character(),
                                pCO2_analog = col_double(),
                                pCO2 = col_double(),
                                Zero = col_factor(),
                                Flush = col_factor(),
                                Zero_ID = col_integer(),
                                duration = col_double(),
                                mixing = col_character()))

df <- df %>%
  select(date_time, ID, dep, tem, Flush, pCO2, Zero_ID, duration, mixing)

df <- df %>%
  filter(Flush == 1, duration <=300)

# df <- df %>% 
#   filter(Flush == 1, duration <=500, !(Zero_ID %in% c(13, 34, 49, 50, 89, 105, 109)))

# df <- df %>%
#   filter(Zero_ID != 53)

df <- df %>% 
  group_by(Zero_ID, mixing) %>% 
  mutate(duration_equi = duration- min(duration))
# Plot individual Flush periods with exponential fit ----------------------

i <- 95

df_ID <- df %>%
  filter(Zero_ID == i)

fit <- df_ID %>%
  filter(mixing == "equilibration") %>%
  nls(pCO2 ~ SSasymp(duration_equi, yf, y0, log_alpha), data = .)

tau <- as.numeric(exp(-tidy(fit)[3,2]))
#RSS <- sum(resid(fit)^2)

augment(fit) %>%
  ggplot(aes(duration_equi, pCO2))+
  geom_point()+
  geom_line(aes(y = .fitted))+
  geom_vline(xintercept = tau)+
  labs(y=expression(pCO[2]~(µatm)), x="Duration of Flush period (s)")
Example response time determination by non-linear least squares fit to the pCO~2~ recovery signal after zeroing. The vertical line indicates the determined response time tau.

Example response time determination by non-linear least squares fit to the pCO2 recovery signal after zeroing. The vertical line indicates the determined response time tau.

rm(df_ID, fit, i, tau)

# Plot individual Flush periods with exponential fit ----------------------

# for (i in unique(df$Zero_ID)) {
# 
# df_ID <- df %>%
#   filter(Zero_ID == i)
# 
# fit <- df_ID %>%
#   filter(mixing == "equilibration") %>%
#   nls(pCO2 ~ SSasymp(duration_equi, yf, y0, log_alpha), data = .)
# 
# tau <- as.numeric(exp(-tidy(fit)[3,2]))
# #RSS <- sum(resid(fit)^2)
# 
# augment(fit) %>%
#   ggplot(aes(duration_equi, pCO2))+
#   geom_point()+
#   geom_line(aes(y = .fitted))+
#   geom_vline(xintercept = tau)
# 
# ggsave(here::here("/Plots/TinaV/Sensor/HydroC_diagnostics/Response_time_fits",
#                   paste(i,"_Zero_ID_HydroC_RT_exp.jpg", sep="")),
#          width = 10, height = 4)
# }


# Plot individual Flush periods with linearized response variable  --------


# for (i in unique(df$Zero_ID)) {
# 
# #i <- 50
# df_ID <- df %>%
#   filter(Zero_ID == i,
#          mixing == "equilibration")
# 
# mean_pCO2 <- df_ID %>% 
#   slice((n()-4) : n()) %>% 
#   summarise(mean_pCO2 = mean(pCO2))
# 
# df_ID <- full_join(df_ID, mean_pCO2) %>% 
#   mutate(dpCO2 = max(pCO2) - pCO2,
#          ln_dpCO2 = log(dpCO2))
# 
# 
# df_ID %>%
#   ggplot(aes(duration_equi, ln_dpCO2))+
#   geom_point()+
#   geom_smooth(method = "lm")+
#   theme_bw()
# 
# # augment(fit) %>%
# #   ggplot(aes(duration_equi, pCO2))+
# #   geom_point()+
# #   geom_line(aes(y = .fitted))+
# #   geom_vline(xintercept = tau)
# 
# ggsave(here::here("/Plots/TinaV/Sensor/HydroC_diagnostics/Response_time_fits",
#                   paste(i,"_Zero_ID_HydroC_RT_linear.jpg", sep="")),
#          width = 10, height = 4)
# }
# Response time fitting ---------------------------------------------------

RT <- df %>% 
  filter(mixing == "equilibration") %>% 
  group_by(Zero_ID) %>% 
  do(fit = nls(pCO2 ~ SSasymp(duration_equi, yf, y0, log_alpha), data = .)) %>% 
  tidy(fit) %>% 
  select(Zero_ID, term, estimate) %>% 
  spread(term, estimate) %>% 
  select(1,2) %>% 
  mutate(tau = exp(-log_alpha))


# Residuals from fit ------------------------------------------------------

augmented <- df %>% 
  filter(mixing == "equilibration") %>% 
  group_by(Zero_ID) %>% 
  do(fit = nls(pCO2 ~ SSasymp(duration_equi, yf, y0, log_alpha), data = .)) %>% 
  augment(fit)

# qplot(duration_equi, pCO2_corr, data = augmented, geom = 'point', colour = as.factor(Zero_ID)) +
#   geom_line(aes(y=.fitted))
# 
# qplot(duration_equi, .resid, data = augmented, geom = 'point', colour = as.factor(Zero_ID))

augmented_sum <- augmented %>% 
  group_by(Zero_ID) %>% 
  summarise(mean_resid = mean(abs(.resid)),
            mean_resid_rel = mean(abs(.resid))/max(pCO2),
            max_pCO2_corr = max(pCO2))

# Standard error of tau ---------------------------------------------------

St_Err <- df %>% 
  filter(mixing == "equilibration") %>% 
  group_by(Zero_ID) %>% 
  do(fit = nls(pCO2 ~ SSasymp(duration_equi, yf, y0, log_alpha), data = .)) %>% 
  tidy(fit) %>% 
  select(Zero_ID, term, std.error) %>% 
  spread(term, std.error) %>% 
  select(1,2) %>% 
  rename(tau_st_err = log_alpha)


# Merge RT, mean residuals and St error -----------------------------------

RT <- full_join(RT, augmented_sum)
RT <- full_join(RT, St_Err)

rm(augmented, augmented_sum, St_Err)



# Identify residual threshold ---------------------------------------------

# RT %>% 
#   ggplot(aes(mean_resid_rel, Zero_ID, label=Zero_ID)) +
#   geom_point(shape=21)+
#   scale_fill_viridis_c()+
#   geom_label()

RT %>% 
  filter(mean_resid_rel >= 0.0065)
# A tibble: 7 x 7
# Groups:   Zero_ID [7]
  Zero_ID log_alpha   tau mean_resid mean_resid_rel max_pCO2_corr
    <int>     <dbl> <dbl>      <dbl>          <dbl>         <dbl>
1      13     -4.55  94.9      1.19         0.00740         161. 
2      50     -3.51  33.5     11.2          0.0176          638. 
3      58     -6.46 639.       4.69         0.0312          150. 
4      60     -4.12  61.6      0.674        0.0108           62.3
5      89     -5.12 168.       2.27         0.0233           97.7
6     108     -5.89 363.      13.3          0.0200          662. 
7     112     -3.98  53.3      2.12         0.00704         301. 
# ... with 1 more variable: tau_st_err <dbl>
# RT %>% 
#   filter(mean_resid_rel < 0.0065) %>% 
#   ggplot(aes(Zero_ID, tau, label=round(mean_resid_rel,4))) +
#   geom_label(data=RT, aes(Zero_ID, tau, label=round(mean_resid_rel,4)), col="red") +
#   geom_point(shape=21)+
#   scale_fill_viridis_c()+
#   geom_label()

RT %>% 
  filter(mean_resid_rel < 0.0065) %>% 
  ggplot(aes(Zero_ID, tau))+
  geom_point()

# Mean tau ----------------------------------------------------------------

max(unique(df[df$date_time < ymd_hms("2018-07-17;13:08:34"),]$Zero_ID))
[1] 20
unique(df[df$date_time > ymd_hms("2018-07-17;13:08:34"),]$Zero_ID)
 [1]  33  34  35  36  37  39  40  42  43  44  47  48  49  50  51  58  59
[18]  60  61  81  82  83  84  85  86  89  90  91  92  93  94  95  98  99
[35] 100 101 102 103 105 106 107 108 109 112
RT %>% 
  filter(mean_resid_rel < 0.0065) %>% 
  mutate(pump_power = if_else(Zero_ID <= 20, "1W", "8W")) %>% 
  group_by(pump_power) %>% 
  summarise(tau = mean(tau))
# A tibble: 2 x 2
  pump_power   tau
  <chr>      <dbl>
1 1W          85.5
2 8W          56.1

This approach

Contros in-house

Comparison

Pre-smoothing

Response time correction

Post-smoothing

Response time optimization

Open tasks / questions

  • Compare Contros and own response time estimates
  • Compare differnt response time correction methods (Bittig vs. Fiedler, Miloshevich, Fietzek)
  • Test impact of duration for response time estimation on final mean response time
  • Test impact of selection criterion for “good” response time estimates on final mean response time
  • Check results from field response time experiment (high zeroing frequency)

sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.7.4   broom_0.5.2       data.table_1.12.2
 [4] seacarb_3.2.12    oce_0.9-23        gsw_1.0-5        
 [7] testthat_2.2.1    forcats_0.4.0     stringr_1.4.0    
[10] dplyr_0.8.3       purrr_0.3.2       readr_1.3.1      
[13] tidyr_1.0.0       tibble_2.1.3      ggplot2_3.2.1    
[16] tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5  xfun_0.9          haven_2.1.1      
 [4] lattice_0.20-35   colorspace_1.3-2  vctrs_0.2.0      
 [7] generics_0.0.2    viridisLite_0.3.0 htmltools_0.3.6  
[10] yaml_2.2.0        utf8_1.1.4        rlang_0.4.0      
[13] pillar_1.3.1      glue_1.3.1        withr_2.1.2      
[16] modelr_0.1.5      readxl_1.3.1      lifecycle_0.1.0  
[19] plyr_1.8.4        munsell_0.5.0     gtable_0.2.0     
[22] workflowr_1.4.0   cellranger_1.1.0  rvest_0.3.4      
[25] evaluate_0.14     labeling_0.3      knitr_1.25       
[28] fansi_0.4.0       highr_0.7         Rcpp_1.0.1       
[31] scales_0.5.0      backports_1.1.2   jsonlite_1.6     
[34] fs_1.3.1          hms_0.5.1         digest_0.6.18    
[37] stringi_1.1.7     grid_3.5.0        rprojroot_1.3-2  
[40] here_0.1          cli_1.1.0         tools_3.5.0      
[43] magrittr_1.5      lazyeval_0.2.1    crayon_1.3.4     
[46] whisker_0.3-2     pkgconfig_2.0.2   zeallot_0.1.0    
[49] xml2_1.2.2        assertthat_0.2.0  rmarkdown_1.15   
[52] httr_1.4.1        rstudioapi_0.10   R6_2.2.2         
[55] nlme_3.1-137      git2r_0.23.0      compiler_3.5.0