I have an exponential smoothing function that is used for predictive analysis. I need it to be grouped over other variables and to have a lag of one row.
Here is the function:
exp1 = function(x) {if (NROW(x) < 4) {res <- x} else {res <- fitted(ses(x, alpha = 0.1))}
retu(res)}
I run it across the below dataset:
school <- c('I','B','B','B','B','I','I','I','I','B','I','I','B','I','B','I','B','B','I','I','B','I','B','I','B','B','I')
Student <- c('S','S','S','O','O','O','O','S','O','S','S','O','S','O','O','S','S','O','S','O','S','O','O','S','S','O','J')
area <- c('A','A','A','A','B','A','B','A','A','A','A','A','A','A','A','A','A','A','A','B','A','A','A','A','A','A','A')
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)
match1 <- data.frame(school, Student, area, Score)
expsmooth1 = ave(match1$Score, match1$area, match1$Student, FUN = exp1)
However when I append new data onto the bottom of the original dataset and run the function again, some prior values change and some remain the same. I need the function to retain the same values for prior and not change them!
Here is the same dataset but with four new readings appended to the bottom:
school <- c('I','B','B','B','B','I','I','I','I','B','I','I','B','I','B','I','B','B','I','I','B','I','B','I','B','B','I','I','I','I','I')
Student <- c('S','S','S','O','O','O','O','S','O','S','S','O','S','O','O','S','S','O','S','O','S','O','O','S','S','O','J','J','J','J','S')
area <- c('A','A','A','A','B','A','B','A','A','A','A','A','A','A','A','A','A','A','A','B','A','A','A','A','A','A','A','A','A','B','A')
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,0,0,0,0)
match2 <- data.frame(school, Student, area, Score)
expsmooth2 = ave(match2$Score, match2$area, match2$Student, FUN = exp1)
Here are the results of the function when ran across both datasets. You can see some values remain the same (like I want) and some change.
School Student Area Score expsmooth1 expsmooth2 Comparison
I S A 5 5.062012 4.803711 different
B S A 2 5.055811 4.823340 different
B S A 7 4.750229 4.541006 different
B O A 3 4.005469 4.005469 same
B O B 9 9.000000 9.000000 same
I O A 6 3.904922 3.904922 same
I O B 3 3.000000 3.000000 same
I S A 7 4.975207 4.786905 different
I O A 1 4.114430 4.114430 same
B S A 7 5.177686 5.008215 different
I S A 3 5.359917 5.207393 different
I O A 8 3.802987 3.802987 same
B S A 3 5.123926 4.986654 different
I O A 4 4.222688 4.222688 same
B O A 1 4.200419 4.200419 same
I S A 9 4.911533 4.787989 different
B S A 4 5.320380 5.209190 different
B O A 6 3.880377 3.880377 same
I S A 3 5.188342 5.088271 different
I O B 8 8.000000 8.000000 same
B S A 3 4.969508 4.879444 different
I O A 4 4.092340 4.092340 same
B O A 1 4.083106 4.083106 same
I S A 9 4.772557 4.691499 different
B S A 4 5.195301 5.122349 different
B O A 6 3.774795 3.774795 same
I J A 6 6.000000 6.000000 same
I J A 0 0.000000
I J A 0 0.000000
I J B 0 0.000000
I S A 0 5.010114
Any idea how to stop this from happening? I need all my prior values to remain the same rather than changing when new values are appended.
Any help is appreciated.
