2

I have seven variables and I want to create many new variables, each is an interaction term of the seven variables. There will be 2-way up to 5-way interactions. I plan to do it in two steps.

First, create all m-way combination of names of the variables. Second, convert the names into real variables. I've done the first step, but not sure how to do the second step.

My first step is:

xvec = c("white", "married", "inftype", "usecondom", "age", "edu", "part")

temp = t(combn(xvec, 2))
temp = paste(temp[,1], "*", temp[,2], sep="")

which gives me all two-way combinations/interactions of the names. However, how can I convert the names into real variables? I used to do something similar using get() or eval(parse()). But none of them works now.

Thanks in advance!

wen
  • 1,875
  • 4
  • 26
  • 43
  • 2
    `sum(choose(7,2:5))`=112. If you're fitting this many models I hope you have a lot of data and a good way to avoid overfitting ... – Ben Bolker May 24 '14 at 02:33

1 Answers1

1

About the first step (not that something is wrong with what you are doing), you can create the names like this:

temp <- combn(xvec, 2, FUN=paste, collapse=".")

This makes all combinations and then using paste it collapses the combinations together. I use ., because * is not very nice in a variable name. You can also check ?make.names, a function that makes strings suitable for use as names.

Second step You can use assign to create variables from a string stored in variable. (get is when you have the name of an existing variable as a string and want to access it)

Try something like:

for(nm in make.names(temp)) {
  assign(nm, "Put something more interesting here")
}

You can see all objects in your environment using ls()

ls()
## [1] "age.edu"           "age.part"          "edu.part"         
## [4] "inftype.age"       "inftype.edu"       "inftype.part"     
## [7] "inftype.usecondom" "married.age"       "married.edu"      
## [10] "married.inftype"   "married.part"      "married.usecondom"
## [13] "nm"                "temp"              "usecondom.age"    
## [16] "usecondom.edu"     "usecondom.part"    "white.age"        
## [19] "white.edu"         "white.inftype"     "white.married"    
## [22] "white.part"        "white.usecondom"   "xvec" 

Now you have created a lot of variables.


Just as a comment, I wanted to add how I would probably do it.

Instead of filling your environment with a lot of objects you can use a list (myCombs) to save all of them.

myCombs <- combn(xvec, 2,simplify=FALSE, FUN = function(cmb) {
  res <- paste("This is the combination of", cmb[1], "and", cmb[2])
  res
})
##Add the names to the list items.
names(myCombs) <- combn(xvec, 2, FUN=paste, collapse=".")

I used the terms to construct a string. You probably want to do something more sophisticated. If you have the items of xvec as variables in your environment you can access them here using get(cmb[1]) and get(cmb[1]).

Now you can access each variable using myCombs$NAME, or myComb[[NAME]], or you can even attach(myComb) the whole list to your environment.

 myCombs$edu.part
 ## [1] "This is the combination of edu and part"

I started to write a small answer, but got carried away. Hope this helps you,

alex

alko989
  • 7,688
  • 5
  • 39
  • 62