How can one compute the PRESS diagnostic?

If an answer is illustrated with R code, it would be even more gratefully accepted.

• What is the statistical question? – utobi Nov 29 '16 at 14:34
• @utobi the question is "how to calculate PRESS". In my opinion this is a perfectly valid question. – Tim Nov 29 '16 at 15:15
• @Tim as far as I understood the question is "how to calculate PRESS using R", which sounds like performing routine operations within a statistical computing platform, hence off-topic. – utobi Nov 29 '16 at 15:45
• @utobi In its current state, this question seems on-topic to me. I don't think the question is demanding only the R code but the details of computation. It is quite common for "how to compute" questions to express a language preference, but answerers are not obliged to respect it. That is, an answer without R code - using pseudo-code or Python or no code altogether - would still be a valid answer to the question as written. I think the line gets crossed when we see a question that asks how to implement in R. – Silverfish Nov 29 '16 at 19:45
• @Tim Thanks for your great answer and I sorry about the way me asking this question.New to the neighbourhood, and still learning. – AkiyamaYukari Nov 30 '16 at 9:51

For linear regression model, given the hat matrix

$$H = X (X'X)^{-1} X'$$

and residuals $e_i$, PRESS can be calculated as (see also here):

$$\mathrm{PRESS} = \sum_i \left( \frac{e_i}{1-h_{ii}}\right)^2$$

This can be easily translated to the simple function:

PRESS <- function(linear.model) {
pr <- residuals(linear.model)/(1 - lm.influence(linear.model)\$hat)
sum(pr^2)
}