Page 1 of 1

state-specific time varying design covariate

PostPosted: Fri Dec 02, 2022 1:00 pm
by bmarcek
Hi all,

I am revising a multistate model and I'd like to include environmental covariates. One covariate (temperature) is time varying but constant across space (strata) and the other (gage height) varies in both space and time. I'd like to include these covariates into the transition probabilities among states (Psi), but I'm uncertain of how to incorporate them into the model/design matrix. In a previous version of the model, I added month and season to the design matrix using the following code:


Code: Select all
time <- 1:103
  month <- c(6:12, rep(1:12, 8))
  season <- character(length = length(month))
  for(i in 1:length(season)){
    if (month[i] > 5 & month[i] < 9)
      season[i] <- 'Summer'
    if (month[i] > 8 & month[i] < 12)
      season[i] <- 'Autumn'
    if (month[i] == 12 | month[i] < 3)
      season[i] <- 'Winter'
    if (month[i] > 2 & month[i] < 6)
      season[i] <- 'Spring'
  }
 
  month <- factor(month, levels = c('6', '7', '8', '9', '10', '11', '12',
                                    '1', '2', '3', '4', '5'),
                  labels = c('Jun', 'Jul', 'Aug', 'Sep',
                             'Oct', 'Nov', 'Dec', 'Jan',
                             'Feb', 'Mar', 'Apr', 'May'))
  season <- as.factor(season)
 
  design_covariates <- data.frame(time = time,
                                  month = month,
                                  season = season)
 
  # merge design covariates with design data for each parameter
  eh_2013_2021_SVC.ddl$S <- merge_design.covariates(eh_2013_2021_SVC.ddl$S,
                                                    design_covariates)
  eh_2013_2021_SVC.ddl$p <- merge_design.covariates(eh_2013_2021_SVC.ddl$p,
                                                    design_covariates)
  eh_2013_2021_SVC.ddl$Psi <- merge_design.covariates(eh_2013_2021_SVC.ddl$Psi,
                                                      design_covariates)


It seems that adding in temperature (not spatially varying) would be simple, I can add it to the design_covariates data frame and it will align with the other design covariates (1 measurement per occasion), but I'm not sure how to approach the spatially varying gage height (8 measurements per occasion). Any thoughts or resources are appreciated.
Thanks,
Ben

Re: state-specific time varying design covariate

PostPosted: Fri Dec 02, 2022 7:18 pm
by jlaake
So does space align with state? Not clear how you align space to animals. The merge function only works with time and group. Has never been programmed to include state. You can create a dataframe that aligns with the design data and then use cbind function to create a new design dataframe.

Re: state-specific time varying design covariate

PostPosted: Mon Dec 05, 2022 10:12 am
by bmarcek
Yes, space does align with state. As it stands, I have monthly encounter histories for ~1500 individuals and I'd like to understand if transitions among spatial states may be related to environmental conditions (e.g., mean monthly temperature or gage height). Essentially, does temperature or gage height affect the probability that individuals may move from one spatial state to another (e.g., A to B). It seems like this would not be dissimilar from the island example in chapter 10 of the Mark book (island size or inter-island distance affecting transition probabilities) but with the state-specific variables being time-varying. Hopefully, that helps clarify what I'm looking for but please let me know if you need additional information.
Thanks for the assistance.
Ben

Re: state-specific time varying design covariate

PostPosted: Mon Dec 05, 2022 12:00 pm
by bmarcek
I just stumbled across a post with a similar question here:
viewtopic.php?f=54&t=3410#p11112

Would it be possible to then incorporate time into this? Something like,
Code: Select all
ddl$Psi$temp[ddl$Psi$stratum == "A" & ddl$Psi$tostratum == "B" & ddl$Psi$time == 1] = 10
ddl$Psi$temp[ddl$Psi$stratum == "A" & ddl$Psi$tostratum == "B" & ddl$Psi$time == 2] = 12


etc.

Obviously, there are many permutations as the number of possible transitions and the number of occasions increases, but that's a coding issues that shouldn't be overly difficult to overcome.
Again, I appreciate any thoughts or advice on this issue.
Thanks,
ben

Re: state-specific time varying design covariate

PostPosted: Wed Dec 07, 2022 9:20 pm
by jlaake
Sure you can do it that way or you can create a dataframe with the covariate data, read it in and then merge it with the design data. Here is an example using the mstrata data in RMark where I create a dataframe in R that is space and time dependent (I used temp but could be your gage data). I created merge_design.covariates to make sure that the order in the design data is maintained but you can use merge and then make sure to re-order as merge doesn't always maintain order. Look through code and comments below.

Code: Select all
library(RMark)
data(mstrata)
mstrata.processed=process.data(mstrata,model="Multistrata")
mstrata.ddl=make.design.data(mstrata.processed)
#examine design data for S
mstrata.ddl$S
#create a dataframe with temp being a function of state and time; this dataframe could be read into R
#instead of being created as in this dummy example
tempdata=data.frame(st=paste(c("A","B","C"),rep(1:3,each=3),sep=""),temp=1:9)
#now create an st field in the design data for S
mstrata.ddl$S$st=paste(mstrata.ddl$S$stratum,mstrata.ddl$S$time,sep="")
#merge design data with temp data
mstrata.ddl$S=merge(mstrata.ddl$S,tempdata,by="st")
# examine S design data. Notice how it is no longer in the same order; merge doesn't force the order
mstrata.ddl$S
# reorder based on par.index - it should always be maintained in this order
mstrata.ddl$S=mstrata.ddl$S[order(mstrata.ddl$S$par.index),]
#remove st field which is no longer needed
mstrata.ddl$S$st=NULL
# use temp in model - don't expect useful results as this is imaginary data
mark(mstrata.processed,mstrata.ddl,model.parameters=list(S=list(formula=~temp)))


Re: state-specific time varying design covariate

PostPosted: Fri Dec 09, 2022 4:10 pm
by bmarcek
Thanks, I will give that a try.
Ben