1

For Example

FP <- data.frame(A = 1:9, B = 11:19, C = 21:29, D = 31:39 ..... N = 145:153, Date: Jan 1 to Jan 9)

(I know the syntax above is wrong. Just for your understanding)

There are like n number of columns say 14 and an additional date column

I need to perform simple linear regression of A (independent Variable) on B,C,D,E...N (dependent Variables)SEPARATELY grouped by the date column, How to make aggregate function work? Or is there any other function which will be come in handy ?

Balamurali N.R
  • 333
  • 2
  • 7

1 Answers1

1

When working / saving models you might want to work with lists:

FP <- data.frame(A = 1:9, B = 11:19, C = 21:29, D = rep(1:3,3))

lapply(split(FP, FP$D), function(x) lm(B + C ~ A, data = x))

#$`1`
# 
#Call:
#lm(formula = B + C ~ A, data = x)

#Coefficients:
#(Intercept)            A  
#         30            2  
# 

#$`2`

#Call:
#lm(formula = B + C ~ A, data = x)

#Coefficients:
#(Intercept)            A  
#         30            2  


#$`3`

#Call:
#lm(formula = B + C ~ A, data = x)

#Coefficients:
#(Intercept)            A  
#         30            2  

First you split your data.frame by D and then run your regressions on those splits.

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • Thank you for your answer. I have slightly modified my question now. Even in the original case (before editing), Isnt odd to have same coefficients and intercept for all three cases ? – Balamurali N.R Oct 25 '17 at 00:25