Observation | R Documentation |
Contains the data, distributions, parameters, and formulas for the observation model from a hidden Markov model.
new()
Create new Observation object
Observation$new( data = NULL, dists, formulas = NULL, n_states = NULL, par, fixpar = NULL )
data
Data frame containing response variables (named in dists and par) and covariates (named in formulas)
dists
Named list of distribution names for each data stream, with the following options: beta, binom, cat, dir, exp, foldednorm, gamma, gamma2, lnorm, mvnorm, nbinom, norm, pois, t, truncnorm, tweedie, vm, weibull, wrpcauchy, zibinom, zigamma, zigamma2, zinbinom, zipois, zoibeta, ztnbinom, ztpois. See vignette about list of distributions for more details, e.g., list of parameters for each distribution.
formulas
List of formulas for observation parameters. This should be a nested list, where the outer list has one element for each observed variable, and the inner lists have one element for each parameter. Any parameter that is not included is assumed to have the formula ~1. By default, all parameters have the formula ~1 (i.e., no covariate effects).
n_states
Number of states (optional). If not provided, the number
of states is derived from the length of entries of par
.
par
List of initial observation parameters. This should
be a nested list, where the outer list has one element for each
observed variable, and the inner lists have one element for each
parameter. The choice of good initial values can be important, especially
for complex models; the package vignettes discuss approaches to selecting
them (e.g., see Observation$suggest_initial()
).
fixpar
List with optional elements "obs" (fixed coefficients for observation parameters), and "lambda_obs" (fixed smoothness parameters), Each element is a named vector of coefficients that should either be fixed (if the corresponding element is set to NA) or estimated to a common value (using integers or factor levels).
A new Observation object
# Load data set from MSwM package data(energy, package = "MSwM") # Initial observation parameters par0 <- list(Price = list(mean = c(3, 6), sd = c(2, 2))) # Model "energy" with normal distributions obs <- Observation$new(data = energy, dists = list(Price = "norm"), par = par0) # Model "energy" with gamma distributions obs <- Observation$new(data = energy, dists = list(Price = "gamma2"), par = par0) # Model with non-linear effect of EurDol on mean price f <- list(Price = list(mean = ~ s(EurDol, k = 5, bs = "cs"))) obs <- Observation$new(data = energy, dists = list(Price = "norm"), par = par0, formula = f)
data()
Data frame
Observation$data()
dists()
List of distributions
Observation$dists()
nstates()
Number of states
Observation$nstates()
par()
Parameters on natural scale
Observation$par(t = 1, full_names = TRUE, linpred = NULL, as_list = FALSE)
t
Time index or vector of time indices; default t = 1. If t = "all", then return observation parameters for all time points.
full_names
Logical. If TRUE, the rows of the output are named in the format "variable.parameter" (default). If FALSE, the rows are names in the format "parameter". The latter is used in various internal functions, when the parameters need to be passed on to an R function.
linpred
Optional custom linear predictor.
as_list
Logical. If TRUE, the output is a nested list with three levels: (1) time step, (2) observed variable, (3) observation parameter. If FALSE (default), the output is an array with one row for each observation parameter, one column for each state, and one slice for each time step.
Array of parameters with one row for each observation parameter, one column for each state, and one slice for each time step. (See as_list argument for alternative output format.)
# Load data set from MSwM package data(energy, package = "MSwM") # Initial observation parameters par0 <- list(Price = list(mean = c(3, 6), sd = c(2, 2))) # Model with linear effect of EurDol on mean price f <- list(Price = list(mean = ~ EurDol)) obs <- Observation$new(data = energy, dists = list(Price = "norm"), par = par0, n_states = 2, formula = f) # Set slope coefficients obs$update_coeff_fe(coeff_fe = c(3, 2, 6, -2, log(2), log(2))) # Observation parameter values for given data rows obs$par(t = c(1, 10, 20))
par_alt()
Alternative parameter output
This function is only useful for the categorical and multivariate normal distributions, and it formats the parameters in a slightly nicer way.
Observation$par_alt(var = NULL, t = 1)
var
Name of observation variable for which parameters are required. By default, the first variable in 'dists' is used.
t
Time index for covariate values. Only one value should be provided.
List of distribution parameters, with one element for each state
inipar()
Return initial parameter values supplied
Observation$inipar()
coeff_fe()
Fixed effect parameters on working scale
Observation$coeff_fe()
coeff_re()
Random effect parameters
Observation$coeff_re()
X_fe()
Fixed effect design matrix
Observation$X_fe()
X_re()
Random effect design matrix
Observation$X_re()
lambda()
Smoothness parameters
Observation$lambda()
sd_re()
Standard deviation of smooth terms
This function transforms the smoothness parameter of each smooth term into a standard deviation, given by SD = 1/sqrt(lambda). It is particularly helpful to get the standard deviations of independent normal random effects.
Observation$sd_re()
formulas()
List of model formulas for observation model
Observation$formulas(raw = FALSE)
raw
Logical. If FALSE, returns the nested list created by make_formulas (default). If TRUE, returns formulas passed as input.
terms()
Terms of model formulas
Observation$terms()
obs_var()
Data frame of response variables
Observation$obs_var(expand = FALSE)
expand
If TRUE, then multivariate variables in observations are expanded to be univariate, creating extra columns.
Data frame of observation variables
known_states()
Vector of known states
Observation$known_states(mat = TRUE)
mat
Logical.
fixpar()
Fixed parameters
Observation$fixpar(all = FALSE)
all
Logical. If FALSE, only user-specified fixed parameters are returned, but not parameters that are fixed for some other reason (e.g., size of binomial distribution)
empty()
Empty model? (for simulation only)
Observation$empty()
update_par()
Update parameters
Updates the 'par' attribute to the list passed as input, and updates the intercept elements of 'coeff_fe' using the list passed as input
Observation$update_par(par)
par
New list of parameters
update_coeff_fe()
Update coefficients for fixed effect parameters
Observation$update_coeff_fe(coeff_fe)
coeff_fe
New vector of coefficients for fixed effect parameters
update_coeff_re()
Update random effect parameters
Observation$update_coeff_re(coeff_re)
coeff_re
New vector of coefficients for random effect parameters
update_X_fe()
Update fixed effect design matrix
Observation$update_X_fe(X_fe)
X_fe
New fixed effect design matrix
update_X_re()
Update random effect design matrix
Observation$update_X_re(X_re)
X_re
New random effect design matrix
update_lambda()
Update smoothness parameters
Observation$update_lambda(lambda)
lambda
New smoothness parameter vector
update_data()
Update data
Observation$update_data(data)
data
New data frame
update_fixpar()
Update information about fixed parameters
Observation$update_fixpar(fixpar)
fixpar
New list of fixed parameters, in the same format expected by Observation$new()
make_mat()
Make model matrices
Observation$make_mat(new_data = NULL)
new_data
Optional new data set, including covariates for which the design matrices should be created. If this argument is not specified, the design matrices are based on the original data frame.
A list with elements:
Design matrix for fixed effects
Design matrix for random effects
Smoothness matrix for random effects
Number of columns of X_fe for each parameter
Number of columns of X_re and S for each random effect
make_newdata_grid()
Design matrices for grid of covariates
Observation$make_newdata_grid(var, covs = NULL, n_grid = 1000)
var
Name of variable
covs
Optional named list for values of covariates (other than 'var') that should be used in the plot (or dataframe with single row). If this is not specified, the mean value is used for numeric variables, and the first level for factor variables.
n_grid
Grid size (number of points). Default: 1000.
A list with the same elements as the output of make_mat, plus a data frame of covariates values.
n2w()
Natural to working parameter transformation
This function applies the link functions of the distribution parameters, to transform parameters from their natural scale to the working scale (i.e., linear predictor scale)
Observation$n2w(par)
par
List of parameters on natural scale
Vector of parameters on working scale
w2n()
Working to natural parameter transformation
This function applies the inverse link functions of the distribution parameters, to transform parameters from the working scale (i.e., linear predictor scale) to their natural scale.
Observation$w2n(wpar)
wpar
Vector of parameters on working scale
List of parameters on natural scale
linpred()
Compute linear predictor
Observation$linpred()
obs_probs()
Observation likelihoods
Observation$obs_probs(data = NULL)
data
Optional dataframe to include in form of obs_var() output
Matrix of likelihoods of observations, with one row for each time step, and one column for each state.
cdf()
Cumulative probabilities of observations
Observation$cdf()
List of cumulative probabilities, with one element for each observed variable. Matrix rows correspond to time steps, and columns correspond to states.
suggest_initial()
Suggest initial observation parameters
The K-means algorithm is used to define clusters of observations
(supposed to approximate the HMM states). Then, for each cluster,
the parapprox
function of the relevant Dist
object
is used to obtain parameter values.
Observation$suggest_initial()
List of initial parameters for each observation variable
# Load data set from MSwM package data(energy, package = "MSwM") # Initial observation parameters par0 <- list(Price = list(mean = c(3, 6), sd = c(2, 2))) # Model "energy" with normal distributions obs <- Observation$new(data = energy, dists = list(Price = "norm"), par = par0, n_states = 2) # Print observation parameters obs$par() # Suggest initial parameters par0_new <- obs$suggest_initial() par0_new # Update model parameters to suggested obs$update_par(par = par0_new) obs$par()
plot_dist()
Plot histogram of data and pdfs
Plot histogram of observations for the variable specified by the argument name, overlaid with the pdf of the specified distribution for that data stream. Helpful to select initial parameter values for model fitting, or to visualise fitted state-dependent distributions.
Observation$plot_dist(var = NULL, weights = NULL, t = 1)
var
Name of response variable for which the histogram and pdfs should be plotted.
weights
Optional vector of length the number of pdfs that are plotted. Useful to visualise a mixture of distributions weighted by the proportion of time spent in the different states.
t
Index of time step to use for covariates (default: 1).
A ggplot object
formulation()
Print model formulation
Observation$formulation()
print()
Print Observation object Check constructor arguments
Observation$print()
clone()
The objects of this class are cloneable with this method.
Observation$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------
## Method `Observation$new`
## ------------------------------------------------
# Load data set from MSwM package
data(energy, package = "MSwM")
# Initial observation parameters
par0 <- list(Price = list(mean = c(3, 6), sd = c(2, 2)))
# Model "energy" with normal distributions
obs <- Observation$new(data = energy,
dists = list(Price = "norm"),
par = par0)
# Model "energy" with gamma distributions
obs <- Observation$new(data = energy,
dists = list(Price = "gamma2"),
par = par0)
# Model with non-linear effect of EurDol on mean price
f <- list(Price = list(mean = ~ s(EurDol, k = 5, bs = "cs")))
obs <- Observation$new(data = energy,
dists = list(Price = "norm"),
par = par0,
formula = f)
## ------------------------------------------------
## Method `Observation$par`
## ------------------------------------------------
# Load data set from MSwM package
data(energy, package = "MSwM")
# Initial observation parameters
par0 <- list(Price = list(mean = c(3, 6), sd = c(2, 2)))
# Model with linear effect of EurDol on mean price
f <- list(Price = list(mean = ~ EurDol))
obs <- Observation$new(data = energy,
dists = list(Price = "norm"),
par = par0,
n_states = 2,
formula = f)
# Set slope coefficients
obs$update_coeff_fe(coeff_fe = c(3, 2, 6, -2, log(2), log(2)))
# Observation parameter values for given data rows
obs$par(t = c(1, 10, 20))
## ------------------------------------------------
## Method `Observation$suggest_initial`
## ------------------------------------------------
# Load data set from MSwM package
data(energy, package = "MSwM")
# Initial observation parameters
par0 <- list(Price = list(mean = c(3, 6), sd = c(2, 2)))
# Model "energy" with normal distributions
obs <- Observation$new(data = energy,
dists = list(Price = "norm"),
par = par0,
n_states = 2)
# Print observation parameters
obs$par()
# Suggest initial parameters
par0_new <- obs$suggest_initial()
par0_new
# Update model parameters to suggested
obs$update_par(par = par0_new)
obs$par()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.