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?