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
411 views
in Technique[技术] by (71.8m points)

r - Using data.table to create a column of regression coefficients

I'm struggling with what seems like it should be a simple extension of a previous question I'd asked here.

I'm trying to aggregate over (a) a range of dates and (b) a factor variable. Sample data might be:

Brand    Day     Rev     RVP              
  A      1        2535.00  195.00 
  B      1        1785.45  43.55 
  C      1        1730.87  32.66 
  A      2        920.00   230.00
  B      2        248.22   48.99 
  C      3        16466.00 189.00      
  A      1        2535.00  195.00 
  B      3        1785.45  43.55 
  C      3        1730.87  32.66 
  A      4        920.00   230.00
  B      5        248.22   48.99 
  C      4        16466.00 189.00

Thanks to helpful advice, I've figured out how to find the mean revenue for brands over a period of days using data.table:

new_df<-df[,(mean(Rev)), by=list(Brand,Day)]

Now, I'd like to create a new table where there is a column listing the coefficient estimate from an OLS regression of Rev by Day for each brand. I tried to do this as follows:

new_df2<-df[,(lm(Rev~Day)), by=list(Brand)]

That doesn't seem quite right. Thoughts? I'm sure it's something obvious I've missed.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You have a couple of options.

You could save the whole model object as a list within the data.table

models <- df[,   list(model  = list(lm(Rev ~ Day))),by = Brand]

models
   Brand model
1:     A  <lm>
2:     B  <lm>
3:     C  <lm>


# look at the models
models[,print(model[[1]]),by= Brand]


Call:
lm(formula = Rev ~ Day)

Coefficients:
(Intercept)          Day  
     2804.2       -538.3  



Call:
lm(formula = Rev ~ Day)

Coefficients:
(Intercept)          Day  
     1741.5       -263.5  



Call:
lm(formula = Rev ~ Day)

Coefficients:
(Intercept)          Day  
      -3698         4653  

You can save the coefficients

models[, {coefs <- coef(model[[1]])
      list(coefs = coefs, name = names(coefs))}, by = Brand]
##     Brand      coefs        name
## 1:     A  2804.1667 (Intercept)
## 2:     A  -538.3333         Day
## 3:     B  1741.5291 (Intercept)
## 4:     B  -263.5251         Day
## 5:     C -3697.8621 (Intercept)
## 6:     C  4653.1989         Day

Or you can just extract the list of models

  models[,model]

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

...