St4096
St4096

Reputation: 87

Iterated models in R

Let's say that I wanted to create n random variables, then create n-1 models regressing each variable on a response. I could do something like this:

x1 <- rnorm(1000, 0, 1)
x2 <- rnorm(1000, 0, 1)
x3 <- rnorm(1000, 0, 1)
x4 <- rnorm(1000, 0, 1)
x5 <- rnorm(1000, 0, 1)
x6 <- rnorm(1000, 0, 1)
x7 <- rnorm(1000, 0, 1)
x8 <- rnorm(1000, 0, 1)
y <- rnorm(1000, 0, 1)

model1 <- lm(y ~ x1)
model2 <- lm(y ~ x2)
model3 <- lm(y ~ x3)
model4 <- lm(y ~ x4)
model5 <- lm(y ~ x5)
model6 <- lm(y ~ x6)
model7 <- lm(y ~ x7)
model8 <- lm(y ~ x8)

summary model1

And so forth. Obviously that's clunky, requires a lot of bookkeeping, and opens the door wide to substantial errors from typos.

I assume there's a more straightforward way to do this that doesn't have as much room for error?

Edit: Addressing the comments I apologize for the lack of clarity. The specifics and data generating process here aren't particularly important to me. I just included toy data because I noticed that people typically include toy data when they have questions, and I thought it was convention here.

What I'm really looking for is a process for attacking this sort of problem. It's commonplace in my line of work to have a large dataset and want to generate a large number of models comparing different pairs of variables. I've been using the process described above where I write out every model by hand, or do some sort of concatenation of commands in excel and then cut-paste. It just makes for really inefficient code, and I thought that there had to be a cleaner way to approach this in R, since this seems like the sort of thing people would frequently have to do.

Upvotes: 1

Views: 48

Answers (3)

JRR
JRR

Reputation: 3223

Use replicate

y <- rnorm(1000, 0, 1)

f = function(y){lm(y~rnorm(1000, 0, 1))}

models = replicate(10, f(y), simplify = FALSE)

summary(models[[1]])

Upvotes: 1

Martin Schmelzer
Martin Schmelzer

Reputation: 23879

Use replicate to draw the samples and plain apply over each sample to estimate the model:

X <- replicate(n = 99, expr = rnorm(1000))
y <- rnorm(1000)
apply(X, 2, function(z) { lm(y~z) })

Upvotes: 1

tobiasegli_te
tobiasegli_te

Reputation: 1463

You could do

result_list <- lapply(1:8,function(i){
    summary(lm(y ~ get(paste0("x",i))))
})

This will give you a list with all the results.

If you don't need the input as single variables, you could put them into a matrix:

y <- rnorm(1000)
x_mat <- sapply(1:8,rnorm, n = 1000)
apply(x_mat,2,function(x){
    summary(lm(y ~ x))
})

This will again result in a list with the results

Upvotes: 0

Related Questions