Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

r - Loop linear regression and saving coefficients

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...