Description Usage Arguments Value Author(s) See Also Examples
Implements Markov chain Monte Carlo (MCMC) simulation to fit a Bayesian hierarchical linear regression model estimating ecological memory.
1 | ecomemMCMC(x)
|
x |
an |
A named list containing posterior samples for each model parameter including: beta
and
sig.y
. If memory variables are specified, the list includes samples for eta
,
w
, X
, and tau.sq
(only if smooth = NULL
). eta
and w
are named lists with basis function coefficients and weights for each memory variable
specified. The i^{th} row of X
contains covariate values weighted based
on the i^{th} posterior sample of ecological memory function weights formed by converting
the weighted design matrix to a vector by rows.
Malcolm S. Itter malcolm.itter@helsinki.fi
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | ## Not run:
## Simulate some time series data
set.seed(1)
# Maximum lag
L = 10
# Sample size
n = 1000
# Total number of time points
TT = n + L
# Generate weights
w = exp(-0.5*(0:L))/sum(exp(-0.5*(0:L)))
# Simulate a single continuous covariate
x = scale(rnorm(TT,(1:TT)/1000+0.05*cos(
2*pi*10*(1:TT)/TT),2.4),center=FALSE)
# Form matrix containing lagged covariate values
x.lag = t(sapply(1:n,function(i){
x[(L+1):1 + i - 1]
}))
# Calculate weighted covariate values
x.tilde = x.lag%*%w
# Form design matrix
X = cbind(rep(1,n),x.tilde)
# Set model parameter values
beta = c(1.3,-0.4)
sig.y = 0.1
# Simulate from ecological memory model
y = c(rep(NA,L),rnorm(n,X%*%beta,sig.y))
# Form model data frame
data = data.frame(time=1:TT,x=x,y=y)
## Generate ecological memory model inputs
n.chains = 2
mod = ecomem(y~x,data=data,mem.vars="x",L=L,timeID="time",
n.chains=n.chains, inputs.only=TRUE)
## Run ecomemMCMC
mod.fit = lapply(1:n.chains, function(i){
ecomemMCMC(mod$inputs[[i]])
})
## Assess model convergence
mod$post.samps = mod.fit
plot(mem2mcmc(mod),ask=T)
## Plot memory function
plotmem(mod)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.