Skip to content

Commit

Permalink
update vignette to show convert_equations
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Oct 21, 2024
1 parent ebcd63c commit 963c499
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions R/equation_to_text.R → R/convert_equations.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions man/equation_to_text.Rd → man/convert_equations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 31 additions & 7 deletions vignettes/vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,],
Expand Down

0 comments on commit 963c499

Please sign in to comment.