I have actuals and four different models with their prediction & fitted values. With these fitted values, I want to find optimal weights so that (summation(wifi)-actuals)^2 is minimized. Here wi's are the weights I want to find optimally & fi's are the fitted values for each models.
The restrictions I am having for wi's are;
- Weights have to be greater than 0,
- Weights have to be smaller than 1,
- Weights must have sum of 1
I have seen a similar example here [https://stats.stackexchange.com/questions/385372/weight-optimization-in-order-to-maximize-correlation-r] but I could not replicate it for my particular problem.
Let's generate sample data to understand the problem better
actuals <- floor(runif(10, 500,1700))
model1_fitted <- floor(runif(10, 600,1800))
model2_fitted <- floor(runif(10, 400,1600))
model3_fitted <- floor(runif(10, 300,1500))
model4_fitted <- floor(runif(10, 300,1200))
sample_model <- data.frame(actuals, model1_fitted, model2_fitted,model3_fitted,model4_fitted)
Now, I need to optimally find (w1,w2,w3,w4) so that (summation(wifi)-actuals)^2 is minimized. I want to save the weights, as I mentioned I also have predictions from these four models. If I get the optimal weights, my predicted values for the ensemble model will be a linear function of these weights & predicted values. First predicted value of ensemble will be like below,
ensemble_pred_1 = w1*model1_pred1+w2*model2_pred1+w3*model3_pred1+w4*model4_pred1
Please help me to find optimal wi's so that I can generate the ensemble model as desired.