Page 1 of 1

Plotting a model parameter by just time

PostPosted: Mon Mar 11, 2024 12:59 pm
by rcscott
This is less of a question and more of an observation. Recently, I had to create a graph for a dynamic model of the effect of time (season) on a parameter of interest. All the posts I found on PhiDot included a time varying covariate, and none that I found had a simple parameter ~ time model to graph. Here, I provide a quick example using the dipper data from RMark. I’m still new to Rmark so please let me know if I’ve done anything incorrectly.

First let’s read in the packages we’ll need, which are just RMark and ggplot2. Then we’ll read in the dipper data.

Code: Select all
library(RMark)
library(ggplot2)
data(dipper)


Then, per standard procedure, we’ll process the data and create a .ddl. In this case we also create a simple model of Phi ~ time.

Code: Select all
dp=process.data(dipper,model="CJS")
ddl=make.design.data(dp)
model=mark(dp,ddl,model.parameters = list(Phi=list(formula=~time)))


Now is where I realized that the best way to graph Phi (or any time varying parameter of interest) is to pull the estimated real values from the model. In this case the real values of phi correspond to rows 1:6 of the model$results$real, so we pull those rows. Note that for your own data you will need to determine which rows to use.

I also added in a dummy variable which is used for time (1:6 for 6 different sessions). If we then check the object we’ve created we see that it has the 6 values of phi corresponding to the 6 different sessions, and we have labeled time 1:6.

Code: Select all
phi <- model$results$real[1:6,]
phi$time <- 1:6
View(phi)   


We can then plot it using the code below:

Code: Select all
phiXtime <- ggplot(data = phi, aes(x = time, y = estimate))+
  geom_line(linewidth = 1.5)+
  geom_errorbar(aes(ymin = lcl, ymax = ucl), width = 0.2) +
  scale_colour_brewer(palette = "Set1") +
  theme(legend.position = c(0.15, 0.8),
        legend.justification = c(1, 0)) +
  xlab("Year") +
  ylab("Extinction probability")+
  ggtitle("Extinction probability by year")+
  theme_classic()+
  ylim(c(0,1))
phiXtime

Re: Plotting a model parameter by just time

PostPosted: Tue Mar 12, 2024 3:57 pm
by jlaake
Thanks for posting the example. What you did will work for any type of plot of real parameter estimates for some function of age, time, cohort or group variable by selecting the set of estimates to be plotted. It can be useful to column bind (cbind) the design data for a parameter (eg phi) with the matching set of real estimates to plot a set of lines or in separate plots for some factor variable ( eg sex, stratum etc).

Re: Plotting a model parameter by just time

PostPosted: Thu Mar 14, 2024 8:48 am
by rcscott
Thanks Jeff! The cbind() tip is good to know!