[R] quick way to construct formula

Eric Lecoutre lecoutre at stat.ucl.ac.be
Wed Jun 29 08:44:20 CEST 2005



Here is a way: it uses 'paste' but I dont think it is a problem anyway
to use it.
Nevertheless, it is surely a bad idea to fit any model with more than
250000 terms...


>   main_effects = paste(nam,collapse="+")
>   inter <- outer(nam,nam,paste,sep=":")
>   inter <- paste(inter[upper.tri(inter)],collapse="+")
>   log_effects <- paste("log(",nam,")",sep="",collapse="+")
>
as.formula(paste("~",main_effects,"+",inter,"+",log_effects,sep=""))
~x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x1:x2 + x1:x3 + 
    x2:x3 + x1:x4 + x2:x4 + x3:x4 + x1:x5 + x2:x5 + x3:x5 + x4:x5 + 
    x1:x6 + x2:x6 + x3:x6 + x4:x6 + x5:x6 + x1:x7 + x2:x7 + x3:x7 + 
    x4:x7 + x5:x7 + x6:x7 + x1:x8 + x2:x8 + x3:x8 + x4:x8 + x5:x8 + 
    x6:x8 + x7:x8 + x1:x9 + x2:x9 + x3:x9 + x4:x9 + x5:x9 + x6:x9 + 
    x7:x9 + x8:x9 + x1:x10 + x2:x10 + x3:x10 + x4:x10 + x5:x10 + 
    x6:x10 + x7:x10 + x8:x10 + x9:x10 + log(x1) + log(x2) + log(x3) + 
    log(x4) + log(x5) + log(x6) + log(x7) + log(x8) + log(x9) + 
    log(x10)




HTH,

Eric



Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward
Tufte   


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Luke
> Sent: mercredi 29 juin 2005 4:49
> To: R-help at stat.math.ethz.ch
> Subject: [R] quick way to construct formula
> 
> 
> Dear R users,
> 
> I have a data with 1000 variables named "x1", "x2", ..., 
> "x1000", and I want to construct a formula like this format:
> 
> ~x1+x2+...+x1000+x1:x2+x1:x3+x999:x1000+log(x1)+...+log(x1000)
> 
> That is: the base variables followed by all interaction terms 
> and all base feature log-transformations. I know I can use 
> several paste functions to construct it. But is there any 
> other handy way to do it?
> 
> -Luke
> 
> ______________________________________________
> 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
>




More information about the R-help mailing list