Hi Tim,

Here is a rather clumsy way of going about your task:

# ---------- example code ---------
func.getY.getX.return.lm <- function(Y,  X.matrix , lm.id.vec)
{
# gets a Y, a vec of T/F and a X.matrix
# performs lm
# and returns output
 potential.X.size <- length(lm.id.vec) + 1
 lm.data <- data.frame(Y, X.matrix[,lm.id.vec])
 lm1 <- lm(Y ~ . ,data = lm.data)
return(lm1)
}

data(mtcars)
X <- mtcars[, -1]
Y <- mtcars[, 1]
X.toUse <- sample(c(T,F), 10, T)

func.getY.getX.return.lm(Y, X , X.toUse )

# ---------- example code ---------


Now what you will need to do is:
1) Create an X matrix that includes the vectors of all the interactions you
will be interested in.
2) make a matrix of TRUE/FALSE id's for the variables you would like to use.
3) go through that matrix and build the models (using, for example, the
function I gave above)
4) insert the output to a big list(), and on that list perform the checks
that interest you for finding your model.

As Frank Harrell and the others have mentioned, you are walking on VERY
shaky grounds (in terms of the stability of your models
predictive/explanatory power).

In case you develop the code more, I'd be curious to see how you did it.

Good luck,
Tal






----------------Contact
Details:-------------------------------------------------------
Contact me: Tal.Galili@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Wed, May 19, 2010 at 8:48 PM, Tim Clark <mudiver1200@yahoo.com> wrote:

> Tal,
>
> No, I am definitely not wanting to generate 7! models.  At least not with
> my current dataset.  That would only be if I was running all first order
> terms and all possible interactions.  In this example I am only wanting to
> run interactions for the variables year:site, which should give 2^7+32=160
> possible models.  What I am really asking is if everyone writes out all of
> their models of interest by hand, or if there is some function that has been
> written that makes it easier to generate all the models.  I keep making
> mistakes when writing out lots of models, and was hoping for something that
> would automate the process so that I was sure I didn't screw up in the
> formulas.  This may just require more practice on my part, but it seems like
> such a common problem that someone would have written a function to do it.
> It looks like a function could be written using combn() with different
> number of elements.  I will see if I can come up with something.
>
> I am using AIC values to pick the best models and the function modavg() in
> the package AICcmodavg to generate model averages.  I have read a lot about
> the problems of stepwise selection, so I am trying to find something besides
> regsubsets() from leaps package.
>
> Thanks,
>
> Tim
>
>
> Tim Clark
> Department of Zoology
> University of Hawaii
>
> --- On *Wed, 5/19/10, Tal Galili <tal.galili@gmail.com>* wrote:
>
>
> From: Tal Galili <tal.galili@gmail.com>
> Subject: Re: [R] Generating all possible models from full model
> To: "Tim Clark" <mudiver1200@yahoo.com>
> Cc: r-help@r-project.org
> Date: Wednesday, May 19, 2010, 12:47 AM
>
>  Hi Tim,
> So if I understand you correctly, you are talking about 7! models, that's
> hell of alot, are you considering model-selection/multiple-comparisons
> issues when you are picking your models ?
> If you are hoping to do cross validation on such a variaty of models, you
> might find out it wouldn't scale for larger problems.
>
>  There is the
> regsubsets from the {leaps} package.
> Which can also work with biglm and bigglm objects.  I am not sure what
> alternative exists for other glm objects, but it's worth checking.
>
> I can imagine you can write a function that will create all the variable
> combinations using a combo of the functions combn (to create all the
> combinations) with eval+parse.
> But asI wrote, I think you're issue here is the model selection, not just
> the creation of all the models.
>
> Best,
> Tal
>
>
>
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili@gmail.com<http://us.mc361.mail.yahoo.com/mc/compose?to=Tal.Galili@gmail.com>|  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
>
> ----------------------------------------------------------------------------------------------
>
>
>
>
> On Wed, May 19, 2010 at 2:00 PM, Tim Clark <mudiver1200@yahoo.com<http://us.mc361.mail.yahoo.com/mc/compose?to=mudiver1200@yahoo.com>
> > wrote:
>
>>   Not necessarily.  In the example I included:
>>
>> manta~year*site
>>
>> in the model, which includes both first order terms and interactions:
>>
>> manta~year+site+year:site
>>
>> I am just wanting to know if there is an easier way than writing out all
>> the possible models long-hand given a full model with all desired terms,
>> where some terms may have interactions and others don't.
>>
>>
>>
>>
>>
>>
>> Tim Clark
>> Department of Zoology
>> University of Hawaii
>>
>> --- On *Tue, 5/18/10, Tal Galili <tal.galili@gmail.com<http://us.mc361.mail.yahoo.com/mc/compose?to=tal.galili@gmail.com>
>> >* wrote:
>>
>>
>> From: Tal Galili <tal.galili@gmail.com<http://us.mc361.mail.yahoo.com/mc/compose?to=tal.galili@gmail.com>
>> >
>> Subject: Re: [R] Generating all possible models from full model
>> To: "Tim Clark" <mudiver1200@yahoo.com<http://us.mc361.mail.yahoo.com/mc/compose?to=mudiver1200@yahoo.com>
>> >
>> Cc: r-help@r-project.org<http://us.mc361.mail.yahoo.com/mc/compose?to=r-help@r-project.org>
>> Date: Tuesday, May 18, 2010, 9:40 PM
>>
>>
>>  Do you mean only all of the FIRST order models (that is, without
>> interactions) ?
>>
>>
>>
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili@gmail.com<http://mc/compose?to=Tal.Galili@gmail.com>|  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>>
>> ----------------------------------------------------------------------------------------------
>>
>>
>>
>>
>> On Wed, May 19, 2010 at 7:38 AM, Tim Clark <mudiver1200@yahoo.com<http://mc/compose?to=mudiver1200@yahoo.com>
>> > wrote:
>>
>>> Is there a function that will allow me to run all model iterations if I
>>> specify a full model?  I am using information criteria to choose between
>>> possible candidate models.  I have been writing out all possible model
>>> combinations by hand, and I am always worried that I am missing models or
>>> have made a mistake somewhere.  It is also difficult to alter models if I
>>> want to change a term.  For example, below are the set of models I would
>>> like to run.  Is there a way to specify the full model and have R generate
>>> the rest?  I.e. specify
>>>
>>>  m1234567<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>> and have R run all the other models.
>>>
>>>
>>> library(MASS)
>>>
>>> #Intercept only
>>>  m0<-glm.convert(glm.nb(mantas~1,data=mydata))
>>>
>>> #One term - 7 models
>>>  #Manta abundance is greater at one of the two sites
>>>  m1<-glm.convert(glm.nb(mantas~site,data=mydata))
>>>  #Manta abundance increases each year as the population increases in size
>>> due to births or immigration being greater than deaths and emmigration
>>>  m2<-glm.convert(glm.nb(mantas~year,data=mydata))
>>>  #Manta abundances increases during part of the year due to seasonal
>>> cycles in resources (mates, food)
>>>  m3<-glm.convert(glm.nb(mantas~cosmonth,data=mydata))
>>>  m4<-glm.convert(glm.nb(mantas~sinmonth,data=mydata))
>>>  #Manta abundance decreases with increased lunar phase
>>>  m5<-glm.convert(glm.nb(mantas~coslunar, data=mydata))
>>>  m6<-glm.convert(glm.nb(mantas~sinlunar, data=mydata))
>>>  #Manta abundance increases with increased levels of plankton
>>>  m7<-glm.convert(glm.nb(mantas~plankton,data=mydata))
>>>
>>> #Two terms - 21 models
>>>  m12<-glm.convert(glm.nb(mantas~site*year, data=mydata))   #Interaction
>>> term to account for hotel being closed at Keauhou for some years
>>>  m13<-glm.convert(glm.nb(mantas~site+cosmonth,data=mydata))
>>>  m14<-glm.convert(glm.nb(mantas~site+sinmonth,data=mydata))
>>>  m15<-glm.convert(glm.nb(mantas~site+coslunar,data=mydata))
>>>  m16<-glm.convert(glm.nb(mantas~site+sinlunar,data=mydata))
>>>  m17<-glm.convert(glm.nb(mantas~site+plankton,data=mydata)) #Should this
>>> have an interaction term?  Plankton may varry by site
>>>
>>>  m23<-glm.convert(glm.nb(mantas~year+cosmonth,data=mydata))
>>>  m24<-glm.convert(glm.nb(mantas~year+sinmonth,data=mydata))
>>>  m25<-glm.convert(glm.nb(mantas~year+coslunar,data=mydata))
>>>  m26<-glm.convert(glm.nb(mantas~year+sinlunar,data=mydata))
>>>  m27<-glm.convert(glm.nb(mantas~year+plankton,data=mydata))
>>>
>>>  m34<-glm.convert(glm.nb(mantas~cosmonth+sinmonth,data=mydata))
>>>  m35<-glm.convert(glm.nb(mantas~cosmonth+coslunar,data=mydata))
>>>  m36<-glm.convert(glm.nb(mantas~cosmonth+sinlunar,data=mydata))
>>>  m37<-glm.convert(glm.nb(mantas~cosmonth+plankton,data=mydata))
>>>  #Interaction term?  Plankton may vary by season
>>>
>>>  m45<-glm.convert(glm.nb(mantas~sinmonth+coslunar, data=mydata))
>>>  m46<-glm.convert(glm.nb(mantas~sinmonth+sinlunar, data=mydata))
>>>  m47<-glm.convert(glm.nb(mantas~sinmonth+plankton, data=mydata))
>>> #Interaction term?  Plankton may vary by season
>>>
>>>  m56<-glm.convert(glm.nb(mantas~coslunar+sinlunar, data=mydata))
>>>  m57<-glm.convert(glm.nb(mantas~coslunar+plankton, data=mydata))
>>>
>>>  m67<-glm.convert(glm.nb(mantas~sinlunar+plankton, data=mydata))
>>> #Interaction term?  Plankton may have lunar cycles
>>>
>>> #Three terms - 35 models
>>>  m123<-glm.convert(glm.nb(mantas~site*year+cosmonth, data=mydata))
>>>  m124<-glm.convert(glm.nb(mantas~site*year+sinmonth, data=mydata))
>>>  m125<-glm.convert(glm.nb(mantas~site*year+coslunar, data=mydata))
>>>  m126<-glm.convert(glm.nb(mantas~site*year+sinlunar, data=mydata))
>>>  m127<-glm.convert(glm.nb(mantas~site*year+plankton, data=mydata))
>>>
>>>  m134<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth,data=mydata))
>>>  m135<-glm.convert(glm.nb(mantas~site+cosmonth+coslunar,data=mydata))
>>>  m136<-glm.convert(glm.nb(mantas~site+cosmonth+sinlunar,data=mydata))
>>>  m137<-glm.convert(glm.nb(mantas~site+cosmonth+plankton,data=mydata))
>>>
>>>  m145<-glm.convert(glm.nb(mantas~site+sinmonth+coslunar,data=mydata))
>>>  m146<-glm.convert(glm.nb(mantas~site+sinmonth+sinlunar,data=mydata))
>>>  m147<-glm.convert(glm.nb(mantas~site+sinmonth+plankton,data=mydata))
>>>
>>>  m156<-glm.convert(glm.nb(mantas~site+coslunar+sinlunar,data=mydata))
>>>  m157<-glm.convert(glm.nb(mantas~site+coslunar+plankton,data=mydata))
>>>
>>>  m167<-glm.convert(glm.nb(mantas~site+sinlunar+plankton,data=mydata))
>>>
>>>  m234<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth,data=mydata))
>>>  m235<-glm.convert(glm.nb(mant as~year+cosmonth+coslunar,data=mydata))
>>>
>>>  m236<-glm.convert(glm.nb(mantas~year+cosmonth+sinlunar,data=mydata))
>>>  m237<-glm.convert(glm.nb(mantas~year+cosmonth+plankton,data=mydata))
>>>
>>>  m245<-glm.convert(glm.nb(mantas~year+sinmonth+coslunar,data=mydata))
>>>  m246<-glm.convert(glm.nb(mantas~year+sinmonth+sinlunar,data=mydata))
>>>  m247<-glm.convert(glm.nb(mantas~year+sinmonth+plankton,data=mydata))
>>>
>>>  m256<-glm.convert(glm.nb(mantas~year+coslunar+sinlunar,data=mydata))
>>>  m257<-glm.convert(glm.nb(mantas~year+coslunar+plankton,data=mydata))
>>>
>>>  m267<-glm.convert(glm.nb(mantas~year+sinlunar+plankton,data=mydata))
>>>
>>>  m345<-glm.convert(glm.nb(mantas~cosmonth+sinmonth+coslunar,data=mydata))
>>>  m346<-glm.convert(glm.nb(mantas~cosmonth+sinmonth+sinlunar,data=mydata))
>>>  m347<-glm.convert(glm.nb(mantas~cosmonth+sinmonth+plankton,data=mydata))
>>>
>>> &
>>> nbsp;m356<-glm.convert(glm.nb(mantas~cosmonth+coslunar+sinlunar,data=mydata))
>>>
>>>  m357<-glm.convert(glm.nb(mantas~cosmonth+coslunar+plankton,data=mydata))
>>>
>>>  m367<-glm.convert(glm.nb(mantas~cosmonth+sinlunar+plankton,data=mydata))
>>>
>>>  m456<-glm.convert(glm.nb(mantas~sinmonth+coslunar+sinlunar,data=mydata))
>>>  m457<-glm.convert(glm.nb(mantas~sinmonth+coslunar+plankton,data=mydata))
>>>
>>>  m467<-glm.convert(glm.nb(mantas~sinmonth+sinlunar+plankton,data=mydata))
>>>
>>>  m567<-glm.convert(glm.nb(mantas~coslunar+sinlunar+plankton,data=mydata))
>>>
>>> #Four terms - 34 models
>>>  m1234<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth,
>>> data=mydata))
>>>  m1235<-glm.convert(glm.nb(mantas~site*year+cosmonth+coslunar,
>>> data=mydata))
>>>  m1236<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinlunar,
>>> data=mydata))
>>>  m1237<-glm.convert(glm.nb(mantas~site*year+cosmonth+plankton,
>>> data=mydata))
>>>
>>>  m1245<-glm.convert(glm.nb(mantas~site*year+sinmonth+coslunar,
>>> data=mydata))
>>>  m1246<-glm.convert(glm.nb(mantas~site*year+sinmonth+sinlunar,
>>> data=mydata))
>>>  m1247<-glm.convert(glm.nb(mantas~site*year+sinmonth+plankton,
>>> data=mydata))
>>>
>>>  m1256<-glm.convert(glm.nb(mantas~site*year+coslunar+sinlunar,
>>> data=mydata))
>>>  m1257<-glm.convert(glm.nb(mantas~site*year+coslunar+plankton,
>>> data=mydata))
>>>
>>>  m1267<-glm.convert(glm.nb(mantas~site*year+sinlunar+plankton,
>>> data=mydata))
>>>
>>>
>>>  m1345<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+coslunar,data=mydata))
>>>
>>>  m1346<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+sinlunar,data=mydata))
>>>
>>>  m1347<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+plankton,data=mydata))
>>>
>>>
>>>  m1356<-glm.convert(glm.nb(mantas~site+cosmonth+coslunar+sinlunar,data=mydata))
>>>
>>>  m1357<-glm.convert(glm.nb(mantas~site+cosmonth+coslunar+plankton,data=mydata))
>>>
>>>
>>>  m1367<-glm.convert(glm.nb(mantas~site+cosmonth+sinlunar+plankton,data=mydata))
>>>
>>>
>>>  m1456<-glm.convert(glm.nb(mantas~site+sinmonth+coslunar+sinlunar,data=mydata))
>>>
>>>  m1457<-glm.convert(glm.nb(mantas~site+sinmonth+coslunar+plankton,data=mydata))
>>>
>>>
>>>  m1467<-glm.convert(glm.nb(mantas~site+sinmonth+sinlunar+plankton,data=mydata))
>>>
>>>
>>>  m1567<-glm.convert(glm.nb(mantas~site+coslunar+sinlunar+plankton,data=mydata))
>>>
>>>  m2345
>>> <-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+coslunar,data=mydata))
>>>
>>>  m2346<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+sinlunar,data=mydata))
>>>
>>>  m2347<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+plankton,data=mydata))
>>>
>>>
>>>  m2356<-glm.convert(glm.nb(mantas~year+cosmonth+coslunar+sinlunar,data=mydata))
>>>
>>>  m2357<-glm.convert(glm.nb(mantas~year+cosmonth+coslunar+plankton,data=mydata))
>>>
>>>
>>>  m2367<-glm.convert(glm.nb(mantas~year+cosmonth+sinlunar+plankton,data=mydata))
>>>
>>>
>>>  m2456<-glm.convert(glm.nb(mantas~year+sinmonth+coslunar+sinlunar,data=mydata))
>>>
>>>  m2457<-glm.convert(glm.nb(mantas~year+sinmonth+coslunar+plankton,data=mydata))
>>>
>>>
>>>  m2467<-glm.convert(glm.nb(mantas~year+sinmonth+sinlunar+plankton,data=mydata))
>>>
>>>
>>>  m2567<-glm.convert(glm.nb(mantas~year+coslunar+sinlunar+plankton,data=mydata))
>>>
>>>  m3456<-glm.convert(glm.nb(mantas~co
>>> smonth+sinmonth+coslunar+sinlunar,data=mydata))
>>>
>>>
>>>  m3457<-glm.convert(glm.nb(mantas~cosmonth+sinmonth+coslunar+plankton,data=mydata))
>>>
>>>
>>>  m3567<-glm.convert(glm.nb(mantas~cosmonth+coslunar+sinlunar+plankton,data=mydata))
>>>
>>>
>>>  m4567<-glm.convert(glm.nb(mantas~sinmonth+coslunar+sinlunar+plankton,data=mydata))
>>>
>>> #Five terms - 21 models
>>>  m12345<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+coslunar,
>>> data=mydata))
>>>  m12346<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+sinlunar,
>>> data=mydata))
>>>  m12347<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+plankton,
>>> data=mydata))
>>>
>>>  m12356<-glm.convert(glm.nb(mantas~site*year+cosmonth+coslunar+sinlunar,
>>> data=mydata))
>>>  m12357<-glm.convert(glm.nb(mantas~site*year+cosmonth+coslunar+plankton,
>>> data=mydata))
>>>
>>>  m12367<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m12456<-glm.convert(glm.nb(mantas~site*year+sinmonth+coslunar+sinlunar,
>>> data=mydata))
>>>  m12457<-glm.convert(glm.nb(mantas~site*year+sinmonth+coslunar+plankton,
>>> data=mydata))
>>>
>>>  m12467<-glm.convert(glm.nb(mantas~site*year+sinmonth+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m12567<-glm.convert(glm.nb(mantas~site*year+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m13456<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+coslunar+sinlunar,
>>> data=mydata))
>>>  m13457<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+coslunar+plankton,
>>> data=mydata))
>>>
>>>  m13467<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m13567<-glm.convert(glm.nb(mantas~site+cosmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m14567<-glm.convert(glm.nb(mantas~site+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m23456<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+coslunar+sinlunar,
>>> data=mydata))
>>>  m23457<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+coslunar+plankton,
>>> data=mydata))
>>>
>>>  m23467<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m23567<-glm.convert(glm.nb(mantas~year+cosmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m24567<-glm.convert(glm.nb(mantas~year+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m34567<-glm.convert(glm.nb(mantas~cosmonth+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>> #Six terms - 7 models
>>>  m123456<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+coslunar+sinlunar,
>>> data=mydata))
>>>  m123457<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+coslunar+plankton,
>>> data=mydata))
>>>
>>>  m123467<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m123567<-glm.convert(glm.nb(mantas~site*year+cosmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m124567<-glm.convert(glm.nb(mantas~site*year+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m134567<-glm.convert(glm.nb(mantas~site+cosmonth+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>  m234567<-glm.convert(glm.nb(mantas~year+cosmonth+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>> #Seven terms - 1 model
>>>  m1234567<-glm.convert(glm.nb(mantas~site*year+cosmonth+sinmonth+coslunar+sinlunar+plankton,
>>> data=mydata))
>>>
>>>
>>> Tim Clark
>>> Department of Zoology
>>> University of Hawaii
>>>
>>> ______________________________________________
>>> R-help@r-project.org <http://mc/compose?to=R-help@r-project.org> mailing
>>> list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>
>

	[[alternative HTML version deleted]]

