0

I have a custom function which runs a regression and returns the result in a desired way. the function is named "reg.fun". For example, I can run such a code:

results <- dt[ ,reg.fun(.SD, depvar="Y", indepvars=c("X1", "X2")),
              .SDcols=c("Y", "X1", "X2"), by = id]

The code above produces an output like this: It is just reporting the regression results in my desired manner.) I am using it in data table to use the "by" group feature.

id  X1coef   X2coef  X1tstat  X2tstat ...other results...
A   0.0418   0.1194   2.65     3.56    ....
B   0.7903   -0.554   2.75     4.59    .....

This works perfectly and gives me the results in the desired table. Now, I want to use the rollapply function to do the same excersice in a rolling window manner. What I write is this:

regress <- dt[ ,rollapply(.SD, width = 3,
               reg.fun(.SD, depvar="Y" indepvars=c("X1","X2")) ),
              .SDcols = c("Y", "X1", "X2"), by = id]

here is the error that I get now:

Error in match.fun(FUN) : 'reg.fun(.SD, depvar = "Y", indepvars = c("X1", "X2"))' is not a function, character or symbol

Ideally, I want to produce the same above table, but now with multiple rows for id A and id B, since I want to run regression on rolling windows. somthing like this:

window    id  X1coef   X2coef  X1tstat  X2tstat ...other results...
  1-3      A   xxx      xxx       xxx     xxx    ....
  2-5      A   xxx      xxx       xxx     xxx
  3-6      A   xxx      xxx       xxx     xxx
   ...
  1-3      B    xxx      xxx       xxx     xxx    .....
  2-5      B    xxx      xxx       xxx     xxx
  3-6      B    xxx      xxx       xxx     xxx 

I cannot find out why in this context reg.fun is not considered as a function for the rollapply.

I know that I might be wrong on so many levels since this is my first project in R. Can some one please let me know what is the problem, or if this is not the way, what is the correct direction to go?

  • 1
    please show a small reproducible example and expected output. – akrun Feb 27 '17 at 02:16
  • Could you post part of the output of results an the expected output of regress? – llrs Feb 27 '17 at 10:31
  • To restate the error message (which seemed fairly clear): The `match.fun`-function was not given a function or a the name of a function but rather was given an expression or call. I also suspect that you would not be able to leave the "interior"-`.SD` in there either. The FUN argument of rollapply would be getting parcels of data extracted from the result of `.SD`. Doing many "regressions" with 3 rows of data each seems rather silly in my option. – IRTFM Feb 27 '17 at 16:31
  • Thanks @akrun. I added an example of my desired output and what I have now. – MarcusAerlius Feb 27 '17 at 21:18
  • @42- Thanks. What I can not understand is that why it does not recognize my function as a "function" in this setting. I thought about the .SD part as well. So I tried to replace interior .SD with name of the input (dt) as well. but it does not seem to work. by the way, the width 3 is just for example since I am trying to find out my code. I am not aiming to run regressions with 3 points! – MarcusAerlius Feb 27 '17 at 21:22
  • @Llopis Thanks. I have added an example of desired output. – MarcusAerlius Feb 27 '17 at 21:23

2 Answers2

0

As I remember you should input arguments of your custom function inside rollapply after comma, not in brackets. You should print something like this rollapply(data, window, function, arg1, arg2). In your case it should look like:

rollapply(.SD, 3, reg.fun, depvar="Y", indepvars = c("X1", "X2")), .SDcols = c("Y", "X1", "X2"), by = id)

Also rollapply by argument most likely should be numeric!

And it seems that your custom function reg.fun has by argument too, which is ambiguous and also may cause errors.

Bobby Digital
  • 79
  • 1
  • 1
  • 6
0
regress <- dt[ ,rollapply(.SD, width = 3, reg.fun, depvar="Y",
                indepvars=c("X1","X2")), .SDcols = c("Y", "X1", "X2"), by = id]
Jason
  • 21
  • 3
  • Rather than just simply supply a code only answer, it would be better to include a description of what the code does and why it solves the problem. Thanks – David Wilson Jun 08 '17 at 07:48