0

I am trying to use a for-loop to determine the optimal polynomial degrees to use for each variable in my regression, and will then use k-fold cross-validation. I am getting an error "Error in xj[i] : only 0's may be mixed with negative subscripts". I know this code may not be very "r-ish" as im new to the language so any other tips would be helpful as well.

b1 = rep(0,27) 
b2 = rep(0,27) 
b3 = rep(0,27)
cv.error = rep(0,27)
index = 1
for (i in c(2,3,4)) {
  for (j in c(2,3,4)) {
    for (k in c(2,3,4)) {
       fit = lm(user_Score ~
               poly(user_count, i),
               poly(year_of_Release, j),
               poly(global_Sales, k), data = video_games)
        b1[index] = i
        b2[index] = j
        b3[index] = k
        cv.error[index] = cv.glm(video_games, fit, K=10)$delta[1]
        index = index + 1
    }
  }
}

I'm expecting to end up with vectors where I store each combination as well as the MSE so that I can then see which combination was optimal.

bgaerber
  • 76
  • 7

0 Answers0