Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using bootstrap technique to draw and save 1000 random sample from a panel data

    Using the bootstrap technique to draw and save 1000 random samples from the nlswork and save them as a data frame. Given the panel structure of the nlswork data, the panel characteristics of the data must be specified as explained here. Panel characteristics of the nlswork data can be specified as shown below.

    It is of note that "two cluster options in the bootstrap command line will be needed. The first option, cluster(idcode), identifies the original panel variable in the dataset, whereas the second, idcluster(newid), creates a unique identifier for each of the selected clusters (panels in this case). Thus if some panels were selected more than once, the temporary variable newid would assign a different ID number to each resampled panel. If the two clusters indicators are omitted, bootstrap will not take into account the panel structure of the data; rather, it will construct the simulated samples by randomly selecting individual observations from the pooled data."

    webuse nlswork, clear
    generate newid = idcode
    tsset newid year

  • #2
    You wouldn't use bootstrap to do what you're asking. It's a prefix command that's used before an estimation command or one that computes summary statistics.

    To take bootstrap samples (with replacement), you'd use bsample in a loop, each pass appending and saving the samples (indexed with loop number), and then calling up the original dataset. It has the same options for preserving original panel IDs and creating new ones for the sample as bootstrap and you'd use them in the same way as the FAQ you link to shows. (And not like your quoted snippet.)

    It's a fairly substantial dataset, and you might be able to save some time by storing it in a repository frame in memory and using frame copy, replace to replenish your working frame after you draw and save the samples each pass.

    I'm curious what you're going to do with this collection.

    Comment


    • #3
      Thanks for your clarification. I have tried the following code but getting an error of "option : not allowed".

      Code:
      // Clear any existing data in memory
      clear all
      
      // Load the nlswork dataset into memory
      webuse nlswork, clear
      
      // Define the number of bootstrap samples you want to generate
      local num_samples = 100
      
      // Create a repository frame to store the original dataset
      tempname repo_frame
      frame create `repo_frame'
      
      // Copy the original dataset to the repository frame
      frame copy `repo_frame' `"_original_dataset"'
      
      // Create a loop to generate bootstrap samples
      forval i = 1/`num_samples' {
      
        // Generate a unique sample ID for each pass
        local sample_id `i'
      
        // Create a temporary frame to store the bootstrap sample
        tempname sample_frame
        frame create `sample_frame'
      
        // Bootstrap sample using bsample with panel characteristics
        bsample 1, cluster(idcode) idcluster(newid `sample_id') : frame copy `sample_frame'
      
        // Append the bootstrap sample to the repository frame
        frame append `repo_frame' `sample_frame'
      
        // Save the bootstrap sample as a separate dataset
        save "bootstrap_sample_`i'.dta", replace
      
        // Clear the temporary sample frame
        frame drop `sample_frame'
      
      }
      
      // Restore the original dataset from the repository frame
      frame copy `"_original_dataset"' `"_working_dataset"'
      
      // Perform operations on the original dataset (e.g., estimate a model)
      
      // Clean up by dropping the repository and working frames
      frame drop `repo_frame'
      frame drop `"_working_dataset"'

      Comment


      • #4
        Use set trace on. And set tracedepth as needed.
        Code:
        help set trace
        Hint: bootstrap is a prefix command; bsample is not. Double-check the command's help file for how to use it.

        Comment

        Working...
        X