--
Oh, I suppose the distinction is between finite sample inference on the correlation between parameters (the parameters were correlated in these years with data) and otherwise (over many many years not yet sampled we expect the parameters will be correlated as…).
On Feb 21, 2025, at 4:25 PM, 'John C' via hmecology: Hierarchical Modeling in Ecology <hmec...@googlegroups.com> wrote:
Oh, I see what Tomas means just playing around with this quickly. I've repeated the following a few times with different nData and so forth. Yeah, seems like one should probably should use the correlation across the posterior of posterior-predicted parameters. Kind of like a true Monte Carlo assessment or parametric bootstrap or something.
John
###Start by simulating "parameters"
set.seed(8675309)
np <- 15
r <- 0.25 # pearson r
X <- rmvnorm(n, sigma = matrix(c(1, r, r, 1), 2))
X2<-plogis(X)
cor.test(X[,1], X[,2]) ###0.033, 95 %CI -0.49 - 0.54
cor.test(X2[,1], X2[,2]) ###0.042, 95 %CI -0.48 - 0.54
###Let's say X2[, 1] is survival probability,
###and X2[, 2] is...the probability of a successful nest or something.
nData<-1000
###we'll make the simplifying assumption that in every year,
###we observe an independent set of 100 birds that survive or do not
###and have a successful nest or not.
Data1<-matrix(NA, nData, np)
Data2<-matrix(NA, nData, np)
for (i in 1:np){
Data1[, i]<-rbinom(nData, 1, X2[i, 1]) ##survived or not
Data2[, i]<-rbinom(nData, 1, X2[i, 2]) ##nest or not
}
library(nimble)
blah<-nimbleCode({
mu.phi~dlogis(0, 1)
sigma.phi~dunif(0, 3)
mu.gamma~dlogis(0, 1)
sigma.gamma~dunif(0, 3)
for (i in 1:15){
logit(phi[i])~dnorm(mu.phi, sd=sigma.phi)
logit(gamma[i])~dnorm(mu.gamma, sd=sigma.gamma)
logit(simphi[i])~dnorm(mu.phi, sd=sigma.phi)
logit(simgamma[i])~dnorm(mu.gamma, sd=sigma.gamma)
for (j in 1:1000){
Data1[j, i]~dbern(phi[i])
Data2[j, i]~dbern(gamma[i])
}
}
})
Data=list(Data1=Data1, Data2=Data2)
Sim2<- nimbleModel(code = blah, name = "blah", constants = NULL,
data = Data, inits = NULL, calculate=FALSE)
SimMCMC2<-configureMCMC(Sim2, monitors=c("phi", "gamma", "simphi", "simgamma"), useConjugacy = FALSE)
SimRMCMC2<-buildMCMC(SimMCMC2)
SimCMCMMC2<-compileNimble(SimRMCMC2,Sim2)
samples<-runMCMC(mcmc=SimCMCMMC2$SimRMCMC2, niter=20000, nburnin = 10000, thin=20, nchains=1, samplesAsCodaMCMC=FALSE)
corMCMC<-rep(NA, 500)
for (i in 1:500){ ###number of stored MCMC samples
corMCMC[i]<-cor.test(samples[i, 1:15], samples[i, 16:30])$estimate
}
quantile(corMCMC, probs=c(.025, .5, .975)) ###too narrow
corMCMC2<-rep(NA, 500)
for (i in 1:500){ ###number of stored MCMC samples
corMCMC2[i]<-cor.test(samples[i, 31:45], samples[i, 46:60])$estimate
}
quantile(corMCMC2, probs=c(.025, .5, .975)) ###about right.
--On Friday, February 21, 2025 at 12:44:33 PM UTC-7 hender...@gmail.com wrote:
Hi all,
Tomas, this is an interesting point you’ve brought up. I thought I'd throw in my 2 cents.
If i'm correct, your example reflects the extremely small measurement error (sd = 0.05) assigned to the 15 data points. If you check the standard deviation of each point’s values across your 5000 MCMC draws (column-wise SDs of ~0.048–0.052), you’ll find almost no variation from one draw to another. In other words, if the model believes each point is pinned down with minimal measurement error, it will naturally infer that the correlation implied by those 15 points is quite certain.
Frequentistically, cor.test() on 15 data points yields a wide confidence interval for the sample correlation i.e., “if we repeatedly collected new datasets of size 15, how variable would the estimated correlation be?” By contrast, a Bayesian approach (like Kéry & Schaub) asks, “Given the model, data, and pzriors, how plausible are different correlation values?” If the model is told there’s almost no uncertainty in each observation, it makes sense how it could produce narrowly distributed posterior for the correlation, even with just 15 points.
I don't think your toy example necessarily show that the BPA 2012 approach is wrong. Instead, i think it just highlights that extremely small measurement error leads to overly confident posterior inferences. Once you allow more realistic measurement error (or other relevant variance components), the distribution of correlation values will widen and better reflect the true uncertainty.
RE: your concern about p‐values and the probability that a correlation is positive or negative. A Bayesian workflow does let you determine how often the posterior distribution of the correlation falls above/below zero. Unlike a single “mean correlation,” you get a distribution of correlation values —one for each posterior draw- which directly answers whether the correlation is likely positive, negative, or near zero. There’s no need for a separate “SE of correlation” inflation step, because the Bayesian draws already capture all uncertainty (provided the model itself is realistic..).
Finally, I like Matt's comment suggesting a more thorough simulation study!
Nick
On Friday, February 21, 2025 at 10:48:24 AM UTC-8 matthijs....@gmail.com wrote:
Hey Tomas,
I think I understand what you're saying. I believe one way of framing it is that you don't think the posterior distribution over the empirical correlation coefficient (i.e., computing the coefficient for each posterior draw) gives you adequate uncertainty over the correlation. This may be correct, but I'm not sure; in some ways it is a hacky solution. But like I said, if you are interested in formally modeling the correlation, assuming your rates (or link-function transformed versions thereof) are multivariate normal distributed will let you actually estimate those correlations, albeit under that assumed parametric model.
What I would suggest is simulating bivariate normal data, then analysing those in (1) a bivariate normal model where you estimate the correlation as a parameter (i.e., the exact data-generating process) and (2) a model with two univariate normal parameters and then estimating the correlation coefficient in the method described in BPA. That would be a better way to test this issue compared to the simulation you provided. This is roughly what John has suggested as well, I believe.
If you do end up doing this, I'd be keen to see the results.
Cheers,
Matt
On Fri, Feb 21, 2025 at 10:04 PM Tomas T. <tomas.t...@gmail.com> wrote:
Hi Matthijs,
You're not interested in the frequentist 95% CI around the correlation coefficient of your sample, in this case the 2 vectors of length 15 that you simulated. The correlation coefficient is just a single value that you're computing for each MCMC sample from your posterior.
No, wrong. Most of the people (that includes the cited references, and also other studies which used this method) are not interested just in a single value of the correlation coefficient. Most people are interested in inference, that is, the probability that the correlation coefficient is positive/negative/different from zero, which is what you can do with the CI of the correlation coefficient returned by cor.test() - and which you aim to do with your resultant posterior distribution. Or, practically equivalently, in frequentistic world, people are interested in p-value - what is the probability that they mistakenly reject the null hypothesis (correlation equals to zero) when it is true.Therefore, when you do this for each MCMC sample, you have draws from the posterior of the correlation between your time varying parameters.
No. Using this method, you have posterior of the mean estimate of the correlation coefficient, which is useless, because you cannot use it for inference of the probability that the correlation coefficient is positive/negative/different from zero. Which is, again, what the cited references and other studies using this method aim to do.
The example I provided should clearly demonstrate how this method could lead to wrong inference and thus wrong conclusions.
Tomas
Re: the correlation matrix, I don't think it would slow down too much. If you just model the varying rates as univariate normal distributions, this implies a prior distribution that there's 0 correlation between the parameters. If you model it multivariate normal, you can still express an uninformative prior over the correlation matrix, but at least your prior is that the rates may be correlated.
Hope this helps.
Matt
On Fri, Feb 21, 2025 at 4:36 AM Tomas T. <tomas.t...@gmail.com> wrote:
Dear Matthijs,
answering below:
On Thu, 20 Feb 2025 at 17:46, Matthijs Hollanders <matthijs....@gmail.com> wrote:
You're simulating very little random noise around your simulated X with your "MCMC samples", which is what's reflected in your "CI".
Of course, that was the intention and that's the point, to provide an example that would clearly demonstrate that the method is in principle wrong. Doesn't matter if it is realistic or not. It clearly demonstrates that the method ignores the variance of the correlation statistics, which leads to wrong inference.
Btw, if you want to directly estimate correlations, you can model the two (or more) time-varying demographic parameters as multivariate normal and estimate the correlation coefficient(s).
True, you could do that, then, if you have e.g. 4 parameters, you would have to estimate the whole correlation matrix... I am not sure how much it would slow down the model fit though. And what would the change of the model mean - perhaps also the estimates might be different then?
Regards,
Tomas
--On Thursday, February 20, 2025 at 11:43:50 PM UTC+11 tomas.t...@gmail.com wrote:
Hello all,
suppose you want to calculate correlation on two variables estimated by a model fitted in bayesian software. For example, a model that calculated two demographic parameters over years, and you have the posterior samples. How to do this?
Until now, I heavily used the method recommended in Bayesian Population Analysis using WinBUGS (Kery & Schaub 2012, code for Fig. 11-8 (Web appendix 1)), which calculates the pearson correlation coefficient for each posterior sample and then uses that as posterior distribution of the correlation. I thought this method is neat. Only now I realized this method is probably wrong?! It does take into account the uncertainty of the estimated parameters, but it completely ignores the uncertainty of the pearson correlation coefficient itself. So, if you have relatively precise estimates of the parameters, it can make very weak correlations appear very strong! I will illustrate it by an example below.
For me this realization is relatively serious, as I already used it in a published paper. Perhaps this is also case of the cited paper Schaub et al (2011)? I understand mistakes like this happen, I make them myself, but I think it would deserve some big red warning so that no more people make this mistake.
Have you used this method yourself? Or are you using a different one? How do you solve this? Below, I also proposed a remedy to this - instead of taking just the mean estimate of pearson, sample from its error distribution. I am not 100% sure this is statistically correct though.
Please let me know!
Best regards,
Tomas
PS: Here is a reproducible code that demonstrates the problem:
library(mvtnorm)
### generate two correlated vectors - true values
set.seed(123)
n <- 15
r <- 0.25 # pearson r
X <- rmvnorm(n, sigma = matrix(c(1, r, r, 1), 2))
cor.test(X[,1], X[,2])
# r = 0.1944498
# you see the CI is very wide: -0.3529724 to 0.6426943
# which means very weak correlation
### simulate the "MCMC samples" of these two vectors, with small uncertainty
samp <- 5000
A <- matrix(rnorm(n*samp, X[,1], 0.05), samp, byrow = TRUE)
B <- matrix(rnorm(n*samp, X[,2], 0.05), samp, byrow = TRUE)
### Calculate correlation coefficients as in BPA 2012, Code for Fig. 11-8 (Web appendix 1)
correl <- c()
for (i in 1:samp) {
correl[i] <- cor(A[i,], B[i,])
}
# Credible intervals of correlation coefficients
quantile(correl, c(0.05, 0.5, 0.95))
# the resultant CI is very narrow - i.e. the correlation suddenly seems super stong:
# 5% 50% 95%
# 0.1628700 0.1936207 0.2257177## This is definitely wrong, as we know that the true correlation is very weak!
####################################################
# Could this be a possible remedy? I am not sure:
# to sample not just mean value, but from the probability distribution of each statistics
# (it is also an approximation, since the distribution of Pearson is not exactly normal, see
# https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#Using_the_exact_distribution
correl2 <- c()
for (i in 1:samp) {
st <- cor.test(A[i,], B[i,])
correl2[i] <- rnorm(1, st$estimate, diff(st$conf.int)/1.96/2)
}
quantile(correl2, c(0.05, 0.5, 0.95))
*** Three hierarchical modeling email lists ***
(1) unmarked: for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology (this list): for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2016, 2021) and Schaub & Kéry (2022)
---
You received this message because you are subscribed to the Google Groups "hmecology: Hierarchical Modeling in Ecology" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hmecology+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/hmecology/20b2720e-f495-4de3-b260-5cd6ee98b1fcn%40googlegroups.com.
*** Three hierarchical modeling email lists ***
(1) unmarked: for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology (this list): for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2016, 2021) and Schaub & Kéry (2022)
---
You received this message because you are subscribed to the Google Groups "hmecology: Hierarchical Modeling in Ecology" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hmecology+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/hmecology/ab522e71-b15a-471c-ac6c-35fab77d4a53n%40googlegroups.com.
*** Three hierarchical modeling email lists ***
(1) unmarked: for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology (this list): for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2016, 2021) and Schaub & Kéry (2022)
---
You received this message because you are subscribed to the Google Groups "hmecology: Hierarchical Modeling in Ecology" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hmecology+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/hmecology/45C15B2C-3339-498F-B148-C126A84C2474%40wisc.edu.
To view this discussion visit https://groups.google.com/d/msgid/hmecology/CAGy9Uw8KWb7Y0X66uoEbERBSUD-49ebc0%2BXAnzPDVxhzYTtDEQ%40mail.gmail.com.
--
*** Three hierarchical modeling email lists ***
(1) unmarked: for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology (this list): for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2016, 2021) and Schaub & Kéry (2022)
---
You received this message because you are subscribed to the Google Groups "hmecology: Hierarchical Modeling in Ecology" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hmecology+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/hmecology/483a9a22-e950-4d8a-aff2-8da12dc5697fn%40googlegroups.com.