2

This is part of the dataset (named "ME1") I'm using (all variables are numeric):

   Year  AgeR   rateM
1  1751 -1.0 0.241104596
2  1751 -0.9 0.036093609
3  1751 -0.8 0.011623734
4  1751 -0.7 0.006670552
5  1751 -0.6 0.006610552
6  1751 -0.5 0.008510828
7  1751 -0.4 0.009344041
8  1751 -0.3 0.011729740
9  1751 -0.2 0.010988005
10 1751 -0.1 0.015896107
11 1751  0.0 0.018190140
12 1751  0.1 0.024588340
13 1751  0.2 0.029801362
14 1751  0.3 0.044515912
15 1751  0.4 0.055240354
16 1751  0.5 0.088476758
17 1751  0.6 0.119045309
18 1751  0.7 0.167866571
19 1751  0.8 0.239244825
20 1751  0.9 0.329683010
21 1751  1.0 0.472448318

I want to use a linear model and save coefficients as follow:

male<-lm(ME1$rateM~exp(AgeR))
summary(male)
coeff <- summary(male)$coefficients[2]

The problem is that I need to repeat this procedure for every year (from 1751 to 2014) and I want to save all coefficients into one dataset like this:

Year coeff
1751 0.1556977
1752 0.0966664
...
2014 0.0420914

I don't know if I have to use a for-loop, lapply or something else. Can someone help me?

DanielUp
  • 23
  • 1
  • 8

3 Answers3

6

There are several ways to do this. First, we create some generated data for illustration purposes:

set.seed(123)
dat <- expand.grid(year=2000:2010, AgeR=seq(-1,1,0.1))
dat$value <- rnorm(nrow(dat))

We can start with base-R. We split our data by year, fit the model and extract our coefficient. Then we bind everything together.

res <- do.call(rbind,lapply(split(dat, dat$year),function(x){
  fit <- lm(value~exp(AgeR), data=x)
  res <- data.frame(year=unique(x$year),coeff=coef(fit)[2])
  res
}))

We can do the same using data.table:

library(data.table)


res2 <- setDT(dat)[,.(coeff=coef(lm(value~exp(AgeR)))[2]),year]
res2
Heroka
  • 12,889
  • 1
  • 28
  • 38
  • You're welcome. This is easily expanded by a custom function to extract more or different data from each model. – Heroka Jan 11 '16 at 14:16
3

The broom package can be pretty useful here too.

library(dplyr)
library(broom)

mtcars %>%
  group_by(gear) %>%
  do(tidy(lm(mpg ~ am + cyl, data = .)))
Benjamin
  • 16,897
  • 6
  • 45
  • 65
1

Package nlme offers a handy function for this:

library(nlme)
coef(lmList(value ~ exp(AgeR)| year, data=dat))[,2, drop = FALSE]
Roland
  • 127,288
  • 10
  • 191
  • 288