I'm looking to use the ave
function to apply the arima()
function over a particular group of my dataframe. The arima
function, however, requires me to list some variables under the xreg
argument, but I don't know how to specify the xreg
variables in such a way that they are the same length as each of the groups separated out by the ave
function.
Currently:
DKmerg
Player CPM Play.at Team
1 Dave Whitting 0.01111111 away 33
2 Dave Whitting 0.00000000 away 46
3 Dave Whitting 0.02222222 home 60
4 Dave Whitting 0.03333333 away 42
5 Dave Whitting 0.04444444 home 45
6 Aaron Bryann 0.00000000 away 49
7 Aaron Bryann 0.08064516 home 49
8 Aaron Bryann 0.00000000 away 68
9 Aaron Bryann 0.05555556 home 57
10 Aaron Bryann 0.01470588 away 68
11 Aaron Bryann 0.03333333 home 68
12 Aaron Bryann 0.03333333 home 68
13 Aaron Bryann 0.05555556 away 43
14 Aaron Bryann 0.03333333 home 75
15 Aaron Bryann 0.00000000 home 64
16 Mark Johnson 0.11111111 home 62
17 Mark Johnson 0.00000000 away 27
18 Mark Johnson 0.00000000 home 54
19 Mark Johnson 0.03333333 home 51
20 Mark Johnson 0.01333333 away 25
ave(DKmerg$CPM, DKmerg$Player, FUN = function(x) arima(x, order = c(1,0,1), xreg = cbind(DKmerg$Play.at, DKmerg$Team)))
leads to an error because the lengths of x
and xreg
do not match.
I assume this is because the xreg
variables are the length of the entire data frame and not the length of the groups broken down by the ave
function.
How can I change my code to get these object lengths to match?