[R] create loops in the explanatory variables using lm

Petr PIKAL petr.pikal at precheza.cz
Thu Sep 29 15:54:17 CEST 2011


Hi
David

> 
> Hey Petr
> 
> thanks for the answer
> 
> I guess I was a bit unclear about the nature of my data
> 
> the species terms are columns in the data frame, and they are coded 0/1, 

> depending whether the species is present in a plot or not. 
> 
> 
> No I want to repeatedly fit the linear model with all the species 
presence
> absence term in a hierarchical model, so that every species is in the 
> first place, after the richness term once. I hope this is possible with 
> data like this.

Yes it shall be possible, although I am not completely sure about 
statistical soundness. You also did not specify how do you want order the 
other variables. If you want permute them you will end with milions of 
models. For 10 variables it is almost 4 milions of permutations. For that 
you probably could use function permutation from gregmisc package but I 
wonder if you really have this on mind.

If you just want to "rotate" plant names you can do it like this.

DF<-data.frame(mort=rnorm(12), aa=sample(0:1, 12, replace=T), 
bb=sample(0:1, 12, replace=T), cc=sample(0:1, 12, replace=T), 
richness=runif(12), comun=runif(12))

xnam <- names(DF)

as.formula(paste("mort ~ ", paste("richness", paste(xnam[2:4], collapse= 
"+"), "comun", sep="+")))
mort ~ richness + aa + bb + cc + comun

and with this function you can rotate items in vector xnam

posun <- function(x, n) c(x[(n+1):length(x)], x[1:n])
as.formula(paste("mort ~ ", paste("richness", paste(posun(xnam[2:4],1), 
collapse= "+"), "comun", sep="+")))
mort ~ richness + bb + cc + aa + comun

But as I said I am not sure if this is sensible from statistical point of 
view. Somebody more capable than me has to resolve it. It seems to me that 
you will get the same result in each call of lm

Regards
Petr

> 
> 
> thanks a lot
> 
> david
> Am 29.09.2011 um 11:57 schrieb Petr PIKAL:
> 
> > Hi
> > 
> > Well, are those names (Acer_davidii, ...) columns in data frame? If 
not 
> > and you want them as factors of species you shall reformat your input 
so 
> > as you get data frame with columns mortality, richness, species, 
> > community. Column species shall have your plant species names in each 
> > respective row.
> > 
> > Your lm call shall be
> > 
> > lm(sqrt(mortality)~richness+species+community, data=your.data.frame)
> > 
> > see then
> > 
> > ?relevel for setting reference level of your factor.
> > 
> > Regards
> > Petr
> > 
> > 
> >> 
> >> Hi everyone
> >> 
> >> I am new to the list and read all the instructions, hope i get it 
right.
> >> 
> >> I have the following linear model:
> >> 
> >> 
> >> model_sqrt<-lm(sqrt(mortality)~richness
> >>               +Acer_davidii+Ailanthus_altissima+Alniphyllum_fortunei
> >>               +Betula_luminifera+Castanea_henryi+Castanopsis_carlesii
> >> +Castanopsis_eyrei+Castanopsis_fargesii+Castanopsis_sclerophylla
> >> +Celtis_biondii+Choerospondias_axillaris+Cinamomum_camphora
> >>               +Cyclobalanopsis_glauca+Cyclobalanopsis_myrsinifolia
> >> +Diospyros_glaucifolia
> >>               +Elaeocarpus_chinensis+Elaeocarpus_glabripetalus
> >> +Elaeocarpus_japonicus
> >> +Idesia_polycarpa+Koelreuteria_bipinnata+Liquidambar_formosana
> >> +Lithocarpus_glaber+Machilus_grijsii+Machilus_leptophylla
> >> +Machilus_thunbergii+Manglietia_yuyuanensis+Melia_azedarach
> >> +Meliosma_flexuosa
> >> +Nyssa_sinensis+Phoebe_bournei+Quercus_acutissima+Quercus_fabri
> >>               +Quercus_phillyreoides+Quercus_serrata+Rhus_chinensis
> >> +Sapindus_mukorossi
> >>               +Sapium_discolor+Sapium_sebiferum+Schima_superba
> >>          +community
> >> ,weights=wei)
> >> 
> >> 
> >> 
> >> 
> >> Where everything from Acer_davidii to Schima_superba are plant 
species. 
> >> Since the order of the plant species matters in my case, I want to 
> >> repeatedly fit the model so that every species is in the first place, 

> > that
> >> is after the richness term, once.
> >> 
> >> I am looking for a way so that I don't have to do this by hand. I 
> > already 
> >> checked combn with as.formula and paste, but I couldn't get it right.
> >> 
> >> I'd be very grateful for any help.
> >> 
> >> 
> >> Thanks
> >> 
> >> 
> >> David
> >> 
> >> 
> >>   [[alternative HTML version deleted]]
> >> 
> >> ______________________________________________
> >> R-help at 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
> >> and provide commented, minimal, self-contained, reproducible code.
> > 
>



More information about the R-help mailing list