Skip to content
Prev 166582 / 398502 Next

Applying 'lm' in each group

Bhargab Chattopadhyay <bhargab_1 <at> yahoo.com> writes:
Now I want to run regression
The one-liner is


mapply(function(x,y) lm(y~x,data=data.frame(x,y)),xg,yg,
       SIMPLIFY=FALSE)

Perhaps easier to understand:

results <- list()
for (i in seq(along=xg)) {
  results[[i]] <- lm(yg[[i]]~xg[[i]])
}

  Ben Bolker