Background re: expand.factor.strategy='model.matrix' vs. 'inla'

477 views
Skip to first unread message

Tyler Scott

unread,
Oct 27, 2015, 3:51:59 PM10/27/15
to R-inla discussion group
Hello,
  I am writing with regards to a topic that I know has been discussed on the group page before, an error associated with factor covariates in an spde model:

Error in inla(form, family = "gaussian", data = inla.stack.data(stk.dat),  : 
  With control.fixed = list(expand.factor.strategy='model.matrix'), then NA's in factor are not allowd. Please use strategy 'inla' instead.

My question is this: why does this error occur when there are no NA values in my data? Is this just because some mesh vertices are at unobserved points?

Secondly, when I do switch the setting to expand.factor.strategy='inla' why would the following error occur even though the factor has 2 or more levels?
 
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

Is there a recommended approach for dealing with this problem(s) aside from using 0/1 dummy variables to get around it, or something obvious that I am overlooking? Thanks for your time.

-tyler scott

Tyler Scott
Assistant Professor of Public Policy
Department of Public Administration and Policy
School of Public and International Affairs, University of Georgia


Finn Lindgren

unread,
Oct 27, 2015, 4:20:06 PM10/27/15
to Tyler Scott, R-inla discussion group
For the first question, see
for a partial explanation.  The issue is that factors and NA doesn't always play nicely, in particular in combination with inla.stack, which has as one of its purposes to add NA:s where needed to construct certain linear combination predictors. The "inla" expansion strategy should be stable in that situation, but the default one isn't.  Factors are _always_ expanded as 0/1 dummy variables internally, it's just that in this case it needs more specific guidance about exactly how to do that expansion.

I'm not sure why the second error occurs.  

Finn
--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at http://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.

Finn Lindgren

unread,
Oct 27, 2015, 4:24:00 PM10/27/15
to Tyler Scott, R-inla discussion group
Further:
In the example in the FAQ, the automatic intercept is used, which you should not do with spde/inla.stack models.  Do run the FAQ example with "-1" in the beginning of the model formula to see how the expansion strategy operates in that case, and then decide if you need an explicit "intercept covariate" or not; the factor may perhaps already give you the needed intercepts.

Finn

On 27 Oct 2015, at 19:51, Tyler Scott <tyler.and...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages