[R] adding variable

kjetil@entelnet.bo kjetil at entelnet.bo
Tue Nov 18 22:38:16 CET 2003


On 18 Nov 2003 at 21:53, Martin Wegmann wrote:

You can go by text representation in a loop, using some of the 
following stuff:

> form <- "y ~ x   "
> as.formula(form)
y ~ x
> nchar(form)
[1] 8
> substr(form, 7,8)
[1] "  "
> substr(form, 7,8) <- "+z"
> form
[1] "y ~ x +z"
> as.formula(form)
y ~ x + z
> 

Then you can loop over (part of) names(your.df)

Kjetil Halvorsen

> On Tuesday 18 November 2003 19:32, Prof Brian Ripley wrote:
> > On Tue, 18 Nov 2003, Martin Wegmann wrote:
> > > I have count data of animals (here y, y1, y2...) and env.
> > > variables (x, x1, x2 ,....).
> > >
> > > I used a glm
> > >
> > > glm(y~x1+x2+x3....)
> > >
> > > glm(y1~x1+x2+x3....)
> > >
> > > and now I would like to add the count data of other species to
> > > investigate if they might have a bigger impact than the env.
> > > variables:
> > >
> > > #x? are the selected var from the first glm run
> > >
> > > glm(y~x?+x?+y1)
> > >
> > > glm(y~x?+x?+x?+y2)
> > >
> > > ....
> > >
> > > I wonder if there is a more elegant method to do this than adding
> > > (and removing) each y by hand.
> >
> > Do you mean each x?  In either case, see ?update.
> 
> update looks good but with update and with adding the y I have to do
> it manually. 
> 
> I thought something like doing 
> 
> glm(y~x+x1+x2+....+y§) 
> 
> where y§ is: grep y1 out of df.y run glm and name it
> grep y2 out of df.y run glm .....
> 
> until all y's of df.y has been onced included in the model.
> every time only one y§ has to be included
> 
> the included x's have to be kept. I only want to look if one species
> variables has more explanation power than the env. variables.
> 
> perhaps this helps to understand what I am looking for:
> I think bash scripts are not possible in R but it would look like such
> a bash script for GRASS:
> 
> for variable in y1 y2 y3  ....; do
> 
> glm(y~x+x1+x2....+$variable)->glm.$variable
> ; done
> 
> #where $variable refers to the name of read in y's.
> 
> 
> Martin
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list