1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Cross Validation

Discussion in 'Education' started by mattyice33x, Oct 8, 2018.

  1. mattyice33x

    mattyice33x Guest

    I am working on a 10-fold cross validation problem, and am having an issue with part of my code. Specifically, I'm having a problem with my "for (1 in nfold)" argument, and with the variable length of x. Is there a better way to set up cross-validation than this? Also, I'm not familiar with the last error message regarding the variable length of x--what exactly does this mean, and how might I correct it?

    I've attached my code (the data set is included in the ElemStatLearn package).

    Any help is appreciated, Thanks!


    library(ElemStatLearn)

    library(kknn)

    library(class)

    data(zip.train)

    train=zip.train[which(zip.train[,1] %in% c(2,3)),]

    test=zip.test[which(zip.test[,1] %in% c(2,3)),]

    nfold = 10

    infold = sample(rep(1:10, length.out = (x)))

    Warning message: In rep(1:10, length.out = (x)) : first element used of 'length.out' argument


    mydata = data.frame(x = train[,c(-1,-4)] , y = train[,1])

    K = 20

    errorMatrix = matrix(NA, K, 10) for (1 in nfold)

    Error: unexpected numeric constant in "for (1"


    { + for (k in 1:20) + { + knn.fit = kknn(y ~ x, train = mydata[infold != l, ], test = mydata[infold == l, ], k = k) + errorMatrix[k, l] = mean((knn.fit$fitted.values - mydata$y[infold == l])^2) + } + }

    Error in model.frame.default(formula, data = train) : variable lengths differ (found for 'x')

    Login To add answer/comment
     

Share This Page