0

I've managed to make different combinations of 2 from a set of data so that I get the pairs of names within the data set. I would like to have a function when I use mapply so that I can use each name in each pair to reference their corresponding datasets. Right now I have:

myPairs <- combn(names(iris[1:4]), 2)

f <- function(x,y)
{
#Want to make a lm(x ~ y) and other potential calculations 
}

a <- myPairs[1,]
b <- myPairs[2,]

mapply(f, a, b)

In other words, I want to calculate it like so:

data <- iris
attach(data)
lm(Sepal.Length ~ Sepal.Width)
lm(Sepal.Length ~ Petal.Length)
lm(Sepal.Length ~ Petal.Width)
lm(Sepal.Width ~ Petal.Length)
lm(Petal.Length ~ Petal.Width)

but using the names from the combn as reference to the dataset

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Luke Zhang
  • 343
  • 1
  • 4
  • 14

1 Answers1

2

First off, don't attach your data. Use the data argument for lm instead.

Let's build the formulas:

myFormula = apply(myPairs, MARGIN = 2, FUN = paste, collapse = " ~ ")

Then make the models

myModels = lapply(myFormula, function(x) lm(formula = x, data = iris))

We can then inspect some results:

myModels[[1]]
# Call:
# lm(formula = x, data = iris)
#
# Coefficients:
# (Intercept)  Sepal.Width  
#      6.5262      -0.2234  
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 2
    `combn` has a `FUN=` argument too - `combn(names(iris[1:4]), 2, FUN=function(x) lm(paste(x,collapse=" ~ "), data=iris), simplify=FALSE)` so you can avoid needing to `lapply` later. – thelatemail Jun 15 '16 at 00:53
  • Thanks guys. The lm() function was just a start. Eventually, I would like to do more complicated calculations and use the coefficient from lm() to use for some other calculations. – Luke Zhang Jun 15 '16 at 16:35