Skip to content

Commit efe3717

Browse files
committed
issue 374
- add tidying of AR1 parameters when time_varying_type = "ar1" - add print handling - bump version number - update news
1 parent 2bf9ebf commit efe3717

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: sdmTMB
33
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
4-
Version: 0.6.0.9024
4+
Version: 0.6.0.9025
55
Authors@R: c(
66
person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca",
77
role = c("aut", "cre"),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
* Add option for `area` to be passed in as the name of a column in the
6868
dataframe to be used for area weighting. Used in `get_index()`,
6969
`get_cog()`, `get_eao()`, etc.
70+
71+
* Add time varying AR(1) correlation to tidy() and print() #374
7072

7173
# sdmTMB 0.6.0
7274

R/print.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ print_time_varying <- function(x, m = 1) {
181181
colnames(mm_tv) <- c("coef.est", "coef.se")
182182
time_slices <- x$time_lu$time_from_data
183183
row.names(mm_tv) <- paste(rep(tv_names, each = length(time_slices)), time_slices, sep = "-")
184+
185+
# append AR(1) parameters if they are estimated
186+
p <- tidy(x, effects = "ran_pars", model = m, silent = TRUE)
187+
if(any(p$term == "rho_time")) {
188+
rho_tv <- as.matrix(p[p$term=="rho_time",c("estimate","std.error")])
189+
colnames(rho_tv) <- colnames(mm_tv)
190+
row.names(rho_tv) <- paste("rho", tv_names, sep = "-")
191+
mm_tv <- rbind(mm_tv, rho_tv)
192+
}
184193
} else {
185194
mm_tv <- NULL
186195
}

R/tidy.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals"), model =
107107
p$sigma_O <- as.numeric(p$sigma_O[1,model])
108108
p$sigma_Z <- as.numeric(p$sigma_Z[,model])
109109
p$sigma_G <- as.numeric(p$sigma_G[,model])
110+
111+
# if delta, a single AR1 -> rho_time_unscaled is a 1x2 matrix
112+
p$rho_time <- 2 * plogis(p$rho_time_unscaled[,model]) - 1
110113
p
111114
}
115+
112116
est <- subset_pars(est, model)
113117
se <- subset_pars(se, model)
114118

@@ -186,6 +190,10 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals"), model =
186190
log_name <- c(log_name, "ln_tau_G")
187191
name <- c(name, "sigma_G")
188192
}
193+
if (!all(est$rho_time==0)) {
194+
log_name <- c(log_name, "rho_time_unscaled")
195+
name <- c(name, "rho_time")
196+
}
189197

190198
j <- 0
191199
if (!"log_range" %in% names(est)) {
@@ -204,6 +212,8 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals"), model =
204212
this <- non_log_name[j]
205213
if (this == "tau_G") this <- "sigma_G"
206214
if (this == "tau_V") this <- "sigma_V"
215+
if (this == "rho_time_unscaled") this <- "rho_time"
216+
207217
this_se <- as.numeric(se[[this]])
208218
this_est <- as.numeric(est[[this]])
209219
if (length(this_est) && !(all(this_se == 0) && all(this_est == 0))) {
@@ -213,6 +223,19 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals"), model =
213223
conf.high = exp(.e + crit * .se),
214224
stringsAsFactors = FALSE
215225
)
226+
if(this == "rho_time") {
227+
out_re[[i]] <- data.frame(
228+
term = i,
229+
estimate = this_est,
230+
# use delta method to get SE in normal space
231+
std.error = 2 * plogis (.e[,model]) * (1 - plogis (.e[,model])) * .se[,model],
232+
# don't use delta-method for CIs, because they can be outside (-1,1)
233+
conf.low = 2 * plogis(.e[,model] - crit * .se[,model]) - 1,
234+
conf.high = 2 * plogis(.e[,model] + crit * .se[,model]) - 1,
235+
stringsAsFactors = FALSE
236+
)
237+
238+
}
216239
}
217240
ii <- ii + 1
218241
}

tests/testthat/test-time-varying.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,79 @@ test_that("AR1 time-varying works", {
6868
expect_equal(dim(ss), c(4000L, 1L))
6969
expect_gt(exp(s$ln_tau_V)[1,1], 0.75)
7070
expect_gt(m121(s$rho_time_unscaled)[1,1], 0.65)
71+
72+
# test that tidy works
73+
fit <- sdmTMB(density ~ 1, time = "year",
74+
time_varying = ~ 1,
75+
time_varying_type = "ar1",
76+
data = pcod_2011, spatial="off",
77+
spatiotemporal = "off",
78+
family = tweedie())
79+
s <- tidy(fit, "ran_pars")
80+
expect_equal(dim(s), c(3L, 5L))
81+
expect_equal(s$term, c("phi","rho_time","tweedie_p"))
82+
83+
# test that tidy works -- RW
84+
fit <- sdmTMB(density ~ as.factor(year), time = "year",
85+
time_varying = ~ -1 + depth_scaled,
86+
data = pcod_2011, spatial="off",
87+
spatiotemporal = "off",
88+
family = tweedie())
89+
s <- tidy(fit, "ran_pars")
90+
expect_equal(dim(s), c(2L, 5L))
91+
expect_equal(s$term, c("phi","tweedie_p"))
92+
93+
# test that tidy works -- AR1
94+
fit <- sdmTMB(density ~ as.factor(year), time = "year",
95+
time_varying = ~ -1 + depth_scaled,
96+
data = pcod_2011, spatial="off",
97+
time_varying_type = "ar1",
98+
spatiotemporal = "off",
99+
family = tweedie())
100+
s <- tidy(fit, "ran_pars")
101+
expect_equal(dim(s), c(3L, 5L))
102+
expect_equal(s$term, c("phi", "rho_time", "tweedie_p"))
103+
104+
# test that tidy works -- no time varying
105+
fit <- sdmTMB(density ~ as.factor(year), time = "year",
106+
data = pcod_2011, spatial="off",
107+
spatiotemporal = "off",
108+
family = tweedie())
109+
s <- tidy(fit, "ran_pars")
110+
expect_equal(dim(s), c(2L, 5L))
111+
expect_equal(s$term, c("phi","tweedie_p"))
112+
113+
# test that tidy works with 2 parameters
114+
fit <- sdmTMB(density ~ 1, time = "year",
115+
time_varying = ~ -1+depth_scaled + I(depth_scaled^2),
116+
time_varying_type = "ar1",
117+
data = pcod_2011, spatial="off",
118+
spatiotemporal = "off",
119+
family = tweedie())
120+
s <- tidy(fit, "ran_pars")
121+
expect_equal(dim(s), c(4L, 5L))
122+
expect_equal(s$term, c("phi","rho_time","rho_time","tweedie_p"))
123+
124+
# test that tidy works with delta
125+
fit <- sdmTMB(density ~ 1, time = "year",
126+
time_varying = ~ -1+depth_scaled + I(depth_scaled^2),
127+
time_varying_type = "ar1",
128+
data = pcod_2011, spatial="off",
129+
spatiotemporal = "off",
130+
family = delta_gamma())
131+
s <- tidy(fit, "ran_pars")
132+
expect_equal(dim(s), c(2L, 5L))
133+
expect_equal(s$term, c("rho_time","rho_time"))
134+
135+
s <- tidy(fit, "ran_pars", model = 1)
136+
expect_equal(dim(s), c(2L, 5L))
137+
expect_equal(s$term, c("rho_time","rho_time"))
138+
expect_equal(s$estimate, c(1.0, 0.9582931))
139+
140+
s <- tidy(fit, "ran_pars", model = 2)
141+
expect_equal(s$term, c("phi", "rho_time","rho_time"))
142+
expect_equal(s$estimate, c(0.652, 1.0, 0.868), tolerance = 0.001)
143+
71144
})
72145

73146

0 commit comments

Comments
 (0)