-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_v5.R
39 lines (31 loc) · 1.88 KB
/
functions_v5.R
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
# Title: "Quantifying the effects of the mitochondrial genome on milk production traits in dairy cows: empirical results and modelling challenges"
# Authors: Vladimir Brajkovic, Ivan Pocrnic, Miroslav Kaps, Marija Špehar, Vlatka Cubric-Curik, Strahil Ristov, Dinko Novosel, Gregor Gorjanc, Ino Curik
# DOI: 10.3168/jds.2024-25203
# Please cite: Brajkovic et al., 2024
# Functions needed for INLA analyses
# Update: April 2024
SummarizeFun = function(x, Quantiles = c(0.025, 0.975)) {
c(mean(x), sd(x), quantile(x, probs = Quantiles, na.rm=TRUE))
}
SummarizeInlaVars = function(x, nSamples = 10000) {
# Summarize INLA effects "precisions" in form of Standard deviations, Variances, and Proportions (var / sum(all vars))
Terms = names(x$marginals.hyperpar)
Terms = Terms[grepl(pattern = "Precision for ", x = Terms)]
TermsShort = sub(pattern = "Precision for ", replacement = "", x = Terms)
TermsShort = sub(pattern = "the ", replacement = "", x = TermsShort)
nTerms = length(Terms)
Samples = matrix(data = numeric(), nrow = nSamples, ncol = nTerms + 1)
Out = vector(mode = "list", length = 3)
names(Out) = c("Sd", "Var", "Proportion")
Out[[1]] = Out[[2]] = Out[[3]] = matrix(data = numeric(), nrow = nTerms + 1, ncol = 4)
dimnames(Out[[1]]) = dimnames(Out[[2]]) = dimnames(Out[[3]]) = list(c(TermsShort, "Total"), c("Mean", "Sd", "Q0.025", "Q0.975"))
for (Term in 1:nTerms) {
# Term = 3
Samples[, Term] = 1 / inla.rmarginal(n = nSamples, marginal = x$marginals.hyperpar[[Terms[Term]]])
}
Samples[, Term + 1] = rowSums(x = Samples[, 1:nTerms, drop = FALSE])
Out$Var[] = t(apply(X = Samples, MARGIN = 2, FUN = SummarizeFun))
Out$Sd[] = t(apply(X = sqrt(Samples), MARGIN = 2, FUN = SummarizeFun))
Out$Proportion[] = t(apply(X = Samples / Samples[, nTerms + 1], MARGIN = 2, FUN = SummarizeFun))
return(Out)
}