-2

I would like to do multiple linear regression between more variables (a and b and c) for each day, but in reality I will have 5 or 6 data points for each day. Than I want to create new columns of the regression coefficients (intercept and slopes).

The data are something like this

Date        a      b        c
1.1.2004    4,8  12,9   5633,0
2.1.2004    1,8  11,5   6166,0
3.1.2004    3,9  15,2   5830,0
4.1.2004    6,8  14,3   2744,0
5.1.2004    5,9  14,2   3422,0

I tried apply

apply(data,1, function(x,y) lm(a~c, data=data))

but I am getting the same coefficients for each day and I still do not know how should I create the two columns with ceofficients.

user3624251
  • 185
  • 1
  • 8
  • http://stackoverflow.com/questions/7523427/ddply-with-lm-function – Ben Bolker May 10 '14 at 22:07
  • or use `nlme::lmList` or `lme4::lmList` – Ben Bolker May 10 '14 at 22:08
  • 1
    now that I look more carefully, this doesn't really make sense -- why are you running a linear model with a single data point? Why not just calculate the slope yourself? – Ben Bolker May 10 '14 at 22:16
  • Many different linear regression models could perfectly fit a single row of your dataset. For instance, a model with intercept of that row's `a` value and slope `0` or a model with intercept `0` and slope that row's `a/c` value. Could you describe what you're trying to accomplish? – josliber May 10 '14 at 22:44

1 Answers1

2

I guess your real data set contains more than only one data point per day, otherwise (as @BenBolker already said in comments) there is no sense to run a linear regression on it. Anyway, you could do something like that in data.table package. Try

library(data.table)
setDT(df)[, list(Interecept = lm(a ~ c)$coefficients[1],
                  coeff = lm(a ~ c)$coefficients[2]), by = Date]
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • Thank you, that is something what I needed. My data are from more years and from more weather stations. I want to do a regression between two variables per different time periods (months, or weeks). For the different days a have data from more meteo stations. – user3624251 May 11 '14 at 11:52
  • How can I extract another coeficients, for example if I ad variables to the formula lm(a~c+b+d), – user3624251 May 12 '14 at 12:17
  • 1
    just add another parameter inside the `list`, for example `, coeff2 = lm(a ~ c + b + d)$coefficients[3], coeff3 = lm(a ~ c + b + d)$coefficients[4]` and ofcorse update the models in the first two variables (`Intercept` and `coeff`) – David Arenburg May 12 '14 at 12:23
  • have you any suggestions why my coeff2 is NA? `setDT(SLSP)[,list(Interecept=lm(SL_T_mean~SL_T_max+SL_Sol_rad)$coefficients[1], coeff=lm(SL_T_mean~SL_T_max+SL_Sol_rad)$coefficients[2], coeff2=lm(SL_T_mean~SL_T_max+SL_Sol_rad)$coefficients[3]), by = Date]` – user3624251 May 12 '14 at 12:44
  • 1
    could be many reasons, see here for example http://stackoverflow.com/questions/7337761/linear-regression-na-estimate-just-for-last-coefficient – David Arenburg May 12 '14 at 12:50