diff --git a/NAMESPACE b/NAMESPACE index d1fadca..ca85fe8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,9 +12,9 @@ export(TMBAIC) export(as_fitted_DAG) export(as_sem) export(classify_variables) +export(convert_equations) export(dsem) export(dsem_control) -export(equation_to_text) export(list_parameters) export(loo_residuals) export(make_dfa) diff --git a/NEWS.md b/NEWS.md index 910ef5f..a5fe77c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ * Adding `stepwise_selection` for automated stepwise model selection * Adding `plot` option to use `ggraph` as alternative to previous `igraph` option +* Adding `convert_equations` to extend `sem::specifyEquations` and simplify + specification for large models # dsem 1.3.0 diff --git a/R/equation_to_text.R b/R/convert_equations.R similarity index 99% rename from R/equation_to_text.R rename to R/convert_equations.R index 1b73bba..73a3730 100644 --- a/R/equation_to_text.R +++ b/R/convert_equations.R @@ -34,9 +34,8 @@ #' To set a start value for a free parameter, enclose the numeric #' start value in parentheses after the parameter name, as parameter(value). #' -#' #' @export -equation_to_text <- +convert_equations <- function(equations){ # Local functions diff --git a/man/equation_to_text.Rd b/man/convert_equations.Rd similarity index 93% rename from man/equation_to_text.Rd rename to man/convert_equations.Rd index 416232f..5a6a9a8 100644 --- a/man/equation_to_text.Rd +++ b/man/convert_equations.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/equation_to_text.R -\name{equation_to_text} -\alias{equation_to_text} +% Please edit documentation in R/convert_equations.R +\name{convert_equations} +\alias{convert_equations} \title{Convert equations notation} \usage{ -equation_to_text(equations) +convert_equations(equations) } \arguments{ \item{equations}{Specification for time-series structural equation model structure diff --git a/vignettes/vignette.Rmd b/vignettes/vignette.Rmd index bd5116a..994dabe 100644 --- a/vignettes/vignette.Rmd +++ b/vignettes/vignette.Rmd @@ -92,18 +92,13 @@ abline(a=0,b=1) ## Comparison with dynamic linear models -We first demonstrate that `dsem` gives identical results to `dynlm` for a well-known econometric model, the Klein-1 model. +We next demonstrate that `dsem` using a well-known econometric model, the Klein-1 model. ```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=7} data(KleinI, package="AER") TS = ts(data.frame(KleinI, "time"=time(KleinI) - 1931)) -# dynlm -fm_cons <- dynlm(consumption ~ cprofits + L(cprofits) + I(pwage + gwage), data = TS) -fm_inv <- dynlm(invest ~ cprofits + L(cprofits) + capital, data = TS) -fm_pwage <- dynlm(pwage ~ gnp + L(gnp) + time, data = TS) - -# dsem +# Specify by declaring each arrow and lag sem = " # Link, lag, param_name cprofits -> consumption, 0, a1 @@ -127,6 +122,35 @@ fit = dsem( sem=sem, control = dsem_control( quiet = TRUE, newton_loops = 0) ) +``` + +This model could instead be specified using equation-and-lag notation, which makes the model structure more clear: + +```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=7} +# Specify using equations +equations = " + consumption = a1*cprofits + a2*lag[cprofits,1]+ a3*pwage + a3*gwage + invest = b1*cprofits + b2*lag[cprofits,1] + b3*capital + pwage = c1*time + c2*gnp + c3*lag[gnp,1] +" + +# Convert and run +sem = convert_equations(equations) +fit = dsem( sem = sem, + tsdata = tsdata, + estimate_delta0 = TRUE, + control = dsem_control( + quiet = TRUE, + newton_loops = 0) ) +``` + +We first demonstrate that `dsem` gives identical results to `dynlm`: + +```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=7} +# dynlm +fm_cons <- dynlm(consumption ~ cprofits + L(cprofits) + I(pwage + gwage), data = TS) +fm_inv <- dynlm(invest ~ cprofits + L(cprofits) + capital, data = TS) +fm_pwage <- dynlm(pwage ~ gnp + L(gnp) + time, data = TS) # Compile results m1 = rbind( summary(fm_cons)$coef[-1,],