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

  • SEARCH
  • Home
  • 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 8775527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:49:18+00:00 2026-06-13T18:49:18+00:00

I am attempting to do bootstrap resampling on a multilevel/hierarchical dataset. The observations are

  • 0

I am attempting to do bootstrap resampling on a multilevel/hierarchical dataset.
The observations are (unique) patients clustered within hospitals.

My strategy is to sample with replacement from the patients within each hospital in turn, which will ensure that all hospitals are represented in the sample and that when repeated all the samples sizes will be the same. This is method 2 here.

My code is like this:

hv <- na.omit(unique(dt$hospital))

samp.out <- NULL

for (hosp in hv ) {
    ss1 <- dt[dt$hospital==hosp & !is.na(dt$hospital),]
    ss2 <- ss1[sample(1:nrow(ss1),nrow(ss1), replace=T),]
    samp.out <- rbind(samp.out,ss2)
}

This seems to work (though if anyone can see any problem I would be grateful).

The issue is that it is slow, so I would like to know if there are ways to speed this up.

Update:

I have tried to implement Ari B. Friedman’s answer but without success – so I have modified it slightly, with the aim of constructing a vector which then indexes the original dataframe. Here is my new code:

# this is a vector that will hold unique IDs
v.samp <- rep(NA, nrow(dt))

#entry to fill next
i <- 1

for (hosp in hv ) {
    ss1 <- dt[dt$hospital==hosp & !is.na(dt$hospital),]

    # column 1 contains a unique ID
    ss2 <- ss1[sample(1:nrow(ss1),nrow(ss1), replace=T),1]
    N.fill <- length(ss2)
    v.samp[ seq(i,i+N.fill-1) ] <- ss2

    # update entry to fill next
    i <- i + N.fill
}

samp.out <- dt[dt$unid %in% v.samp,]

This is fast ! BUT, it fails to work properly because it only selects the unique IDs of v.samp in the final line, but the sampling is with replacement so there are repeated IDs in v.samp. Any further help will be much appreciated

  • 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-13T18:49:19+00:00Added an answer on June 13, 2026 at 6:49 pm

    The usual trick to speeding up bootstrapping is to draw the whole sample (all replicates) for each hospital at once, then assign them to replicates. That way you only run ss1<- once per hospital. You can likely improve on that by not subsetting for each hospital. Another huge win might come from pre-allocating rather than rbinding. More suggestions on speed improvements.

    To re-allocate, calculate how many entries you need (call it N.out). Then, just before your loop, add:

    samp.out <- rep(NA, N.out)
    

    And replace your rbind line with:

    samp.out[ seq(i,i+N.iter) ] <- ss2
    

    Where i is your calculation of the first entry not yet filled, and i+N.iter is the last entry you have data to fill on this round.

    See the R Inferno for more details and tricks.

    Update

    You have two approaches and you’re mixing them. You can either make v.samp a data.frame and just sample all the rows into it in real-time, or you can sample IDs, and then select a data.frame using the vector of IDs outside of the loop. The key to the latter is that myDF[c(1,1,5,2,3),] will give you a data.frame which repeats the first row–exactly what you want, and exactly what that feature was designed for. Make sure v.samp is an ID that you can select from a data.frame on (either a row number or a row name), then select outside the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to correctly vertically align offsetting elements using Twitter Bootstrap with a fluid
Attempting/struggling to get registration and sign-up working within an active admin project. I have
I am attempting to create a Backbone.js view based on a Twitter bootstrap-modal, which
Just looked at the Bootstrap toolkit and attempting to get the dropdown's working on
Attempting to create a filter select to find schedules with coursedates within a given
In attempting to upgrade to Bootstrap 2 (using twitter-bootstrap-rails gem), it seems that page-specific
Using Bootstrap's typeahead javascript plugin, I'm attempting to change the data-source attribute via jQuery's
I'm attempting to gem install twitter-bootstrap-rails on my windows machine. This gem has a
I am attempting to build a simple content management system using Twitter's Bootstrap for
I am attempting to use Twitter Bootstrap input-append search and it's not lining up

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.