Skip to content

Commit

Permalink
change default behavior to include SDs for all variables (#12)
Browse files Browse the repository at this point in the history
* Update vignette.Rmd

* changing default behavior ...

... to include SD for all variables in `tsdata` by default

* try again

* Update Date in DESCRIPTION

* adding test for adding variances

* adding expect_equal for new test

* strip the version from Matrix/TMB in DESCRIPTION

---------

Co-authored-by: Jim Thorson <James.T.Thorson@gmail.com>
  • Loading branch information
James-Thorson-NOAA and James-Thorson authored Feb 2, 2024
1 parent 09f9d09 commit 225d733
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 37 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
branches: [main, dev]
pull_request:
branches: [main, master]
branches: [main, dev]

name: R-CMD-check

Expand All @@ -21,9 +21,21 @@ jobs:
with:
use-public-rspm: true

- name: Install dependencies Ubuntu
if: runner.os == 'Linux'
run: |
install.packages('remotes')
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("knitr")
remotes::install_cran("rmarkdown")
remotes::install_cran("rcmdcheck")
install.packages("Matrix", type = "source")
install.packages("TMB", type = "source")
shell: Rscript {0}

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
- uses: r-lib/actions/check-r-package@v2
16 changes: 14 additions & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
branches: [main, dev]
pull_request:
branches: [main, master]
branches: [main, dev]
release:
types: [published]
workflow_dispatch:
Expand All @@ -30,6 +30,18 @@ jobs:
with:
use-public-rspm: true

- name: Install dependencies Ubuntu
if: runner.os == 'Linux'
run: |
install.packages('remotes')
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("knitr")
remotes::install_cran("rmarkdown")
remotes::install_cran("rcmdcheck")
install.packages("Matrix", type = "source")
install.packages("TMB", type = "source")
shell: Rscript {0}

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
Expand Down
16 changes: 14 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master, test_codecov]
branches: [main, dev]
pull_request:
branches: [main, master, test_codecov]
branches: [main, dev]

name: test-coverage

Expand All @@ -21,6 +21,18 @@ jobs:
with:
use-public-rspm: true

- name: Install dependencies Ubuntu
if: runner.os == 'Linux'
run: |
install.packages('remotes')
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("knitr")
remotes::install_cran("rmarkdown")
remotes::install_cran("rcmdcheck")
install.packages("Matrix", type = "source")
install.packages("TMB", type = "source")
shell: Rscript {0}

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ Package: dsem
Type: Package
Title: Fit Dynamic Structural Equation Models
Version: 1.0.2
Date: 2024-01-17
Date: 2024-02-01
Authors@R:
c(person(given = "James",
family = "Thorson",
role = c("aut", "cre"),
email = "James.Thorson@noaa.gov")
)
Imports:
TMB (>= 1.9.7),
Matrix (>= 1.6.3),
TMB,
Matrix,
sem,
igraph,
methods
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Eliminate `eval` usage
* Add codecov Action and badge
* Change default behavior so that all variables in `tsdata` have a standard
deviation by default

# dsem 1.0.1

Expand Down
1 change: 1 addition & 0 deletions R/dsem.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function( sem,
out = make_dsem_ram( sem,
times = as.numeric(time(tsdata)),
variables = colnames(tsdata),
covs = colnames(tsdata),
quiet = control$quiet )
ram = out$ram

Expand Down
39 changes: 21 additions & 18 deletions R/make_dsem_ram.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @inheritParams dsem
#' @param times A character vector listing the set of times in order
#' @param variables A character vector listing the set of variables
#' @param covs A character vector listing variables for which to estimate a standard deviation
#' @param quiet Boolean indicating whether to print messages to terminal
#' @param remove_na Boolean indicating whether to remove NA values from RAM (default) or not.
#' \code{remove_NA=FALSE} might be useful for exploration and diagnostics for
Expand Down Expand Up @@ -193,6 +194,7 @@ make_dsem_ram <-
function( sem,
times,
variables,
covs = NULL,
quiet = FALSE,
remove_na = TRUE ){
# Docs : https://roxygen2.r-lib.org/articles/formatting.html
Expand Down Expand Up @@ -284,24 +286,25 @@ function( sem,
model$par[model$par == ""] <- NA
model <- cbind( "path"=model$path, "lag"=model$lag, "name"=model$par, "start"=model$start)

#if( !is.null(covs) ){
# for (cov in covs) {
# vars <- strsplit(cov, "[ ,]+")[[1]]
# nvar <- length(vars)
# for (i in 1:nvar) {
# for (j in i:nvar) {
# p1 = paste(vars[i], "<->", vars[j])
# p2 = if (i==j) paste("V[", vars[i], "]", sep = "") else paste("C[",vars[i], ",", vars[j], "]", sep = "")
# p3 = NA
# row <- c(p1, 0, p2, p3)
# if( any((row[1]==model[,1]) & (row[2]==model[,2])) ){
# next
# }else{
# model <- rbind(model, row, deparse.level = 0)
# }
# }}
# }
#}
# Adding a SD automatically
if( !is.null(covs) ){
for (cov in covs) {
vars <- strsplit(cov, "[ ,]+")[[1]]
nvar <- length(vars)
for (i in 1:nvar) {
for (j in i:nvar) {
p1 = paste(vars[i], "<->", vars[j])
p2 = if (i==j) paste("V[", vars[i], "]", sep = "") else paste("C[",vars[i], ",", vars[j], "]", sep = "")
p3 = NA
row <- c(p1, 0, p2, p3)
if( any((row[1]==model[,1]) & (row[2]==model[,2])) ){
next
}else{
model <- rbind(model, row, deparse.level = 0)
}
}}
}
}

exog.variances = endog.variances = TRUE
model = add.variances()
Expand Down
11 changes: 10 additions & 1 deletion man/make_dsem_ram.Rd

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

2 changes: 1 addition & 1 deletion scratch/Upload_to_CRAN.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

root_dir = R'(C:\Users\James.Thorson\Desktop\Work files\Collaborations\2023 -- DSEM\CRAN submission)'

library(devtools)
setwd( R'(C:\Users\James.Thorson\Desktop\Git\dsem)' )
Expand Down Expand Up @@ -66,6 +65,7 @@ if( FALSE ){
}

# Build for uploading via web interface
root_dir = R'(C:\Users\James.Thorson\Desktop\Git\dsem\scratch)'
# https://cran.r-project.org/submit.html
build( path=root_dir, manual=TRUE )

Expand Down
Binary file added scratch/dsem_1.0.2.tar.gz
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/testthat/test-platform.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ test_that("dsem example is working ", {
residuals(fit1, type="response")
})

test_that("dsem adds variances ", {
data(isle_royale)
data = ts( log(isle_royale[,2:3]), start=1959)

sem = "
wolves <-> wolves, 0, sd1
moose <-> moose, 0, sd2
"
# initial first without delta0 (to improve starting values)
fit1 = dsem( sem = "",
tsdata = data )
# initial first without delta0 (to improve starting values)
fit2 = dsem( sem = sem,
tsdata = data )
# Check objective function
expect_equal( as.numeric(fit1$opt$obj), as.numeric(fit2$opt$obj), tolerance=1e-2 )
})

16 changes: 9 additions & 7 deletions vignettes/vignette.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Demonstration of selected features"
author: "Anonymous"
author: "James Thorson"
output: rmarkdown::html_vignette
#output: rmarkdown::pdf_document
vignette: >
Expand Down Expand Up @@ -35,7 +35,7 @@ library(phylopath)

We first demonstrate that `dsem` gives identical results to `dynlm` for a well-known econometric model, the Klein-1 model.

```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=5}
```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=7}
data(KleinI, package="AER")
TS = ts(data.frame(KleinI, "time"=time(KleinI) - 1931))
Expand Down Expand Up @@ -148,7 +148,7 @@ This dataset was previously analyzed by in Chapter 14 of the User Manual for the

Here, we compare fits using `dsem` with `dynlm`, as well as a vector autoregressive model package `vars`, and finally with `MARSS`.

```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=5}
```{r, echo=TRUE, message=FALSE, fig.width=5, fig.height=7}
data(isle_royale)
data = ts( log(isle_royale[,2:3]), start=1959)
Expand Down Expand Up @@ -242,7 +242,7 @@ Results again show that `dsem` can estimate parameters for a vector autoregressi

We next replicate an analysis involving climate, forage fishes, stomach contents, and recruitment of a predatory fish.

```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=5}
```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=7}
data(bering_sea)
Z = ts( bering_sea )
family = rep('fixed', ncol(bering_sea))
Expand Down Expand Up @@ -277,7 +277,8 @@ fit = dsem( sem = sem,
control = dsem_control(use_REML=FALSE, quiet=TRUE) )
ParHat = fit$obj$env$parList()
# summary( fit )
```
```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=7}
# Timeseries plot
oldpar <- par(no.readonly = TRUE)
par( mfcol=c(3,3), mar=c(2,2,2,0), mgp=c(2,0.5,0), tck=-0.02 )
Expand All @@ -296,7 +297,8 @@ for(i in 1:ncol(bering_sea)){
y=c(tmp[,3],rev(tmp[,4])), col=rgb(0,0,1,0.2), border=NA )
}
par(oldpar)
```
```{r, echo=TRUE, message=FALSE, fig.width=8, fig.height=8}
#
library(phylopath)
library(ggplot2)
Expand Down Expand Up @@ -333,7 +335,7 @@ These results are further discussed in the paper describing dsem.

Finally, we replicate an analysis involving a trophic cascade involving sea stars predators, sea urchin consumers, and kelp producers.

```{r, echo=TRUE, message=FALSE, fig.width=7, fig.height=5}
```{r, echo=TRUE, message=FALSE, fig.width=5, fig.height=7}
data(sea_otter)
Z = ts( sea_otter[,-1] )
Expand Down

0 comments on commit 225d733

Please sign in to comment.