0

I have a problem with doing cross validation I did 10-fold cross validation and I got following error

Error in data[currentFold, ] : object of type 'closure' is not subsettable

Here are codes which I used:

k <- 10;
folds <- cvsegments(nrow(data2), k);
result1 <- rep (NA, nrow (data2))
insituValidation <- rep (NA, nrow (data2))

for (fold in 1 : k) {
   currentFold <- folds[[fold]]

   trainDat <- data2[-currentFold,]

   model3 = nls(Height ~ a*(Diameter)^2+b*Diameter+c, data = data2, start = list(a = 1.2, b=.9,c=.5), algorithm="port")
   #end model

   # The results of the predictions of the model
   result1 [currentFold] <- predict(model3, data[currentFold,]) 
   insituValidation <- data2[currentFold,3]
}

I used the same codes in another dataset before and It worked But now with new dataset, the mentioned problem occurs.

Please help me to find solution.

vrajs5
  • 4,066
  • 1
  • 27
  • 44
user3713988
  • 13
  • 1
  • 7
  • 2
    How did you define `data`? R can't find this object and thinks you want to subset the (base R) `data` function. – Roland Jun 06 '14 at 07:45
  • the data matrix has been run under following instruction: setwd('D:\\PhD\\Data\\Field Measurments\\Data Analysis\\') data2 = read.table('analysis2.csv',header = TRUE, sep =',') – user3713988 Jun 06 '14 at 15:05
  • In your second-to-last line, replace `data[currentFold,]` with `data2[currentFold,]`. Also, in the call to `nls(...)`, why are you using `data=data2` and not `data=trainDat`?? – jlhoward Jun 06 '14 at 15:41
  • Thank you, the problem was solved by your help:) data2 includes both train and test data. – user3713988 Jun 06 '14 at 18:35
  • User "jlhoward", you were right. I should use data=trainDat instead of data=data2 in the model. Thanks again – user3713988 Jun 08 '14 at 05:25

1 Answers1

0

An object of type closure is a function in R.

The object data you are using is function, so that you cannot subset it with [ like vector. By default, the variable data is a function. You can find its definition here.

You must simply have forgotten to initialize the variable data with a matrix.

Pop
  • 12,135
  • 5
  • 55
  • 68
  • the data matrix has been run under following instruction: setwd('D:\\PhD\\Data\\Field Measurments\\Data Analysis\\') dat2 = read.table('analysis2.csv',header = TRUE, sep =',') – user3713988 Jun 06 '14 at 14:57