Skip to content

How to calculate VIF (variance inflation factor) with sdmTMB models #202

Answered by seananderson
seananderson asked this question in Q&A
Discussion options

You must be logged in to vote

Although there's no built-in method currently, it's fairly simple to calculate yourself on the input data frame.

From the Wikipedia entry:

where $R^2_i$ is the coefficient of determination of the regression equation in step one, with $X_{i}$ on the left hand side, and all other predictor variables (all the other X variables) on the right hand side.

Here's a little function to do that and an example:

vif <- function(x) {
  v <- vapply(seq_along(x), function(i) {
    rsq <- summary(lm(x[[i]] ~ . , data = x[,-i, drop = FALSE]))$r.squared
    1 / (1 - rsq)
  }, FUN.VALUE = numeric(1L))
  setNames(v, colnames(x))
}


# subset your data frame to include just the predictors in the model:
d <- m…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by seananderson
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants