Description Usage Arguments Details Value Author(s) See Also Examples
Estimates ecological memory functions for a specified subset of covariates and a continuous response within a linear model framework. The function is a wrapper to
ecomemMCMC
which implements Markov chain Monte Carlo (MCMC) simulation to fit a Bayesian hierarchical model estimating ecological memory.
1 2 3 4 |
formula |
an object of class |
data |
a data frame including the response, explanatory, group, and time index variables. |
mem.vars |
an optional character vector indicating the subset of explanatory variables for which ecological memory functions should be estimated (memory variables). |
L |
an integer or an integer vector specifying the maximum lag for memory
variables. If a vector is specified, its dimension must match the number of memory
variables specified in |
timeID |
a character value indicating the time index variable within
|
groupID |
an optional character value indicating a group index variable
within |
starting |
optional list of starting values for model parameters. If
specified, the length of the list must match |
smooth |
an optional vector with length equal to the number of memory
variables specifying penalty parameters for penalized spline regression used
to estimate ecological memory functions. If included, |
n.post |
an optional scalar indicating the number of desired posterior
MCMC samples post |
thin |
an optional scalar indicating the thinning interval for posterior samples. The default is set to 10. |
burn.in |
an optional scalar indicating the number of burn-in samples for MCMC chains. The default is set to 5000. |
n.step |
an optional scalar indicating the number of basis function coefficient updates taken per iteration of the MCMC sampler. The default is set to 5. |
n.chains |
an optional scalar indicating the number of MCMC chains. The default is set to 3. |
parallel |
an optional |
max.cpu |
an optional scalar indicating the maximum number of CPUs to
utilize. Only evaluated if |
inputs.only |
an optional |
... |
currently no additional arguments. |
Unequally-spaced time points are not supported. The timeID
variable should include equally-spaced, sequential time points within each group.
Modeled time periods for different groups may be non-overlapping, but the spacing
of time points must be consistent among groups. Time points with NA
values for the response and/or explanatory variables are dropped from the analysis.
NA
values are not supported in the timeID
and groupID
index variables.
Explanatory variable observations for L
time points prior to the
response are needed to fit the regression model when estimating ecological
memory functions for a subset of covariates. If explanatory variable observations
are not available for L
time points prior to the response, the observation
is not evaluated as part of the regression model. This is particularly important
during model initialization. If explanatory variable observations
are available prior to the initial response (first time point), the first
max(L)
responses for each group (if groupID
is
non-NULL
) within data
should be set to NA
. Otherwise,
the first max(L)
response observations within each
group will not be evaluated as part of the regression model.
An object of class ecomem
, which is a list with the following elements:
post.samps |
a list of posterior samples with length equal to |
inputs |
a list of MCMC inputs with length equal to |
data |
a data frame containing data used to fit the regression model including
a response variable with the first |
n |
an integer value indicating the number of reponse observations used to fit the regression model across all groups. |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | ## Not run:
####################################
#### Continuous covariate ##########
####################################
## 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)
## Run ecomem function
mod = ecomem(y~x,data=data,mem.vars="x",L=L,timeID="time")
## Assess model convergence
post.samps = mem2mcmc(mod)
plot(post.samps,ask=T)
## Process results
coef.summ = memsum(mod)
p = plotmem(mod)
print(p)
####################################
#### Discrete covariate ############
####################################
## Simulate some time series data
set.seed(1)
# Maximum lag
L = 5
# Sample size
n = 500
# Total number of time points
TT = n + L
# Generate weights
w = exp(-0.95*(0:L))/sum(exp(-0.95*(0:L)))
# Simulate a single binary covariate
x = rbinom(TT,1,0.05)
# 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)
# Define model parameters
beta = c(2.5,3.4)
sig.y = 0.2
# 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)
## Run ecomem function
mod = ecomem(y~x,data=data,mem.vars="x",L=L,timeID="time")
## Assess model convergence
post.samps = mem2mcmc(mod)
plot(post.samps,ask=T)
## Process results
coef.summ = memsum(mod)
p = plotmem(mod)
print(p)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.