Re: [R] Combination of variables

From: hadley wickham <h.wickham_at_gmail.com>
Date: Mon 22 Jan 2007 - 14:05:12 GMT

You might find the following code useful. It's part of a package I'm developing for interactive model exploration.

Hadley

# Generate all models
# Fit all combinations of x variables ($2^p$)
#
# This technique generalises \code{\link{fitbest}}.  While it is much
# slower it will work for any type of model.
#
# @arguments vector y values
# @arguments matrix of x values
# @arguments method used to fit the model, eg
\code{\link{lm}},\code{\link[MASS]{rlm}} # @keyword regression
fitall <- function(y, x, method=lm, ...) {

        data <- cbind(y=y, x)

        combs <- do.call(expand.grid, rep(list(c(FALSE, TRUE)), ncol(x)))[-1, ]

	vars <- apply(combs, 1, function(i) names(x)[i])
	form <- paste("y ~ ", lapply(vars, paste, collapse=" + "), sep = "")
	form <- lapply(form, as.formula)

	models <- lapply(form, function(f) eval(substitute(method(f,
data=data, ...), list(f=f, data=data, method=method))))
	names(models) <- 1:length(models)
	class(models) <- c("ensemble", class(models))
	models

}
# Generate best linear models
# Use the leaps package to generate the best subsets.
#
# @arguments model formula
# @arguments data frame
# @arguments number of subsets of each size to record
# @arguments other arguments passed to \code{\link[leaps]{regsubsets}}
# @keyword regression
fitbest <- function(formula, data, nbest=10, ...) {
	b <- regsubsets(formula, data=data, nbest=nbest, ...)
	mat <- summary(b, matrix.logical = TRUE)$which

	intercept <- c("", "-1")[as.numeric(mat[,1])]
	vars <- apply(mat[,-1], 1, function(x) colnames(mat[, -1])[x])
	form <- paste(formula[[2]], " ~ ", lapply(vars, paste, collapse=" +
"), sep = "")
	form <- lapply(form, as.formula)

	models <- lapply(form, function(f) eval(substitute(lm(f, data=data),
list(f=f, data=data))))
	names(models) <- 1:length(models)
	class(models) <- c("ensemble", class(models))
	models

}

On 22 Jan 2007 08:51:22 -0000, anil kumar rohilla <anil_rohilla@rediffmail.com> wrote:
>
> Hi,
> List , i have 6 predictor variables and i want to make possible combinations of these 6 predictors ,all the data is in matrix form ,
> if i am having 6 predictors than possible combination of sets are 64 2 power 6, or 63 ,whatever it may be i want to store the result in another variable to each combination and that i want to put in some model ,
>
> i want to put every combination in some model ,please help me how to find suitable combinations of these variables ........
>
> i was not able to do this .
> Any package is there in R which can help me in this regards
> any help .
> thanks in Advance
>
>
> ANIL KUMAR( METEOROLOGIST)
> LRF SECTION
> NATIONAL CLIMATE CENTER
> ADGM(RESEARCH)
> INDIA METEOROLOGICAL DEPARTMENT
> SHIVIJI NAGAR
> PUNE-411005 INDIA
> MOBILE +919422023277
> anilkumar@imdpune.gov.in
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>



R-help@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. Received on Tue Jan 23 01:18:15 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 22 Jan 2007 - 14:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.