Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:02:05+00:00 2026-06-03T08:02:05+00:00

I have made a start to create some training and test sets using 10

  • 0

I have made a start to create some training and test sets using 10 fold crossvalidation for an artificial dataset:

rows <- 1000

X1<- sort(runif(n = rows, min = -1, max =1))
occ.prob <- 1/(1+exp(-(0.0 + 3.0*X1)))
true.presence <- rbinom(n = rows, size = 1, prob = occ.prob)

# combine data as data frame and save
data <- data.frame(X1, true.presence)

id <- sample(1:10,nrow(data),replace=TRUE)
ListX <- split(data,id) 
fold1 <- data[id==1,] 
fold2 <- data[id==2,] 
fold3 <- data[id==3,] 
fold4 <- data[id==4,] 
fold5 <- data[id==5,] 
fold6 <- data[id==6,] 
fold7 <- data[id==7,] 
fold8 <- data[id==8,] 
fold9 <- data[id==9,] 
fold10 <- data[id==10,] 

trainingset <- subset(data, id %in% c(2,3,4,5,6,7,8,9,10))
testset <- subset(data, id %in% c(1))

I am just wondering whether there are easier ways to achieve this and how I could perform stratified crossvalidation which ensures that the class priors (true.presence) are roughly the same in all folds?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:02:06+00:00Added an answer on June 3, 2026 at 8:02 am

    I’m sure that (a) there’s a more efficient way to code this, and (b) there’s almost certainly a function somewhere in a package that will just return the folds, but here’s some simple code that gives you an idea of how one might do this:

    rows <- 1000
    
    X1<- sort(runif(n = rows, min = -1, max =1))
    occ.prob <- 1/(1+exp(-(0.0 + 3.0*X1)))
    true.presence <- rbinom(n = rows, size = 1, prob = occ.prob)
    
    # combine data as data frame and save
    dat <- data.frame(X1, true.presence)
    
    require(plyr)
    createFolds <- function(x,k){
        n <- nrow(x)
        x$folds <- rep(1:k,length.out = n)[sample(n,n)]
        x
    }
    
    folds <- ddply(dat,.(true.presence),createFolds,k = 10)
    
    #Proportion of true.presence in each fold:
    ddply(folds,.(folds),summarise,prop = sum(true.presence)/length(true.presence))
    
       folds      prop
    1      1 0.5049505
    2      2 0.5049505
    3      3 0.5100000
    4      4 0.5100000
    5      5 0.5100000
    6      6 0.5100000
    7      7 0.5100000
    8      8 0.5100000
    9      9 0.5050505
    10    10 0.5050505
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made some custom attached properties that enable me to create a pop
I have a pointer of a structure type that I made. On program start
I have made an application (for my self) for feeds reading, using SyndicationFeed ,
I have made a multiplayer game using the GameKit Framework where 2 iPhones/iPods can
I have made an application using Java/Hibernate/MySQL, but whenever I try running it, it
Using information from some of the questions here on generic views, I have created
I have made a kind of Video Slider, where thumbnail of videos are displaying
I have made a game which is nearly identical to Popcap's Atomica. ( http://www.popcap.com/gamepopup.php?theGame=atomica
I have made a generic Observer interface and an Observable class, but can't compile
I have made a basic ascx control which is just a panel with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.