Description Usage Arguments Value Author(s) See Also Examples
Implements Markov chain Monte Carlo (MCMC) simulation to fit a Bayesian hierarchical generalized linear regression model estimating ecological memory.
1 |
x |
an |
A named list containing posterior samples for model parameters including beta
.
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 68 69 70 71 72 73 74 75 76 77 78 | ## Not run:
## Simulate some time series data
####################################
#### Binomial response #############
####################################
## Define logit backtransformation
inv.logit = function(x){
1/(1+exp(-x))
}
## Simulate some time series data
set.seed(1)
# Maximum lag
L = 8
# Sample size
n = 200
# Total number of time points
TT = n + L
# Generate weights
w = exp(-0.7*(0:L))/sum(exp(-0.7*(0:L)))
# Simulate covariate values
x = scale(rnorm(TT,(1:TT)/5000+0.02*cos(2*pi*(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(-0.8,0.75)
# Simulate from ecological memory model
n.trials = sample(1:5,n,replace=T)
y = c(rep(NA,L),rbinom(n,n.trials,inv.logit(X%*%beta)))
# Form model data frame
data = data.frame(time=1:TT,x=x,y=y,n.trials=c(rep(NA,L),n.trials))
## Generate ecological memory model inputs
n.chains = 2
mod = ecomemGLM(y~x,family="binomial",data=data,mem.vars="x",
L=L,timeID="time",offset="n.trials",
n.chains=n.chains,inputs.only=TRUE)
## Run ecomemMCMC
mod.fit = lapply(1:n.chains, function(i){
ecomemGLMMCMC(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.