I have performed the k-fold cross-validation without package based on here How to split a data set to do 10-fold cross validation using no packages
I need to select 30% of the sample from each fold in train data. Here is my function:
samples = 300
r = 0.83
library('MASS')
df = data.frame(mvrnorm(n=samples, mu=c(0, 0), Sigma=matrix(c(1, r, r, 1), nrow=2), empirical=TRUE))
w = df[sample(nrow(df)),]
w = data.frame(w)
kcv = 10
folds <- cut(seq(from = 1,to = nrow(w)),breaks=kcv,labels=FALSE)
kfolddata<-cbind(w,folds)
for(i in 1:kcv){ #i=1
testIndexes <- which(kfolddata[,ncol(kfolddata)]==i,arr.ind=TRUE)
testData <- w[testIndexes, ]
trainData <- w[-testIndexes, ]
trainIndexes <- kfolddata[-testIndexes,]
if(i==1) {
set.seed=1234
SubInd = sample(nrow(trainData) , size = round(0.3 *
(nrow(trainData))),replace=F)
} else {
SubInd = rbind(SubInd,sample(nrow(trainData) , size = round(0.3 *
nrow(trainData))),replace=F))}}
}
}
The results will only display the ID of the selected subset. How can I obtain the information (the variables) for the selected ID for SubInt?
Does using rbind
is the correct way? since I need to do another looping from SubInt.