library(MASS)
library(rjags)
Loading required package: coda
Linked to JAGS 4.3.0
Loaded modules: basemod,bugs
We use a logistic function and reflect it to make it decrease with increasing fruits at year n-1.
# Logistic function: y goes from 0 to maximum as x increases
logistic <- function(x, midpoint, maximum=1, steepness=0.1) {
y = maximum/(1+exp(-steepness*(x-midpoint)))
return(y)
}
# Reflected logistic function: y goes from maximum to 0 as x increases
ref_logistic <- function(x, midpoint, maximum=1, steepness=0.1) {
return(maximum - logistic(x, midpoint, maximum, steepness))
}
x = (1:1000)*2
plot(x, ref_logistic(x, midpoint=500), t="l")
# Simulates fructification at a given year with a simple model that depends on the previous year's number of fruits only.
# This dependency is modelled through the ref_logistic function, which provides a probability of making buds p_buds.
simulateFructification <- function (fruits_last_year, midpoint, maximum=1, steepness=0.1, meanFruits=1000, theta_fruits=10) {
p_buds = ref_logistic(fruits_last_year, midpoint, maximum, steepness)
#print(paste("Probability of getting buds this year: ", p_buds, sep=""))
n_fruits = rnegbin(1, mu=meanFruits * p_buds, theta=theta_fruits)
return(n_fruits)
}
# Simulates fructifications over N years, by calling iteratively simulateFructification.
simulateNYearsFructification <- function (Nyears=7, midpoint=500, maximum=1, steepness=0.1, meanFruits=1000, theta_fruits=10) {
fruits = c()
# we arbitrarily start assuming there had been fruits the year preceding the samples
fruits = c(fruits, rnegbin(1, mu=meanFruits, theta=theta_fruits))
for (year in 1:Nyears) {
fruits = c(fruits, simulateFructification(fruits[length(fruits)], midpoint, maximum, steepness, meanFruits, theta_fruits))
}
return(fruits)
}
# Simulates fructifications over N years and in several areas, by calling iteratively simulateNYearsFructification.
simulateNAreas <- function(Nareas=15) {
all_fruits = c()
for (area in 1:Nareas) {
all_fruits = rbind(all_fruits, simulateNYearsFructification())
}
return(all_fruits)
}
fruits = simulateNYearsFructification()
plot(fruits, xlab="Year")
fruits = simulateNAreas()
plot(x=1:8, y=fruits[1,], xlab="Year", ylab="Number of fruits", t="l", col=0, ylim=c(0,2000))
for (i in 2:dim(fruits)[1]) {
lines(fruits[i,], col=i)
}
We are going to use Jags to estimate the parameters of a model that matches the simulation model. The distribution dnegbin in Jags is parametrized in p and s, whereas R’s rnegbin is parametrized in mu (the mean) and theta. The relationships between those parameters are as follows: - s = theta - mu = s(1-p)/p i.e. p = s/(mu+s) In the simulation, we have: mu = p_buds * p1 * meanFruits We want to have these parameters in the Jags estimation model to be able to compare the estimated to the true values: the estimation model has to match the simulation model.
jags_model <-
"model
{
for (area in 1:Nareas)
{
# special case: year 1
fruits[area, 1] ~ dnegbin(min(scalePrior/(mu[area, 1]+scalePrior), 0.999), scalePrior)
mu[area, 1] <- pBuds[area, 1] * meanFruits
pBuds[area, 1] ~ dbeta(1.0,1.0)
# years 2 and following
for (year in 2:Nyears)
{
fruits[area, year] ~ dnegbin(min(scalePrior/(mu[area, year]+scalePrior), 0.999), scalePrior)
mu[area,year] <- pBuds[area, year] * meanFruits
pBuds[area, year] <- 1.0 - 1.0/(1+exp(-steepness*(fruits[area, year-1]-midpoint)))
}
}
scalePrior ~ dunif(0.1, 5000)
meanFruits ~ dunif(5, 5000)
midpoint ~ dunif(0, 5000)
steepness ~ dunif(0, 5)
}"
dcomplete = list(fruits=fruits, Nareas=dim(fruits)[1], Nyears=dim(fruits)[2])
ini <- list(list(scalePrior=1, meanFruits = 500, midpoint = 500, steepness = 0.1),
list(scalePrior=10, meanFruits = 1000, midpoint = 1000, steepness = 1.0),
list(scalePrior=100, meanFruits = 100, midpoint = 100, steepness = 4.0))
m1<-jags.model(file = textConnection(jags_model), data = dcomplete, inits = ini, n.chains = 3)
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 120
Unobserved stochastic nodes: 19
Total graph size: 808
Initializing model
|
| | 0%
|
|+ | 2%
|
|++ | 4%
|
|+++ | 6%
|
|++++ | 8%
|
|+++++ | 10%
|
|++++++ | 12%
|
|+++++++ | 14%
|
|++++++++ | 16%
|
|+++++++++ | 18%
|
|++++++++++ | 20%
|
|+++++++++++ | 22%
|
|++++++++++++ | 24%
|
|+++++++++++++ | 26%
|
|++++++++++++++ | 28%
|
|+++++++++++++++ | 30%
|
|++++++++++++++++ | 32%
|
|+++++++++++++++++ | 34%
|
|++++++++++++++++++ | 36%
|
|+++++++++++++++++++ | 38%
|
|++++++++++++++++++++ | 40%
|
|+++++++++++++++++++++ | 42%
|
|++++++++++++++++++++++ | 44%
|
|+++++++++++++++++++++++ | 46%
|
|++++++++++++++++++++++++ | 48%
|
|+++++++++++++++++++++++++ | 50%
|
|++++++++++++++++++++++++++ | 52%
|
|+++++++++++++++++++++++++++ | 54%
|
|++++++++++++++++++++++++++++ | 56%
|
|+++++++++++++++++++++++++++++ | 58%
|
|++++++++++++++++++++++++++++++ | 60%
|
|+++++++++++++++++++++++++++++++ | 62%
|
|++++++++++++++++++++++++++++++++ | 64%
|
|+++++++++++++++++++++++++++++++++ | 66%
|
|++++++++++++++++++++++++++++++++++ | 68%
|
|+++++++++++++++++++++++++++++++++++ | 70%
|
|++++++++++++++++++++++++++++++++++++ | 72%
|
|+++++++++++++++++++++++++++++++++++++ | 74%
|
|++++++++++++++++++++++++++++++++++++++ | 76%
|
|+++++++++++++++++++++++++++++++++++++++ | 78%
|
|++++++++++++++++++++++++++++++++++++++++ | 80%
|
|+++++++++++++++++++++++++++++++++++++++++ | 82%
|
|++++++++++++++++++++++++++++++++++++++++++ | 84%
|
|+++++++++++++++++++++++++++++++++++++++++++ | 86%
|
|++++++++++++++++++++++++++++++++++++++++++++ | 88%
|
|+++++++++++++++++++++++++++++++++++++++++++++ | 90%
|
|++++++++++++++++++++++++++++++++++++++++++++++ | 92%
|
|+++++++++++++++++++++++++++++++++++++++++++++++ | 94%
|
|++++++++++++++++++++++++++++++++++++++++++++++++ | 96%
|
|+++++++++++++++++++++++++++++++++++++++++++++++++ | 98%
|
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100%
# burnin
update(m1, 5000)
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
# sampling
mcmc1 <- coda.samples(m1, c("meanFruits", "midpoint", "steepness", "scalePrior"), n.iter = 10000, thin = 5)
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
gelman.diag(mcmc1)
Potential scale reduction factors:
Point est. Upper C.I.
meanFruits 1 1
midpoint 1 1
scalePrior 1 1
steepness 1 1
Multivariate psrf
1
The potential scale reduction factors seem close to 1.0 for both the mean and the upper C.I., which is a good sign that all 3 chains converged to similar posterior densities.
effectiveSize(mcmc1)
meanFruits midpoint scalePrior steepness
5868.638 2271.160 5825.108 2137.073
It would seem like we have enough independent samples to estimate the posterior distributions well.
plot(mcmc1)
We don’t see much, we’ll look at each variable in turn.
plot(mcmc1[,c("meanFruits")])
summary(mcmc1[,c("meanFruits")])
Iterations = 16005:26000
Thinning interval = 5
Number of chains = 3
Sample size per chain = 2000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
1060.4967 48.5300 0.6265 0.6341
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
969.4 1027.1 1058.3 1091.8 1160.8
Not too far from the true value which is 1000.
plot(mcmc1[,c("midpoint")])
summary(mcmc1[,c("midpoint")])
Iterations = 16005:26000
Thinning interval = 5
Number of chains = 3
Sample size per chain = 2000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
529.3575 30.2216 0.3902 0.6382
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
471.5 503.9 535.6 557.9 565.1
Not too far from the true value of 500, but the shape of the distribution is a bit surprising…
plot(mcmc1[,c("steepness")])
summary(mcmc1[,c("steepness")])
Iterations = 16005:26000
Thinning interval = 5
Number of chains = 3
Sample size per chain = 2000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
1.89583 1.49891 0.01935 0.03273
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
0.08961 0.49946 1.57574 3.11968 4.78204
Far from the true value of 0.1! There may not be much information in the data to estimate the steepness parameter, or maybe there is a correlation between two parameters.
plot(mcmc1[,c("scalePrior")])
summary(mcmc1[,c("scalePrior")])
Iterations = 16005:26000
Thinning interval = 5
Number of chains = 3
Sample size per chain = 2000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
8.98131 1.66137 0.02145 0.02179
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
6.055 7.804 8.857 10.030 12.579
Not too far from the true value which was 10.
crosscorr.plot(mcmc1)
There does not seem to be strong correlations between the variables.
We use a linear function of the absolute distance to the optimum amount of precipitation that returns values between 0 and 1.
abs_dist <- function(x, optimum=50) {
return (1-abs(x-50)/50)
}
x=1:100
plot(abs_dist(x), t="l", ylab="Probability of making fruits", xlab="Precipitation")
# Simulates fructification at a given year with a model that depends on the previous year's number of fruits and the precipitations at year n.
# This dependency between years is modelled through the ref_logistic function, which provides a probability of making buds p_buds.
# Then we use abs_dist to get a probability p_1 to get fruits from buds, based on precipitation at year n.
simulatePrecipitations <- function(Nyears=7) {
return(runif(Nyears, min=0, max=100))
}
simulateFructification <- function (fruits_last_year, precipitation, midpoint, maximum=1, steepness=0.1, meanFruits=1000, theta_fruits=10) {
p_buds = ref_logistic(fruits_last_year, midpoint, maximum, steepness)
p1 = abs_dist(precipitation)
#print(paste("Probability of getting buds this year: ", p_buds, sep=""))
n_fruits = rnegbin(1, mu=meanFruits * p_buds * p1, theta=theta_fruits)
return(n_fruits)
}
# Simulates fructifications over N years, by calling iteratively simulateFructification.
simulateNYearsFructification <- function (Nyears=7, midpoint=500, maximum=1, steepness=0.1, meanFruits=1000, theta_fruits=10) {
# We simulate precipitations
precipitations = c(50)
precipitations = c(precipitations, simulatePrecipitations(Nyears))
fruits = c()
# we arbitrarily start assuming there had been fruits the year preceding the samples, with the best amount of precipitation
fruits = c(fruits, rnegbin(1, mu=meanFruits, theta=theta_fruits))
for (year in 1:Nyears) {
fruits = c(fruits, simulateFructification(fruits[length(fruits)], precipitations[year], midpoint, maximum, steepness, meanFruits, theta_fruits))
}
return(list(precipitations, fruits))
}
# Simulates fructifications over N years and in several areas, by calling iteratively simulateNYearsFructification.
simulateNAreas <- function(Nareas=15) {
all_fruits = c()
all_precipitations=c()
for (area in 1:Nareas) {
precipitations_fruits_li = simulateNYearsFructification()
all_precipitations = rbind(all_precipitations, precipitations_fruits_li[[1]])
all_fruits = rbind(all_fruits, precipitations_fruits_li[[2]])
}
return(list(all_precipitations, all_fruits))
}
precipitations_fruits_li = simulateNYearsFructification()
par(mfrow=c(2,1), mar=c(3, 4, 0, 2))
plot(x=1:8, precipitations_fruits_li[[1]], ylab="Precipitations", xlab="", pch=20)
plot(x=1:8, precipitations_fruits_li[[2]], ylab="Number of fruits", xlab="year", pch=20)
precipitations_all_fruits_li <- simulateNAreas()
all_precipitations <- precipitations_all_fruits_li[[1]]
all_fruits <- precipitations_all_fruits_li[[2]]
jags_model_2 <-
"model
{
for (area in 1:Nareas)
{
# special case: year 1
fruits[area, 1] ~ dnegbin(min(scalePrior/(mu[area, 1]+scalePrior), 0.999), scalePrior)
mu[area, 1] <- pBuds[area, 1] * meanFruits * p1[area, 1]
pBuds[area, 1] ~ dbeta(1.0,1.0)
p1[area, 1] <- 1-abs(precipitation[area,1]-optimalPrecipitation)/(max(100-optimalPrecipitation, optimalPrecipitation))
# years 2 and following
for (year in 2:Nyears)
{
fruits[area, year] ~ dnegbin(min(scalePrior/(mu[area, year]+scalePrior), 0.999), scalePrior)
mu[area,year] <- pBuds[area, year] * meanFruits * p1[area, year]
pBuds[area, year] <- 1.0 - 1.0/(1+exp(-steepness*(fruits[area, year-1]-midpoint)))
p1[area, year] <- 1-abs(precipitation[area,1]-optimalPrecipitation)/(max(100-optimalPrecipitation, optimalPrecipitation))
}
}
optimalPrecipitation ~ dunif(0,100)
scalePrior ~ dunif(0.1, 5000)
meanFruits ~ dunif(5, 5000)
midpoint ~ dunif(0, 5000)
steepness ~ dunif(0, 5)
}"
dcomplete = list(fruits=all_fruits, Nareas=dim(all_fruits)[1], Nyears=dim(all_fruits)[2], precipitation=all_precipitations)
ini <- list(list(optimalPrecipitation=10, scalePrior=1, meanFruits = 500, midpoint = 500, steepness = 0.1),
list(optimalPrecipitation=90, scalePrior=10, meanFruits = 1000, midpoint = 1000, steepness = 1.0),
list(optimalPrecipitation=40, scalePrior=100, meanFruits = 100, midpoint = 100, steepness = 4.0))
m2 <- jags.model(file=textConnection(jags_model_2), data = dcomplete, inits = ini, n.chains = 3)
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 120
Unobserved stochastic nodes: 20
Total graph size: 1026
Initializing model
|
| | 0%
|
|+ | 2%
|
|++ | 4%
|
|+++ | 6%
|
|++++ | 8%
|
|+++++ | 10%
|
|++++++ | 12%
|
|+++++++ | 14%
|
|++++++++ | 16%
|
|+++++++++ | 18%
|
|++++++++++ | 20%
|
|+++++++++++ | 22%
|
|++++++++++++ | 24%
|
|+++++++++++++ | 26%
|
|++++++++++++++ | 28%
|
|+++++++++++++++ | 30%
|
|++++++++++++++++ | 32%
|
|+++++++++++++++++ | 34%
|
|++++++++++++++++++ | 36%
|
|+++++++++++++++++++ | 38%
|
|++++++++++++++++++++ | 40%
|
|+++++++++++++++++++++ | 42%
|
|++++++++++++++++++++++ | 44%
|
|+++++++++++++++++++++++ | 46%
|
|++++++++++++++++++++++++ | 48%
|
|+++++++++++++++++++++++++ | 50%
|
|++++++++++++++++++++++++++ | 52%
|
|+++++++++++++++++++++++++++ | 54%
|
|++++++++++++++++++++++++++++ | 56%
|
|+++++++++++++++++++++++++++++ | 58%
|
|++++++++++++++++++++++++++++++ | 60%
|
|+++++++++++++++++++++++++++++++ | 62%
|
|++++++++++++++++++++++++++++++++ | 64%
|
|+++++++++++++++++++++++++++++++++ | 66%
|
|++++++++++++++++++++++++++++++++++ | 68%
|
|+++++++++++++++++++++++++++++++++++ | 70%
|
|++++++++++++++++++++++++++++++++++++ | 72%
|
|+++++++++++++++++++++++++++++++++++++ | 74%
|
|++++++++++++++++++++++++++++++++++++++ | 76%
|
|+++++++++++++++++++++++++++++++++++++++ | 78%
|
|++++++++++++++++++++++++++++++++++++++++ | 80%
|
|+++++++++++++++++++++++++++++++++++++++++ | 82%
|
|++++++++++++++++++++++++++++++++++++++++++ | 84%
|
|+++++++++++++++++++++++++++++++++++++++++++ | 86%
|
|++++++++++++++++++++++++++++++++++++++++++++ | 88%
|
|+++++++++++++++++++++++++++++++++++++++++++++ | 90%
|
|++++++++++++++++++++++++++++++++++++++++++++++ | 92%
|
|+++++++++++++++++++++++++++++++++++++++++++++++ | 94%
|
|++++++++++++++++++++++++++++++++++++++++++++++++ | 96%
|
|+++++++++++++++++++++++++++++++++++++++++++++++++ | 98%
|
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100%
# burnin
update(m2, 5000)
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
# sampling
mcmc2 <- coda.samples(m2, c("optimalPrecipitation", "scalePrior", "meanFruits", "midpoint", "steepness"), n.iter = 10000, thin = 5)
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
gelman.diag(mcmc2)
Potential scale reduction factors:
Point est. Upper C.I.
meanFruits 1.00 1.00
midpoint 1.01 1.02
optimalPrecipitation 1.03 1.09
scalePrior 1.00 1.00
steepness 1.14 1.27
Multivariate psrf
1.03
Some potential scale reduction factors seem a bit far from 1.0. The steepness in particular may be a bit problematic to estimate.
effectiveSize(mcmc2)
meanFruits midpoint optimalPrecipitation scalePrior steepness
1886.3595 199.2191 478.6848 5331.7645 131.4734
Clearly, midpoint and steepness are difficult to sample. More samples would be needed.
raftery.diag(mcmc2)
[[1]]
Quantile (q) = 0.025
Accuracy (r) = +/- 0.005
Probability (s) = 0.95
You need a sample size of at least 3746 with these values of q, r and s
[[2]]
Quantile (q) = 0.025
Accuracy (r) = +/- 0.005
Probability (s) = 0.95
You need a sample size of at least 3746 with these values of q, r and s
[[3]]
Quantile (q) = 0.025
Accuracy (r) = +/- 0.005
Probability (s) = 0.95
You need a sample size of at least 3746 with these values of q, r and s
So we don’t have enough iterations:
dim(mcmc2[[1]])[1]
[1] 2000
mcmc2 <- coda.samples(m2, c("optimalPrecipitation", "scalePrior", "meanFruits", "midpoint", "steepness"), n.iter = 30000, thin = 5)
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
How are we doing now?
gelman.diag(mcmc2)
Potential scale reduction factors:
Point est. Upper C.I.
meanFruits 1.00 1.00
midpoint 1.00 1.01
optimalPrecipitation 1.00 1.00
scalePrior 1.00 1.00
steepness 1.01 1.02
Multivariate psrf
1
This diagnostic seems good.
raftery.diag(mcmc2)
This one too.
effectiveSize(mcmc2)
meanFruits midpoint optimalPrecipitation scalePrior steepness
20070.6589 985.6415 4469.0865 25149.2075 465.1268
Midpoint and steepness are really difficult to estimate: even though we have sampled for 100 000 iterations, in the end the autocorrelation is so strong it’s as if we only had 465 independent draws from the posterior for steepness! To improve the estimation of these parameters, one could run the chains longer, or change the model, structurally, or with the priors.
crosscorr.plot(mcmc2)
There is a correlation between two variables:
crosscorr(mcmc2)
meanFruits midpoint optimalPrecipitation scalePrior steepness
meanFruits 1.00000000 -0.043662376 -0.012425025 -0.06231696 -0.013420287
midpoint -0.04366238 1.000000000 -0.002899814 -0.15313660 0.665963409
optimalPrecipitation -0.01242503 -0.002899814 1.000000000 0.01274707 -0.008446693
scalePrior -0.06231696 -0.153136600 0.012747075 1.00000000 -0.111870177
steepness -0.01342029 0.665963409 -0.008446693 -0.11187018 1.000000000
0.67: no wonder steepness and midpoint are difficult to estimate: they are strongly correlated.
plot(mcmc2)
We can see on the traces that steepness and midpoint are difficult to sample for the mcmc.
summary(mcmc2)
Iterations = 46005:146000
Thinning interval = 5
Number of chains = 3
Sample size per chain = 20000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
meanFruits 1137.4237 243.0871 0.9923990 1.715768
midpoint 517.7311 27.5409 0.1124353 0.994977
optimalPrecipitation 49.4480 31.2113 0.1274198 0.467075
scalePrior 1.2794 0.1949 0.0007956 0.001243
steepness 0.4591 0.9029 0.0036862 0.080577
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
meanFruits 711.68826 949.88293 1136.0055 1310.4696 1613.231
midpoint 476.04084 497.99014 511.5795 531.6963 572.150
optimalPrecipitation 1.83924 20.32795 48.8303 78.5571 98.117
scalePrior 0.93085 1.14210 1.2685 1.4036 1.694
steepness 0.06404 0.09414 0.1232 0.2003 3.773
Looking at the median values, the estimates are pretty good for most parameters. The true values were 1000 for meanFruits, 500 for midpoint, 50 for optimalPrecipitation, 0.1 for steepness. But it was 10 for scalePrior, which is estimated here at 1.27. One would need to investigate why scalePrior is not well estimated.