Predict() for additive model

889 views
Skip to first unread message

Sean Wineland

unread,
Jan 17, 2018, 10:28:38 AM1/17/18
to unmarked
Hello,

If I have an additive model, for example:

> fm3<-occu(~turb + cond ~road + publ,cadframe)

and want to plot occupancy and detection curves, is the predict function only going to select the first covariate? 

For example, when I go to plot the second occupancy covariate "publ" (%public land), using the following code:

> newdata.7<-data.frame(publ=seq(0, 1, by=0.1))
> proccfm3<-predict(fm3,type="state",newdata=newdata.3, appendData=TRUE)

The following error is returned:
Error in eval(predvars, data, env) : object 'road' not found

It seems as if the predict() function is only looking for the first predictor variable "road".

If I want to plot an occupancy curve for the covariate "publ", how would I do this? 

Thank you,

-Sean

Matthew Giovanni

unread,
Jan 17, 2018, 11:55:27 AM1/17/18
to unma...@googlegroups.com
Hi Sean, the dataframe should contain all the occupancy covariates in your model. Eg set road as equal to the median road value if it's continuous and you want to only estimate effects of and predictions for public land.

If you want to estimate additive or interactive effects and predictions for road w public land, then set road as equal to y/n or present/absent if it's categorical, or a min/max range if it's continuous. I believe there are several examples in this group's threads and the unmarked documentation.

Matt

Matt Giovanni
608-320-9331
Midwest Biological Services
https://sites.google.com/site/matthewgiovanni/

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

Sean Wineland

unread,
Jan 21, 2018, 1:41:57 PM1/21/18
to unma...@googlegroups.com
Matt, 

Thank you for replying, I am still trying to figure out how to do this. My problem is that the predict() function seems to only select one of the covariates in an additive model. If I want to plot separate detection curves for an additive model, how would I do this?

For example: 

I have the following model:

fm2<-occu(~turb + cond ~fcover,cadframe)

and I want to plot detection curves for both sample covariates "turb" (turbidity) and "cond" (conductivity)

If I am interested in plotting a detection curve for "turb", I used the following code:
newdata.1<-data.frame(turb=seq(-0.5,10,by=0.1))
fm2.p<-predict(fm2,type="det",newdata=newdata.1,appendData=TRUE)

and get this error:
Error in eval(predvars, data, env) : object 'cond' not found

The predict() function seems to only search for one of the two predictor variables in the additive model, and in this case it is searching for "cond" instread of turbidity. I can go ahead and plot the detection curve for "cond" but would really like to plot both.

Is there a way to do this?

Any help is greatly appreciated.


--
You received this message because you are subscribed to a topic in the Google Groups "unmarked" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/unmarked/M0EW6V2xZEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to unmarked+unsubscribe@googlegroups.com.

Matthew Giovanni

unread,
Jan 21, 2018, 1:50:51 PM1/21/18
to unma...@googlegroups.com
Hi Sean. Your model contains two variables for detection so include both in the dataframe:

newdata.1<-data.frame(turb=seq(-0.5,10,by=0.1), cond=seq(min, max, by=??))

Matt

Matt Giovanni
608-320-9331
Midwest Biological Services
https://sites.google.com/site/matthewgiovanni/

Chris Sutherland

unread,
Jan 21, 2018, 2:16:34 PM1/21/18
to unma...@googlegroups.com
Sean,

Matt is correct. You have built a model that estimates the expected occupancy state based on multiple variables (2 in your case). So, to create a prediction,  you must provide values for both. When you are interested in one, you still need to provide a value for the other because yoy are using the fitted model to make the prediction, so each covariate must be present. Typically one uses the mean of the other covariate (you predict Y holding X1 constant while varying X2). Like this:

newdata.1<-data.frame(turb=seq(min(cadframe&turb),max(cadframe$turb),length=50), cond=mean(cadframe$cond))


On Jan 21, 2018 1:50 PM, "Matthew Giovanni" <matthew...@gmail.com> wrote:
Hi Sean. Your model contains two variables

Sean Wineland

unread,
Jan 21, 2018, 2:46:03 PM1/21/18
to unma...@googlegroups.com
Matt,

Thank you, and maybe I am missing something entirely, but doing this produces identical graphs for both conductivity and turbidity.
Both covariates are standardized and have different min/max ranges, so I had to make both the same sequence.

I guess I should be asking more of an "am I doing this right" question. I am very new to occupancy modeling in general.

Is the predict function essentially the same as what was explained in the UVM (Donovan, T. M. and J. Hines. 2007. Exercises in Occupancy Estimation and Modeling.), where it is using the coefficients (betas) output by BOTH covariates in an additive to model to obtain estimates of detection probability?

Using the same model mentioned above, for example, where B implies the coefficients for each covariate:
Is this what the predict() function performs?

Logit (p) =Β(intercept) +B(turb)* turb +B(cond)*cond

Site x (p)= exp(Β(intercept) +B(turb)* turb +B(cond)*cond)/(1+exp(Β(intercept) +B(turb)* turb +B(cond)*cond)

Should I be making two separate graphs for detection probability since the predictions are based on both covariates? If not, and I should just be making one, what does one make the x-axis?

I apologize if this forum is the wrong place for such questions, but I am simply confused on this.

Thank you,


Matthew Giovanni

unread,
Jan 21, 2018, 3:44:03 PM1/21/18
to unma...@googlegroups.com
Hi Sean. I believe Chris Sutherland answered your question earlier w a more complete and accurate response. My last response didn't have the correct suggestion for coding: you should hold the covariate (that you're not making predictions for) at its mean or median in your dataframe.


Matt Giovanni
608-320-9331
Midwest Biological Services
https://sites.google.com/site/matthewgiovanni/

Sean Wineland

unread,
Jan 23, 2018, 6:12:31 PM1/23/18
to unma...@googlegroups.com
Hi Matt and Chris, and anyone else following this thread,

I am unsure if this is the correct way to do this. For example, below I coded my dataframe based on Chris' suggestion. I modified the code slightly because it would not let me use the "$" symbol as an operator:

> cadframe$turb
Error in cadframe$turb : $ operator not defined for this S4 class

Here is the code I used:

> newdata.1<-data.frame(cond=seq(-1,4, by=0.01),turb=turb.mean)
> fm2.p<-predict(fm2,type="det",newdata=newdata.1,appendData=TRUE)
> head(fm2.p)
     Predicted           SE        lower        upper  cond    turb
1 2.200753e-32 4.447902e-31 1.377495e-49 3.516030e-15 -1.00 11.7906
2 2.172725e-32 4.391163e-31 1.361084e-49 3.468364e-15 -0.99 11.7906
3 2.145054e-32 4.335148e-31 1.344867e-49 3.421349e-15 -0.98 11.7906
4 2.117736e-32 4.279848e-31 1.328840e-49 3.374977e-15 -0.97 11.7906
5 2.090765e-32 4.225254e-31 1.313002e-49 3.329239e-15 -0.96 11.7906
6 2.064138e-32 4.171356e-31 1.297351e-49 3.284126e-15 -0.95 11.7906

Doing so gives unrealistic predicted values and the graph looks like a straight line at Y=0. 

I think setting the mean value to zero messes with the predict() function equation I mentioned above, where it is using the beta values (coefficients) to make predictions, which I will post again here:

Logit (p) =Β(intercept) +B(turb)* turb +B(cond)*cond

Site x (p)= exp(Β(intercept) +B(turb)* turb +B(cond)*cond)/(1+exp(Β(intercept) +B(turb)* turb +B(cond)*cond)


For example, I believe the predict function is looking for values of both turbidity and conductivity based on the above equation, and setting one to the mean is skewing the predicted values. 


If you only set the dataframe to include one of the covariates, it throws an error code that it can't find the other:

> newdata.1<-data.frame(cond=seq(-1,4, by=0.01))

> fm2.p<-predict(fm2,type="det",newdata=newdata.1,appendData=TRUE)

Error in eval(predvars, data, env) : object 'turb' not found


> newdata.1<-data.frame(turb=seq(-1,10, by=0.01))
> fm2.p<-predict(fm2,type="det",newdata=newdata.1,appendData=TRUE)
Error in eval(predvars, data, env) : object 'cond' not found

So, with the same goal in mind to graph separate detection curves for each covariate based on the additive model for detection probability:

 fm2<-occu(~turb + cond ~fcover,cadframe)

Is there currently no way to graph separate detection curves from an additive model using the unmarked functions?

Should I simply try to do the equation by hand in R by removing the values being multiplied by the coefficient of the covariate I am not interested in graphing? 

For example, If I am interested in graphing a detection curve for turbidity, would I modify the above equation to read: 

Site x (p)= exp(Β(intercept) +B(turb)* turb +B(cond))/(1+exp(Β(intercept) +B(turb)* turb +B(cond))

where the values for conductivity are removed?

Thanks,

-Sean

Matthew Giovanni

unread,
Jan 23, 2018, 7:33:27 PM1/23/18
to unma...@googlegroups.com
From just a quick look, I don't think you back-transformed your detection predictions. It would probably help if you included a full section of working code and data set or subset.


Matt Giovanni
608-320-9331
Midwest Biological Services
https://sites.google.com/site/matthewgiovanni/
Message has been deleted

Prashant Mahajan

unread,
Mar 4, 2023, 3:27:10 AM3/4/23
to unmarked
Hello,
I am using the single-season occupancy models, I have both continuous and categorical covariates, which I am using to model both detection and occupancy probability. However, I have an issue regarding the use of categorical covariates. For example, how should I keep my categorical variable constant while preparing the new data frame? Suppose my model is: 

 occu(formula = ~cover+habitat ~1, data = data)

Where cover is continuous, and the Habitat is categorical, I want to know the effect of the habitat type on the detection probability. I have four types of habitat in 100 sites with six replicates each. How should I construct a data frame while keeping categorical covariate constant as explained in the above examples using continuous covariates?

Thank You,
Prashant

Matt,
Logit (p) =Β(intercept) +B(turb)* turb +B(cond)*condSite x (p)= exp(Β(intercept) +B(turb)* turb +B(cond)*cond)/(1+exp(Β(intercept) +B(turb)* turb +B(cond)*cond)
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "unmarked" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/unmarked/M0EW6V2xZEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to unmarked+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "unmarked" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/unmarked/M0EW6V2xZEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to unmarked+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "unmarked" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/unmarked/M0EW6V2xZEU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to unmarked+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages