Optimizing weights for average across 3 variables

230 views Asked by At

I am trying to create an ensemble of machine learning and I have code that made predictions based on occupation Ocp, Age Age, and Gender Gender.

I want to average the 3 predictions for a final prediction, but I am not sure how I should optimize the weights to minimize the RSME.

I know that Gender should dominate the data set.

Here is my attempt at the code:

temp <- NA; temp2 <- NA;temp3 <- NA
for (i in seq_len(11)) {
  for (j in seq_len(11)){
    temp2 = ((i-1)/10)*(((j-1)/10)*movie_pred2[,1]+((11-j)/10)*movie_pred2[,2]) +
      ((11-i)/10)*movie_pred[,3]
    temp2[temp2 > 5] = 5
    temp2[temp2 < 1] = 1
    temp[j] <- (sum((temp2 - tsind2[,2])^2)/length(tsind2[,2]))^.5
  }
  temp3[i,j] = temp[j]
}

I now get the warning:

Error in temp3[i, j] = temp[j] : incorrect number of subscripts on matrix

In ((i - 1)/10) * (((j - 1)/10) * movie_pred2[, 1] + ((11 -  ... :
longer object length is not a multiple of shorter object length
1

There are 1 answers

0
Stephen Henderson On

Your code begins:

> temp3<- NA

.. then some other stuff and ends

> temp3[i,j] = temp[j]

but it doesn't matter what dimensions or size your result temp is you can't push dimensioned data into a null dimension object.

>dim(temp3)
NULL

You probably want something like:

>temp3=matrix(NA, i,j)
>temp3[,j] <- something

Now ..firstly I'm sorry I can't be any more helpful/specific but it's near impossible to interpret the rest of your code without an example of the input data. Secondly unless this is a homework assignment or self-learning I recommend you investigate the many R packages that will calculate the RMSE and/or do ensemble learning for you e.g. the train function of caret