Density spline relationship with spatial covariate (ruggedness)

66 views
Skip to first unread message

Elsa Bussiere

unread,
Nov 2, 2017, 6:13:33 AM11/2/17
to secr
Thanks Murray for the past reply regarding hybrid mixture models (h2 and h3).

I would like to ask another question about the predict() function in secr package.

It seems that -- in the dataframe newdata -- not only the covariates of the Density parameter, but also that of g0 and sigma, must be specified. 

1. I don't really understand why It is necessary to specify covariates that affect g0 and sigma when I am only interested in the relationship between covariates and density?

2. An example: g0 ~1, D~spline(ruggedness, k=4) + session, sigma~h2 + session
I understand that ruggedness and session need to be specified in newdata. However, If I do not specify h2 as well, then I receive an error message:

rdh.m17.1 <- predict(m17.1, newdata = data.frame(ruggedness = seq(3.54,33.08, length =  1000), session = "A"))
Error in secr.lpredictor(formula = models[[x]], newdata = newdata, indx = parindices[[x]],  : 
  model covariate h2 not found in 'newdata'

I realised that once I add session= and h2= in the dataframe, the error message goes away, indepently of what I specify (ex: session = "J" although there is no J session), so clearly something is not working correctly.  When I do specify the correct session names, I do not see any difference between the graphs plotted, lettying me think that a default value is always used and that whatever I specify is ignored.
Would you please clarify what needs to be fed into the newdata dataframe of the predict() function, and what syntax to use?

Thanks very much
Kind regards, 

Elsa

Murray Efford

unread,
Nov 2, 2017, 6:38:00 AM11/2/17
to Elsa Bussiere, secr
1. That's the way it is. I think you'll find it is standard behaviour for the predict method of other models in R.
2. I guess this has to do with matching the internal numeric codes for factor levels rather than the levels themselves. If you create a new factor session = 'J' then its first level still has numeric code 1. Just guessing. You could investigate - I don't have time right now.
Murray

--
You received this message because you are subscribed to the Google Groups "secr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to secrgroup+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elsa Bussiere

unread,
Nov 10, 2017, 3:57:52 AM11/10/17
to Murray Efford, secr
Hi Murray, 

Thanks for your last email. 
I have a new question about plotting confidence intervals (shaded area) around the regression spline (relationship between density and spatial covariate).
Is there any easy way to do so in secr? 
This is the code I use to produce the regression spline from my model m35:
rdh.m35 <- predict(m35, newdata = data.frame(ruggedness = seq(0,200, length =  300), session =c("A","B", "C", "D", "E", "F"), h2 = 2) )
plot(seq(0,200, length =  300), bty="n", pch = 19, cex = 2, sapply(rdh.m35, "[", "D", "estimate"), xlab = "", ylab = "", yaxt = "n", xaxt = "n")

Please note (in relation to my previous question), that in the newdata dataframe of the predict() function, it was important to:
1) specify ALL covariates that are defined in the model 
Otherwise R returns error messages such as: 
Error in secr.lpredictor(formula = models[[x]], newdata = newdata, indx = parindices[[x]],  : 
  model covariate h2 not found in 'newdata'

2) mention ALL  factor levels for these covariates (eg session = "A" was incorrect)
Otherwise R returns error messages such as:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels


Thanks very much
Kind regards,

Elsa

Elsa Bussiere | PhD student

Animal Demography Unit | John Day Zoology Building 
Department of Biological Sciences | Upper Campus | University of Cape Town
Rondebosch 7701 | South Africa

Reply all
Reply to author
Forward
0 new messages