I would like to apply an optimization by group on my own function:
Here a reproducable data set:
data <- data.frame(ID=c(1,1,1,2,2,3,3),C=c(1,1,1,2,2,3,4),
Lambda=c(0.5),s=c(1:7),
sigma_S=c(0.5,0.4,0.3,0.7,0.4,0.5,0.8),
d=c(20,30,40,50,60,70,80),
sigma_B=0.3,t=5,Rec=0.5,r=0.05)
My function is defined as follows (the function is trivial, i just want to understand the method):
TestMSE <- function(LR)
{
d <- data
D <- LR + d$s
mse(d$C, D) # mse is from the Metrics Package
}
optimize(TestMSE,lower = 0.1, upper =1.5)
I tried using the ddply function:
test <- ddply(data,"ID",summarise, optimize(TestMSE,lower = 0.1, upper =1.5))
But applying the ddply function I receive the same solution for all of my groups, although there is a difference by subgroups.
Thanks.