+ Show code- Hide code # This is code Op_en3104/bayes on page [[EU-kalat]]
library(OpasnetUtils)
library(reshape2)
library(rjags) # JAGS
library(ggplot2)
library(MASS) # mvrnorm
library(car) # scatterplotMatrix
objects.latest("Op_en3104", code_name = "preprocess") # [[EU-kalat]]
################## Data for the main congeners and species only
#> unique(euAll$Congener)
#[1] 2378TCDD 12378PeCDD 123478HCDD 123678HCDD 123789HCDD 1234678HpCDD
#[7] OCDD 2378TCDF 12378PeCDF 23478PeCDF 123478HCDF 123678HCDF
#[13] 123789HCDF 234678HCDF 1234678HpCDF 1234789HpCDF OCDF CoPCB77 ...
# Remove the four with too little data (>70% BDL) and all non-PCDDF
# aggregate(eu@data$euResult, by = eu@data["POP"], FUN = function(x) mean(x == 0))
#[1] Baltic herring Sprat Salmon Sea trout Vendace
#[6] Roach Perch Pike Pike-perch Burbot
#[11] Whitefish Flounder Bream River lamprey Cod
#[16] Trout Rainbow trout Arctic char
if(TRUE) {
conl <- c("TEQdx", "TEQpcb")
} else {
conl <- as.character(unique(euRaw@data$POP)[c(1:12, 14, 15)]) # 7 OCDD should be removed
}
fisl <- as.character(unique(euRaw@data$Fish_species)[c(1:4, 6:14, 17)])
# conl
#[1] "2378TCDD" "12378PeCDD" "123478HCDD" "123678HCDD" "123789HCDD"
#[6] "1234678HpCDD" "OCDD" "2378TCDF" "12378PeCDF" "23478PeCDF"
#[11] "123478HCDF" "123678HCDF" "234678HCDF" "1234678HpCDF"
#> fisl
#[1] "Baltic herring" "Sprat" "Salmon" "Sea trout"
#[5] "Roach" "Perch" "Pike" "Pike-perch"
#[9] "Burbot" "Whitefish" "Flounder" "Bream"
#[13] "River lamprey" "Rainbow trout"
C <- length(conl)
Fi <- length(fisl)
N <- 1000
conl
fisl
euAll <- EvalOutput(euAll)
replaces <- list(
c("Chlorinated dibenzo-p-dioxins", "TEQdx"),
c("Chlorinated dibenzofurans", "TEQdx"),
c("Mono-ortho–substituted PCBs", "TEQpcb"),
c("Non-ortho–substituted PCBs", "TEQpcb")
)
for(i in 1:length(replaces)) {
levels(euAll$Compound)[replaces[[i]][1]] <- replaces[[i]][2]
}
euRatio <- EvalOutput(euRatio)
# Hierarchical Bayes model.
# PCDD/F concentrations in fish.
# It uses the sum of PCDD/F (Pcdsum) as the total concentration of dioxin in fish.
# Cong_j is the fraction of a congener from pcdsum.
# pcdsum is log-normally distributed. cong_j follows Dirichlet distribution.
# pcdsum depends on age of fish, fish species and catchment area, but we only have species now so other variables are omitted.
# cong_j depends on fish species.
if(FALSE) { # Multicongener model NOT updated to reflect new names: eu -> euMain
conl <- as.character(unique(eu@output$Congener))
fisl <- sort(as.character(unique(eu@output$Fish)))
C <- length(conl)
Fi <- length(fisl)
N <- 1000
conl
fisl
fishsamples <- reshape(
eu@output,
v.names = "euResult",
idvar = "THLcode",
timevar = "Congener",
drop = c("Matrix", "euSource"),
direction = "wide"
)
# Find the level of quantification for dinterval function
LOQ <- unlist(lapply(fishsamples[3:ncol(fishsamples)], FUN = function(x) min(x[x!=0])))
names(LOQ) <- conl
cong <- data.matrix(fishsamples[3:ncol(fishsamples)])
cong <- sapply(1:length(LOQ), FUN = function(x) ifelse(cong[,x]==0, 0.5*LOQ[x], cong[,x]))
mod <- textConnection("
model{
for(i in 1:S) { # s = fish sample
# below.LOQ[i,j] ~ dinterval(-cong[i,j], -LOQ[j])
cong[i,1:C] ~ dmnorm(mu[fis[i],], Omega[fis[i],,])
}
for(i in 1:Fi) { # Fi = fish species
for(j in 1:C) {
mu[i,j] ~ dnorm(mu1[j], tau1[j])
}
Omega[i,1:C,1:C] ~ dwish(Omega0[1:C,1:C],S)
pred[i,1:C] ~ dmnorm(mu[i,], Omega[i,,]) # Model prediction
}
for(i in 1:C) { # C = Compound
mu1[i] ~ dnorm(0, 0.0001)
tau1[i] ~ dunif(0,10000)
pred1[i] ~ dnorm(mu1[i], tau1[i])
}
Omega1[1:C,1:C] ~ dwish(Omega0[1:C,1:C],S)
}
")
jags <- jags.model(
mod,
data = list(
S = nrow(fishsamples),
C = C,
Fi = Fi,
cong = log(cong),
# LOQ = LOQ,
# below.LOQ = is.na(cong)*1,
fis = match(fishsamples$Fish, fisl),
Omega0 = diag(C)/100000
),
n.chains = 4,
n.adapt = 100
)
} # if(FALSE)
fishsamples <- unkeep(euAll, prevresults = TRUE, sources = TRUE)@output
fishsamples <- fishsamples[fishsamples$Compound %in% conl & fishsamples$Matrix == "Muscle" , ]
fishsamples <- reshape(
fishsamples,
v.names = "euAllResult",
idvar = "THLcode",
timevar = "Compound",
drop = c("Matrix"),
direction = "wide"
)
# colnames(euAll@output)
# "THLcode" "Matrix" "Compound" "Fish" "euRawSource"
# [6] "TEFversion" "TEFrawSource" "TEFSource" "Source" "euAllResult"
# [11] "euAllSource"
oprint(head(fishsamples))
# Find the level of quantification for dinterval function
LOQ <- unlist(lapply(fishsamples[3:ncol(fishsamples)], FUN = function(x) min(x[x!=0])))
names(LOQ) <- conl
cong <- data.matrix(fishsamples[3:ncol(fishsamples)])
cong <- sapply(1:length(LOQ), FUN = function(x) ifelse(cong[,x]==0, 0.5*LOQ[x], cong[,x]))
mod <- textConnection("
model{
for(i in 1:S) { # s = fish sample
# below.LOQ[i,j] ~ dinterval(-cong[i,j], -LOQ[j])
cong[i,1:C] ~ dmnorm(mu[fis[i],], Omega[fis[i],,])
}
for(i in 1:Fi) { # Fi = fish species
for(j in 1:C) {
mu[i,j] ~ dnorm(mu1[j], tau1[j])
}
Omega[i,1:C,1:C] ~ dwish(Omega0[1:C,1:C],S)
pred[i,1:C] ~ dmnorm(mu[i,], Omega[i,,]) # Model prediction
}
for(i in 1:C) { # C = Compound
mu1[i] ~ dnorm(0, 0.0001)
tau1[i] ~ dunif(0,10000)
pred1[i] ~ dnorm(mu1[i], tau1[i])
}
Omega1[1:C,1:C] ~ dwish(Omega0[1:C,1:C],S)
}
")
jags <- jags.model(
mod,
data = list(
S = nrow(fishsamples),
C = C,
Fi = Fi,
cong = log(cong),
# LOQ = LOQ,
# below.LOQ = is.na(cong)*1,
fis = match(fishsamples$Fish, fisl),
Omega0 = diag(C)/100000
),
n.chains = 4,
n.adapt = 100
)
update(jags, 100)
samps.j <- jags.samples(
jags,
c('mu', 'Omega', 'pred', 'mu1', 'Omega1', 'tau1', 'pred1'),
N
)
dimnames(samps.j$mu) <- list(Fish = fisl, Compound = conl, Iter = 1:N, Chain = 1:4)
dimnames(samps.j$mu1) <- list(Compound = conl, Iter = 1:N, Chain = 1:4)
dimnames(samps.j$pred) <- list(Fish = fisl, Compound = conl, Iter = 1:N, Chain = 1:4)
dimnames(samps.j$mu1) <- list(Compound = conl, Iter = 1:N, Chain = 1:4)
dimnames(samps.j$tau1) <- list(Compound = conl, Iter = 1:N, Chain = 1:4)
dimnames(samps.j$pred1) <- list(Compound = conl, Iter = 1:N, Chain = 1:4)
dimnames(samps.j$Omega) <- list(Fish = fisl, Compound = conl, Compound2 = conl, Iter=1:N, Chain=1:4)
dimnames(samps.j$Omega1) <- list(Compound = conl, Compound2 = conl, Iter=1:N, Chain=1:4)
##### conc.param contains expected values of the distribution parameters from the model
conc.param <- list(
mu = apply(samps.j$mu[,,,1], MARGIN = 1:2, FUN = mean),
Omega = apply(samps.j$Omega[,,,,1], MARGIN = 1:3, FUN = mean),
pred.mean = apply(samps.j$pred[,,,1], MARGIN = 1:2, FUN = mean),
pred.sd = apply(samps.j$pred[,,,1], MARGIN = 1:2, FUN = sd),
mu1 = apply(samps.j$mu1[,,1], MARGIN = 1, FUN = mean),
tau1 = apply(samps.j$tau1[,,1], MARGIN = 1, FUN = mean),
pred1.mean = apply(samps.j$pred1[,,1], MARGIN = 1, FUN = mean),
pred1.sd = apply(samps.j$pred1[,,1], MARGIN = 1, FUN = sd)
)
objects.store(conc.param, samps.j)
cat("Lists conc.params and samps.j stored.\n")
######################3
cat("Descriptive statistics:\n")
euRatio <- EvalOutput(euRatio)
ggplot(euAll@output, aes(x = euAllResult, colour = Fish))+geom_density()+
facet_wrap(~ Compound) + scale_x_log10()
ggplot(euRatio@output, aes(x = euRatioResult, colour = Fish))+geom_density()+
facet_wrap(~ Compound)
# Predictions for all congeners of fish1 (Baltic herring)
scatterplotMatrix(t(samps.j$pred[1,,,1]))
# Means for all congeners of the generic fish
scatterplotMatrix(t(samps.j$mu1[,,1]))
# Prediction for all congeners of the generic fish
scatterplotMatrix(t(samps.j$pred1[,,1]))
# Predictions for all fish species for TCDD
scatterplotMatrix(t(samps.j$pred[,1,,1]))
# Predictions for pike for omegas and PeCDD
scatterplotMatrix(t(samps.j$Omega[6,2,,,1]))
ggplot(melt(exp(samps.j$pred[,,,1])), aes(x=value, colour=Compound))+geom_density()+
facet_wrap( ~ Fish,scales = "free_y")+scale_x_log10()
ggplot(eu@output, aes(x = euResult, colour=Congener))+geom_density()+
facet_wrap( ~ Fish, scales = "free_y")+scale_x_log10()
#stat_ellipse()
coda.j <- coda.samples(
jags,
c('mu', 'pred', 'omega1', 'pred1'),
N
)
plot(coda.j)
#################
euAll <- euRaw[, c(1:4, 18, 19)]
colnames(euAll@output)[1:4] <- c("THLcode", "Matrix", "Compound",
"Fish")
if (exists("TEQgroups"))
levels(TEF$Group) <- TEQgroups$Out
temp <- oapply(euAll * TEF, cols = "Compound", FUN = "sum")
colnames(temp@output)[colnames(temp@output) == "Group"] <- "Compound"
eu2 <- combine(euAll, temp)
return(eu2)
| |