[R] fitting of all possible models

vaniscotte avanisco at univ-fcomte.fr
Fri Mar 16 15:09:39 CET 2007


Rense Nieuwenhuis a écrit :
> Dear Lukas,
>
> allthough I'm  intrigued by the purpose of what you are trying to do,  
> as mentioned by some of the other persons on this list, I liked the  
> challenge to write such a function.
>
> I came up with the following during some train-traveling this morning:
>
>
> tum <- function(x)
> 	{
> 		tum <- matrix(data=NA, nrow=2^x, ncol=x)
> 		
> 		for (i in 1:x)
> 			{
> 				tum[,i] <- c(rep(NA,2^i/2),rep(i+1,2^i/2))
> 			}
> 		
> 		return(tum)
> 	}
>
> ###
>
> all.models <- function(model)
> 	{
> 		npred <- length(model$coefficients) - 1
> 		matr.model <- tum(npred)
> 		output <- matrix(data=NA, nrow=2^npred, ncol=1)
> 	
> 		for (t in 2:2^npred)
> 		{
> 			preds <- names(model$coefficients)
> 			interc <- names(model$coefficients)[1]
> 			form <- as.formula(paste(". ~", paste(preds[na.omit(matr.model 
> [t,])],collapse="+")))
>
> 			model2 <- update(model, form)
> 			output[t,] <- mean(resid(model2)^2)
> 		}
> 	
> 	return(output)
> 	
> 	}
>
> ##
>
>
> As you can see, I used a helper-function (tum, "the ultimate matrix")  
> to the actual function. Also, I wrote it using lm instead of glm, but  
> I suppose that you can easily alter that. As well, the function now  
> returns just some regular fit-measurement. But that is not all that  
> essential, I think.
>
> The main point is: it works! Using this on my G4 mac, with a lm of 10  
> predictors and 18 cases, it returns the output quite fast (<1 minute).
>
> I hope you can put this to use. It needs some easy adapting to your  
> specific needs, but I don't expect that to be a problem. If you need  
> help with that, please contact me.
>
> I'd appreciate to hear from you, if this function is helpful in any way.
>
> sincerely,
>
> Rense Nieuwenhuis
>
>
>
>
>
> On Feb 27, 2007, at 8:46 , Indermaur Lukas wrote:
>
>   
>> Hi,
>> Fitting all possible models (GLM) with 10 predictors will result in  
>> loads of (2^10 - 1) models. I want to do that in order to get the  
>> importance of variables (having an unbalanced variable design) by  
>> summing the up the AIC-weights of models including the same  
>> variable, for every variable separately. It's time consuming and  
>> annoying to define all possible models by hand.
>>
>> Is there a command, or easy solution to let R define the set of all  
>> possible models itself? I defined models in the following way to  
>> process them with a batch job:
>>
>> # e.g. model 1
>> preference<- formula(Y~Lwd + N + Sex + YY)
>> # e.g. model 2
>> preference_heterogeneity<- formula(Y~Ri + Lwd + N + Sex + YY)
>> etc.
>> etc.
>>
>>
>> I appreciate any hint
>> Cheers
>> Lukas
>>
>>
>>
>>
>>
>> °°°
>> Lukas Indermaur, PhD student
>> eawag / Swiss Federal Institute of Aquatic Science and Technology
>> ECO - Department of Aquatic Ecology
>> Überlandstrasse 133
>> CH-8600 Dübendorf
>> Switzerland
>>
>> Phone: +41 (0) 71 220 38 25
>> Fax    : +41 (0) 44 823 53 15
>> Email: lukas.indermaur at eawag.ch
>> www.lukasindermaur.ch
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>     
>
>
> 	[[alternative HTML version deleted]]
>
>   
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   
Dear Lukas,

In order to define the set of all possible models I used the function combos() of the hier.part package. 
The function all.regs() of the same package fit all the possible models.

God luck

Amélie Vaniscotte (Phd)

Department of Environmental Biology
UsC INRA EA3184MRT
Institute for Environmental Sciences and Technology

University of Franche-comté
Place Leclerc, 25030 Besançon cedex
FRANCE
Tel. :   +33 (0)381 665 714
Fax :  +33 (0)381 665 797
E-m at il: amelie.vaniscotte at univ-fcomte.fr
http://lbe.univ-fcomte.fr/



More information about the R-help mailing list