chrysoparia
chrysoparia

Reputation: 31

How to use the predict() function in the R package "pscl" with categorical predictor variables

I'm fitting count data (number of fledgling birds produced per territory) using zero-inflated poisson models in R, and while model fitting is working fine, I'm having trouble using the predict function to get estimates for multiple values of one category (Year) averaged over the values of another category (StudyArea). Both variables are dummy coded (0,1) and are set up as factors. The data frame sent to the predict function looks like this:

  Year_d StudyArea_d
1      0         0.5
2      1         0.5

However, I get the error message:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

If instead I use a data frame such as:

  Year_d StudyArea_d
1      0           0
2      0           1
3      1           0
4      1           1

I get sensible estimates of fledgling counts per year and study site combination. However, I'm not really interested in the effect of study site (the effect is small and isn't involved in an interaction), and the year effect is really what the study was designed to examine.

I have previously used similar code to successfully get estimated counts from a model that had one categorical and one continuous predictor variable (averaging over the levels of the dummy-coded factor), using a data frame similar to:

  VegHeight StudyArea_d
1      0           0.5
2      0.5         0.5
3      1           0.5
4      1.5         0.5

So I'm a little confused why the first attempt I describe above doesn't work.

I can work on constructing a reproducible example if it would help, but I have a hunch that I'm not understand something basic about how the predict function works when dealing with factors. If anyone can help me understand what I need to do to get estimates at both levels of one factor, and averaged over the levels of another factor, I would really appreciate it.

Upvotes: 3

Views: 476

Answers (0)

Related Questions