Skip to content

Commit a7cdc56

Browse files
add return argument
1 parent 33b976e commit a7cdc56

File tree

7 files changed

+59
-17
lines changed

7 files changed

+59
-17
lines changed

R/plotStationary.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,34 +274,35 @@ statPlot<-function(model,Sigma,nbStates,formula,covs,tempCovs,tmpcovs,cov,hierRe
274274

275275
if(isTRUE(model$conditions$CT)) tempCovs$dt <- covs$dt
276276
out <- vector('list',mixtures)
277+
names(out) <- paste0("mix",1:mixtures)
277278

278279
for(mix in 1:mixtures){
279280
if(!inherits(model,"hierarchical")){
280-
out[[mix]] <- plotCall(cov,tempCovs,probs[[mix]],model,nbStates,covnames,lwd,arg,col,legend.pos,cex.legend,plotCI,gridLength,hierRecharge,desMat,mix,Sigma,gamInd,alpha,1:nbStates,model$stateNames,formula)
281+
out[[paste0("mix",mix)]] <- plotCall(cov,tempCovs,probs[[mix]],model,nbStates,covnames,lwd,arg,col,legend.pos,cex.legend,plotCI,gridLength,hierRecharge,desMat,mix,Sigma,gamInd,alpha,1:nbStates,model$stateNames,formula)
281282
if(length(covnames)>1) do.call(mtext,c(list(paste0(ifelse(mixtures>1,paste0("Mixture ",mix," s"),"S"),"tationary state probabilities: ",paste(covnames[-cov]," = ",tmpcovs[-cov],collapse=", ")),side=3,outer=TRUE,padj=2,cex=cex.main),marg))
282283
else do.call(mtext,c(list(paste0(ifelse(mixtures>1,paste0("Mixture ",mix," s"),"S"),"tationary state probabilities"),side=3,outer=TRUE,padj=2,cex=cex.main),marg))
283284
} else {
284-
out[[mix]] <- vector('list',model$conditions$hierStates$height-1)
285-
names(out[[mix]]) <- paste0("level",1:(model$conditions$hierStates$height-1))
285+
out[[paste0("mix",mix)]] <- vector('list',model$conditions$hierStates$height-1)
286+
names(out[[paste0("mix",mix)]]) <- paste0("level",1:(model$conditions$hierStates$height-1))
286287
for(j in 1:(model$conditions$hierStates$height-1)){
287288
if(j==1) {
288289
# only plot if there is variation in stationary state proabilities
289290
if(!all(apply(probs[[mix]][["level1"]],2,function(x) all( abs(x - mean(x)) < 1.e-6 )))){
290291
ref <- model$conditions$hierStates$Get(function(x) data.tree::Aggregate(x,"state",min),filterFun=function(x) x$level==j+1)
291-
out[[mix]][[paste0("level",j)]] <- plotCall(cov,tempCovs[which(tempCovs$level==j),],probs[[mix]][["level1"]],model,nbStates,covnames,lwd,arg,col,legend.pos,cex.legend,plotCI,gridLength,hierRecharge,desMat[which(tempCovs$level==j),],mix,Sigma,gamInd,alpha,ref,names(ref),formula)
292+
out[[paste0("mix",mix)]][[paste0("level",j)]] <- plotCall(cov,tempCovs[which(tempCovs$level==j),],probs[[mix]][["level1"]],model,nbStates,covnames,lwd,arg,col,legend.pos,cex.legend,plotCI,gridLength,hierRecharge,desMat[which(tempCovs$level==j),],mix,Sigma,gamInd,alpha,ref,names(ref),formula)
292293
if(length(covnames[-cov][which(covnames[-cov]!="level" & !grepl("recharge",covnames[-cov]))])) do.call(mtext,c(list(paste0(ifelse(mixtures>1,paste0("Mixture ",mix," s"),"S"),"tationary state probabilities for level",j,": ",paste(covnames[-cov][which(covnames[-cov]!="level" & !grepl("recharge",covnames[-cov]))]," = ",tmpcovs[which(tmpcovs$level==j),-cov][which(covnames[-cov]!="level" & !grepl("recharge",covnames[-cov]))],collapse=", ")),side=3,outer=TRUE,padj=2,cex=cex.main),marg))
293294
else do.call(mtext,c(list(paste0(ifelse(mixtures>1,paste0("Mixture ",mix," s"),"S"),"tationary state probabilities for level",j),side=3,outer=TRUE,padj=2,cex=cex.main),marg))
294295
}
295296
} else {
296297
t <- data.tree::Traverse(model$conditions$hierStates,filterFun=function(x) x$level==j)
297298
names(t) <- model$conditions$hierStates$Get("name",filterFun=function(x) x$level==j)
298-
out[[mix]][[paste0("level",j)]] <- vector('list',length(names(t)))
299-
names(out[[mix]][[paste0("level",j)]]) <- names(t)
299+
out[[paste0("mix",mix)]][[paste0("level",j)]] <- vector('list',length(names(t)))
300+
names(out[[paste0("mix",mix)]][[paste0("level",j)]]) <- names(t)
300301
for(k in names(t)){
301302
ref <- t[[k]]$Get(function(x) data.tree::Aggregate(x,"state",min),filterFun=function(x) x$level==j+1)#t[[k]]$Get("state",filterFun = data.tree::isLeaf)
302303
# only plot if jth node has children and there is variation in stationary state proabilities
303304
if(!is.null(ref) && !all(apply(probs[[mix]][[paste0("level",j)]][[k]],2,function(x) all( abs(x - mean(x)) < 1.e-6 )))){
304-
out[[mix]][[paste0("level",j)]][[k]] <- plotCall(cov,tempCovs[which(tempCovs$level==j),],probs[[mix]][[paste0("level",j)]][[k]],model,nbStates,covnames,lwd,arg,col,legend.pos,cex.legend,plotCI,gridLength,hierRecharge,desMat[which(tempCovs$level==j),],mix,Sigma,gamInd,alpha,ref,names(ref),formula)
305+
out[[paste0("mix",mix)]][[paste0("level",j)]][[k]] <- plotCall(cov,tempCovs[which(tempCovs$level==j),],probs[[mix]][[paste0("level",j)]][[k]],model,nbStates,covnames,lwd,arg,col,legend.pos,cex.legend,plotCI,gridLength,hierRecharge,desMat[which(tempCovs$level==j),],mix,Sigma,gamInd,alpha,ref,names(ref),formula)
305306
if(length(covnames[-cov][which(covnames[-cov]!="level" & !grepl("recharge",covnames[-cov]))])) do.call(mtext,c(list(paste0(ifelse(mixtures>1,paste0("Mixture ",mix," s"),"S"),"tationary state probabilities for level",j," ",k,": ",paste(covnames[-cov][which(covnames[-cov]!="level" & !grepl("recharge",covnames[-cov]))]," = ",tmpcovs[which(tmpcovs$level==j),-cov][which(covnames[-cov]!="level" & !grepl("recharge",covnames[-cov]))],collapse=", ")),side=3,outer=TRUE,padj=2,cex=cex.main),marg))
306307
else do.call(mtext,c(list(paste0(ifelse(mixtures>1,paste0("Mixture ",mix," s"),"S"),"tationary state probabilities for level",j," ",k),side=3,outer=TRUE,padj=2,cex=cex.main),marg))
307308
}
@@ -310,7 +311,7 @@ statPlot<-function(model,Sigma,nbStates,formula,covs,tempCovs,tmpcovs,cov,hierRe
310311
}
311312
}
312313
}
313-
if(plotCI && mixtures==1) out <- out[[1]]
314+
if(plotCI && mixtures==1) out <- out[[paste0("mix",mix)]]
314315
return(out)
315316
}
316317

@@ -377,7 +378,7 @@ plotCall <- function(cov,tempCovs,pr,model,nbStates,covnames,lwd,arg,col,legend.
377378
uci[ciInd,state], length=0.025, angle=90, code=3, col=col[ref[state]], lwd=lwd),arg)),warning=muffWarn)
378379

379380
out[[stateNames[state]]] <- data.frame(est=pr[,state],se=c(se),lci=lci[,state],uci=uci[,state])
380-
out[[stateNames[state]]]$cov <- tempCovs[,cov]
381+
out[[stateNames[state]]][[names(tempCovs[,cov,drop=FALSE])]] <- tempCovs[,cov]
381382

382383
}
383384
return(out)

R/plot_miHMM.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#' @param alpha Significance level of the confidence intervals (if \code{plotCI=TRUE}). Default: 0.95 (i.e. 95\% CIs).
2626
#' @param plotStationary Logical indicating whether to plot the stationary state probabilities as a function of any covariates (default: FALSE)
2727
#' @param plotEllipse Logical indicating whether to plot error ellipses around imputed location means. Default: TRUE.
28+
#' @param return Logical indicating whether to return a list containing estimates, SEs, CIs, and covariate values used to create the plots for each mixture and state. Ignored if \code{plotCI=FALSE}. Default: \code{FALSE}.
2829
#' @param ... Additional arguments passed to \code{graphics::plot} and \code{graphics::hist} functions. These can currently include \code{asp}, \code{cex}, \code{cex.axis}, \code{cex.lab}, \code{cex.legend}, \code{cex.main}, \code{legend.pos}, and \code{lwd}. See \code{\link[graphics]{par}}. \code{legend.pos} can be a single keyword from the list ``bottomright'', ``bottom'', ``bottomleft'', ``left'', ``topleft'', ``top'', ``topright'', ``right'', and ``center''. Note that \code{asp} and \code{cex} only apply to plots of animal tracks.
2930
#'
3031
#' @details The state-dependent densities are weighted by the frequency of each state in the most
@@ -63,9 +64,9 @@
6364
#' @export
6465

6566
plot.miHMM <- function(x,animals=NULL,covs=NULL,ask=TRUE,breaks="Sturges",hist.ylim=NULL,sepAnimals=FALSE,
66-
sepStates=FALSE,col=NULL,cumul=TRUE,plotTracks=TRUE,plotCI=FALSE,alpha=0.95,plotStationary=FALSE,plotEllipse=TRUE,...)
67+
sepStates=FALSE,col=NULL,cumul=TRUE,plotTracks=TRUE,plotCI=FALSE,alpha=0.95,plotStationary=FALSE,plotEllipse=TRUE,return=FALSE,...)
6768
{
6869
m <- x$miSum # the name "x" is for compatibility with the generic method
6970
plot(m,animals,covs,ask,breaks,hist.ylim,sepAnimals,
70-
sepStates,col,cumul,plotTracks,plotCI,alpha,plotStationary,plotEllipse,...)
71+
sepStates,col,cumul,plotTracks,plotCI,alpha,plotStationary,plotEllipse,return,...)
7172
}

R/plot_miSum.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#' @param alpha Significance level of the confidence intervals (if \code{plotCI=TRUE}). Default: 0.95 (i.e. 95\% CIs).
2626
#' @param plotStationary Logical indicating whether to plot the stationary state probabilities as a function of any covariates (default: FALSE)
2727
#' @param plotEllipse Logical indicating whether to plot error ellipses around imputed location means. Default: TRUE.
28+
#' @param return Logical indicating whether to return a list containing estimates, SEs, CIs, and covariate values used to create the plots for each mixture and state. Ignored if \code{plotCI=FALSE}. Default: \code{FALSE}.
2829
#' @param ... Additional arguments passed to \code{graphics::plot} and \code{graphics::hist} functions. These can currently include \code{asp}, \code{cex}, \code{cex.axis}, \code{cex.lab}, \code{cex.legend}, \code{cex.main}, \code{legend.pos}, and \code{lwd}. See \code{\link[graphics]{par}}. \code{legend.pos} can be a single keyword from the list ``bottomright'', ``bottom'', ``bottomleft'', ``left'', ``topleft'', ``top'', ``topright'', ``right'', and ``center''. Note that \code{asp} and \code{cex} only apply to plots of animal tracks.
2930
#'
3031
#' @details The state-dependent densities are weighted by the frequency of each state in the most
@@ -64,7 +65,7 @@
6465
#' @export
6566

6667
plot.miSum <- function(x,animals=NULL,covs=NULL,ask=TRUE,breaks="Sturges",hist.ylim=NULL,sepAnimals=FALSE,
67-
sepStates=FALSE,col=NULL,cumul=TRUE,plotTracks=TRUE,plotCI=FALSE,alpha=0.95,plotStationary=FALSE,plotEllipse=TRUE,...)
68+
sepStates=FALSE,col=NULL,cumul=TRUE,plotTracks=TRUE,plotCI=FALSE,alpha=0.95,plotStationary=FALSE,plotEllipse=TRUE,return=FALSE,...)
6869
{
6970
m <- x # the name "x" is for compatibility with the generic method
7071
m <- delta_bc(m)
@@ -82,5 +83,5 @@ plot.miSum <- function(x,animals=NULL,covs=NULL,ask=TRUE,breaks="Sturges",hist.y
8283
m <- momentuHMM(m)
8384

8485
plot.momentuHMM(m,animals,covs,ask,breaks,hist.ylim,sepAnimals,
85-
sepStates,col,cumul,plotTracks,plotCI,alpha,plotStationary,...)
86+
sepStates,col,cumul,plotTracks,plotCI,alpha,plotStationary,return,...)
8687
}

0 commit comments

Comments
 (0)