Reputation: 445
I have an issue with a function I have that calculates exponential smoothing with a lag of one over groups on a field:
expsmo <- function(x) {res <- stats::filter(x * c(1, rep(0.1, length(x) - 1)), 0.9, method = "recursive")
c(head(x,1), res[-length(res)])}
Here is an example of what it does:
Score Exp Smooth Calculation
4 4 head(x,1)
7 4 4
3 4.3 0.1*7+0.9*4
5 4.17 0.1*3+0.9*4.3
7 4.253 0.1*5+0.9*4.17
2 4.5277 0.1*7+0.9*4.253
3 4.27493 0.1*2+0.9*4.5277
The issue is that when I run this over groups I get an issue with groups that are small. Here is a dataset with varying group sizes:
Player <- c('A','B','B','C','C','C','D','D','D','D','E','E','E','E','E','F','F','F','F','F','F','G','G','G','G','G','G','G')
Team <- c('A','B','B','C','C','C','D','D','D','D','E','E','E','E','E','F','F','F','F','F','F','G','G','G','G','G','G','G')
Score <- c(5,2,7,3,9,6,3,7,1,7,3,8,3,4,1,9,4,6,3,8,3,4,1,9,4,6,6,4)
When I run the function over the Score field grouped by Player and Team:
ave(Score, Player, Team, FUN = expsmo)
I just get errors.
Here is my desired output with the expsmo field:
Player Team Score expsmo
A A 5 5
B B 2 2
B B 7 2
C C 3 3
C C 9 3
C C 6 3.6
D D 3 3
D D 7 3
D D 1 3.4
D D 7 3.16
E E 3 3
E E 8 3
E E 3 3.5
E E 4 3.45
E E 1 3.505
F F 9 9
F F 4 9
F F 6 8.5
F F 3 8.25
F F 8 7.725
F F 3 7.7525
G G 4 4
G G 1 4
G G 9 3.7
G G 4 4.23
G G 6 4.207
G G 6 4.3863
G G 4 4.54767
I'm sure there is a way to modify the function to do this. Please help!!!
Upvotes: 1
Views: 93
Reputation: 214957
I think what is happening behind ave() is that the function checks every combination of your group variables and for your case, some combination doesn't have any data for example Player = "A", Team = "C"
thus the length of x will be zero and your code breaks at this case, since rep(0.1, -1)
is invalid, so add a length checker will solve the problem.
expsmo <- function(x) {
if(length(x) != 0){
res <- stats::filter(x * c(1, rep(0.1, (length(x) - 1))), 0.9, method = "recursive")
return(c(x[1], res[-length(res)]))
}
}
Upvotes: 1