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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:31:47+00:00 2026-06-01T02:31:47+00:00

I am trying write a function that generates simulated data but if the simulated

  • 0

I am trying write a function that generates simulated data but if the simulated data does not meet the condition, I need to skip it and if it does meet the condition, then I will apply the function summary.

I would like to loop it until I find 10 valid datasets and then stop. (I actually have to do this until it reaches 10000). Here is the code. The code sort of works except it does not stop. I think I probably placed the next and break function in the wrong place. I hope someone could help me write this together.

Another way I could approach this is to generate all the valid data first and then apply the function find_MLE (summary) to the final list.

Edit: I put break inside repeat. I edit the code to make it reproducible. Still the code keeps generating data and does not break.

here is a reproducible version

      validData <-  function(GM, GSD,sampleSize, p) {

            count=0
            for (i in 1:n) {  
              repeat {
                lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
                X_before <- rlnorm(sampleSize, log(GM), log(GSD))
                Xs <-  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 {

                  sumStats <- summary(Xs)

                  Med <- sumStats[3]
                  Ave <- sumStats[4]

                      }

                  count<- count+1

                if (count == 10) break
              }}
            return(c(pct_cens, Med, Ave))
          }



          validData(GM=1,GSD=2,sampleSize=10,p=0.1)            
  • 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-01T02:31:48+00:00Added an answer on June 1, 2026 at 2:31 am

    Thanks for your help. I was able to write a function without using break function! I posted it here in case other people might find it helpful.

        dset <- function (GM, GSD, n, p) {
    
              Mean <- array()
              Median <- array()
    
              count = 0
              while(count < 10) { 
    
                lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
                X_before <- rlnorm(n, log(GM), log(GSD))
                Xs <-  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(Xs)
                        Median[count] <- sumStats[3]
                        Mean [count]<- sumStats[4]
    
                        print(list(pct_cens=pct_cens,Xs=Xs, delta=delta, Median=Median,Mean=Mean))
              }
                }
              }
    
              return(data.frame( Mean=Mean, Median=Median)) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that would use its first argument, then send
I am trying to write my first real python function that does something real.
I am trying to write a generic function that generates an Xelement based on
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a function that includes a file named header.php in
I am trying to write a function that will filter a list of tuples
I am trying to write a function that will: Disable the submit button when
I am trying to write a function that returns the index of the passed
I am trying to write a function that will randomly return an (x,y) co-ordinates

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.