-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from birdflow-science/combine-transitions
- Loading branch information
Showing
6 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Function to combine a sequence of transition matrices into one | ||
#' | ||
#' @param bf A BirdFlow object | ||
#' @inheritDotParams lookup_transitions -x | ||
#' | ||
#' @return This returns the transition probabilities associated with a | ||
#' sequence of timesteps. It will have a column for every unmasked cell | ||
#' at the starting timestep and a row for every unmasked cell in the | ||
#' last timestep, with cell values being the probably of transitioning from | ||
#' that row to that column between the start and end of the time sequence | ||
#' described by `...` | ||
#' | ||
#' @export | ||
combine_transitions <- function(bf, ...){ | ||
# Lookup transition names | ||
transitions <- lookup_transitions(x = bf, ...) | ||
|
||
# Multiply all the transitions together | ||
for(i in seq_along(transitions)){ | ||
# Origin timestep get transtion | ||
if(i == 1){ | ||
trans <- get_transition(bf, transitions[i]) | ||
next | ||
} | ||
# All other timesteps get transition and multiple with prior | ||
a <- get_transition(bf, transitions[i]) | ||
trans <- a %*% trans | ||
} | ||
return(trans) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
test_that("combine_transitions is consistent with predict", { | ||
|
||
bf <- BirdFlowModels::amewoo | ||
|
||
start <- 5 | ||
end <- 49 | ||
direction <- "backward" | ||
|
||
# Combine the transitions | ||
expect_no_error( | ||
trans <- combine_transitions(bf, start = start, | ||
end = end, direction = direction ) | ||
) | ||
|
||
# predict ending distribution with bf | ||
# - by stepping through intervening transitions | ||
s_st <- get_distr(bf, start) | ||
e_st <- get_distr(bf, end) # status and trends | ||
p <- predict.BirdFlow(bf, s_st, start = start, | ||
end = end, direction = direction) | ||
e_bf <- p[, ncol(p)] # end distribution from Birdflow predict (active cells) | ||
|
||
start_dm <- get_dynamic_mask(bf, start) | ||
end_dm <- get_dynamic_mask(bf, end) | ||
|
||
# end distribution from consolidated transition (just dynamic) | ||
e_trans <- as.vector(trans %*% s_st[start_dm]) | ||
|
||
# They should be identical | ||
expect_equal <- cor(e_trans, e_bf[end_dm]) | ||
|
||
|
||
}) |