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 7795477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:04:49+00:00 2026-06-01T23:04:49+00:00

I need to generate simulated data where the percent censored cannot be 0 or

  • 0

I need to generate simulated data where the percent censored cannot be 0 or 1. That’s why I use while loop. The problem is if I increase count to 10,000 (instead of 5), the program is very slow. I have to repeat this with 400 different scenarios so it is extremely slow. I’m trying to figure out places where I can vectorize my code piece by piece. How can I avoid while-loop and still able to keep the condition?

Another approach is keep the while loop and generate a list of 10,000 dataset that meet my criteria and then apply the function to the list. Here I use summary function as an example but my real function use both X_after and delta (ie. mle(X_after,delta)). Is this a better option if I have to use while loop?

Another concern I have is memory issue. How can I avoid using up memory while doing such large simulation?

mu=1 ; sigma=3 ; n=10 ; p=0.10
dset <- function (mu,sigma, n, p) {              
   Mean <- array()
   Median <- array()
   Pct_cens_array <- array()
   count = 0
   while(count < 5) { 

     lod <- quantile(rlnorm(100000, log(mu), log(sigma)), p = p)
     X_before <- rlnorm(n, log(mu), log(sigma))
     X_after <-  ifelse(X_before <= lod, lod,  X_before)
     delta <- ifelse(X_before <= lod, 1,  0) 
     pct_cens <- sum(delta)/length(delta)
     # print(pct_cens)
     if (pct_cens == 0 | pct_cens == 1 ) next
     else {
        count <-  count +1
        if (pct_cens > 0 & pct_cens < 1) {
             sumStats <- summary(X_after)
             Median[count] <- sumStats[3]
             Mean [count]<- sumStats[4]
             Pct_cens_array [count] <- pct_cens 
             print(list(pct_cens=pct_cens,X_after=X_after, delta=delta, Median=Median,Mean=Mean,Pct_cens_array=Pct_cens_array))
          }
       }
    }

          return(data.frame(Pct_cens_array=Pct_cens_array, Mean=Mean, Median=Median)) 
 }
  • 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-01T23:04:50+00:00Added an answer on June 1, 2026 at 11:04 pm

    I’ve made a few little tweaks to your code without changing the whole style of it. It would be good to heed Yoong Kim’s advice and try to break up the code into smaller pieces, to make it more readable and maintainable.

    • Your function now gets two “n” arguments, for how many samples you have in each row, and how many iterations (columns) you want.

    • You were growing the arrays Median and Mean in the loop, which requires a lot of messing about reallocating memory and copying things, which slows everything down. I’ve predefined X_after and moved the mean and median calculations after the loop to avoid this. (As a bonus, mean and median only get called once instead of n_iteration times.)

    • The calls to ifelse weren’t really needed.

    • It is a little quicker to call rlnorm once, generating enough values for x and the lod, than to call it twice.

    Here’s the updated function.

    dset2 <- function (mu, sigma, n_samples, n_iterations, p) {    
      X_after <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
      pct_cens <- numeric(n_iterations)
      count <- 1
      while(count <= n_iterations) {     
        random_values <- rlnorm(2L * n_samples, log(mu), log(sigma))
        lod <- quantile(random_values[1:n_samples], p = p)
        X_before <- random_values[(n_samples + 1L):(2L * n_samples)]
        X_after[count, ] <- pmax(X_before, lod)
        delta <- X_before <= lod
        pct_cens[count] <- mean(delta)
        if (pct_cens > 0 && pct_cens < 1 ) count <- count + 1
      }
    
      Median <- apply(X_after, 1, median)
      Mean <- rowMeans(X_after)
      data.frame(Pct_cens_array=pct_cens, Mean=Mean, Median=Median) 
    }
    

    Compare timings with, for example,

    mu=1
    sigma=3
    n_samples=10L
    n_iterations = 1000L
    p=0.10
    system.time(dset(mu,sigma, n_samples, n_iterations, p))
    system.time(dset2(mu,sigma, n_samples, n_iterations, p))
    

    On my machine, there is a factor of 3 speedup.

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

Sidebar

Related Questions

I need generate action links outside controllers. I can use Html.Action in Views, Url.Action
I need generate thumbnails for a bunch of jpegs (200,000+) but I want to
I want to use hipay in my site. So i need generate a xml
I need to generate java class definition from json data. Is there any tool
I need to generate a XSD for a XML file we are gonna use
I need to generate and send SyncML OTA SMS. I have sms provider that
I need to generate the .bin file that contains the VBA macro part of
I need to generate an editable xml file to supply content to a flash
I need to generate Entities/Object from selected tables - not all. Is this possible
I need to generate invoices in large batch which will be transformed to EDI

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.