s-news
[Top] [All Lists]

SUMMARY (II): ...speed up a for loop (leave-one-out procedure)

To: <s-news@lists.biostat.wustl.edu>
Subject: SUMMARY (II): ...speed up a for loop (leave-one-out procedure)
From: "Javier Seoane" <seoane@ebd.csic.es>
Date: Mon, 27 May 2002 09:39:02 +0200
Thanks again to Dimitris C. Rizopoulos who suggested a different, more 
efficient, way to obtain leave-one-out predictions for a series of models 
(decreasing computing time from 250 to 49 min when three models were used). His 
kind reply is copied below:

======== Dimitris C. Rizopoulos (25/05/2002)
model.1 <- lm(Fuel ~ Weight + Disp., fuel.frame)
model.2 <- lm(Fuel ~ Weight + Mileage, fuel.frame)
model.3 <- lm(Fuel ~ Weight + Type, fuel.frame)
model.4 <- lm(Fuel ~ Weight + Type+Mileage,
fuel.frame)
model.5 <- lm(Fuel ~ Weight + Type+Mileage+Disp.,
fuel.frame)
models <- list(model.1, model.2,
model.3,model.4,model.5) 
##########
tic <- proc.time()
n <- nrow(fuel.frame)
predictions <- list(NULL)
for(i in 1:n) {
predictions[[i]] <- lapply(models, function(x){
model <- update(x, data = fuel.frame[-i,])
predict(model, newdata = fuel.frame[i,])})
}
tac <- proc.time() - tic
results <- matrix(unlist(predictions), nrow=n,
ncol=length(models), byrow=T)
dimnames(results) <- list(paste("observation", 1:n),
paste("model.", 1:length(models), sep=""))
CV.results <- colSums((results-fuel.frame$Fuel)^2)
tac
results
CV.results

===================================
Javier Seoane
Department of Applied Biology
Estación Biológica de Doñana, CSIC
Avda. María Luisa s/n
41013, Sevilla
SPAIN


<Prev in Thread] Current Thread [Next in Thread>