[R] R Code Execution taking forever

Paul Bernal p@u|bern@|07 @end|ng |rom gm@||@com
Sun Apr 24 21:21:57 CEST 2022


Dear friend Ebert,

The only reason why I labeled the numbers is because out instructor asked
for it(as part of the project). However, I agree with you 100% in that
labeling is pointless and doesn’t add any value.

>From the modified code shared by Rui, it seems to me that the dice function
is rather inefficient (I could be wrong).

I will run the mofied version provided by Rui and will let you all know how
it went. I even thought for a moment that the problem could be due to
system variables configurations or even my computer’s capacity (RAM and
processor).

The code I sent originally worked perfectly from 10 trials up to 100,000
trials but ejem trying with 1 million trials it was over, more than 24
hours and still didn’t finish execution.

Best,
Paul

El El dom, 24 de abr. de 2022 a la(s) 2:03 p. m., Ebert,Timothy Aaron <
tebert using ufl.edu> escribió:

> 1) Does it run perfectly with num_tirals_6 <- 100 ?
> 2) Rework the code to remove as much as possible from loops.
>             Renaming column names each time through the loop seems
> pointless.
>             Is the nested for loops converting the dice roll to person
> name necessary within the while loop?
> 3) Stop all other apps on the computer.
> 4) Consider rewriting to take advantage of multiple cores in your system
> in parallel processing (this might or might not help much).
> 5) Rerun with num_trials_6 set to different values 10, 100, 1000, and
> 10000. Linear regression with run time and trial size should let you
> estimate run time for 1 million.
>
>
> Tim
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Rui Barradas
> Sent: Sunday, April 24, 2022 5:44 AM
> To: Paul Bernal <paulbernal07 using gmail.com>; R <r-help using r-project.org>
> Subject: Re: [R] R Code Execution taking forever
>
> [External Email]
>
> Hello,
>
> I'm having trouble running the code, where does function dice come from?
> CRAN package dice only has two functions,
>
> getEventProb
> getSumProbs
>
> not a function dice.
>
> Can you post a link to where the package/function can be found?
>
> Rui Barradas
>
>
> Às 02:00 de 24/04/2022, Paul Bernal escreveu:
> > Dear R friends,
> >
> > Hope you are doing great. The reason why I am contacting you all, is
> > because the code I am sharing with you takes forever. It started
> > running at
> > 2:00 AM today, and it's 7:52 PM and is still running (see code at the
> > end of this mail).
> >
> > I am using Rx64  4.1.2, and the code is being executed in RStudio. The
> > RStudio version I am currently using is Version 2022.02.0 Build 443
> > "Prairie Trillium" Release (9f796939, 2022-02-16) for Windows.
> >
> > My PC specs:
> > Processor: Intel(R) Core(TM) i5-10310U CPU @ 1.70 GHz Installed RAM:
> > 16.0 GB (15.6 GB usable) System type: 64-bit operating system,
> > x64-based processor Local Disc(C:) Free Space: 274 GB
> >
> > I am wondering if there is/are a set of system variable(s) or
> > something I could do to improve the performance of the program.
> >
> > It is really odd this code has taken this much (and it is still running).
> >
> > Any help and/or guidance would be greatly appreciated.
> >
> > Best regards,
> > Paul
> >
> >
> >
> >
> > #performing 1,000,000 simulations 10 times
> > num_trials_6 = 1000000
> > dice_rolls_6 = num_trials_6*12
> > num_dice_6 = 1
> > dice_sides_6 = 6
> >
> > prob_frame_6 <- data.frame(matrix(ncol = 10, nrow = 1))
> >
> > k <- 0
> > while(k < 10){
> >    dice_simul_6 = data.frame(dice(rolls = dice_rolls_6, ndice =
> > num_dice_6, sides = dice_sides_6, plot.it = FALSE))
> >
> >    #constructing matrix containing results of all dice rolls by month
> >    prob_matrix_6 <- data.frame(matrix(dice_simul_6[,1], ncol = 12,
> > byrow =
> > TRUE))
> >
> >    #naming each column by it's corresponding month name
> >    colnames(prob_matrix_6) <-
> > c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","D
> > ec")
> >
> >
> >    #assigning each person´s name depending on the number showed in the
> > dice once rolled
> >    for (i in 1:nrow(prob_matrix_6)){
> >      for (j in 1:ncol(prob_matrix_6)){
> >        if (prob_matrix_6[i,j] == 1){
> >          prob_matrix_6[i,j] = "Alice"
> >        }
> >        if (prob_matrix_6[i,j] == 2){
> >          prob_matrix_6[i,j] = "Bob"
> >        }
> >        if (prob_matrix_6[i,j] == 3){
> >          prob_matrix_6[i,j] = "Charlie"
> >        }
> >        if (prob_matrix_6[i,j] == 4){
> >          prob_matrix_6[i,j] = "Don"
> >        }
> >        if (prob_matrix_6[i,j] == 5){
> >          prob_matrix_6[i,j] = "Ellen"
> >        }
> >        if (prob_matrix_6[i,j] == 6){
> >          prob_matrix_6[i,j] = "Fred"
> >        }
> >
> >      }
> >    }
> >
> >    #calculating column  which will have a 1 if trial was successful
> > and a 0 otherwise
> >    prob_matrix_6['success'] <- for (i in 1:nrow(prob_matrix_6)){
> >      if (("Alice" %in% prob_matrix_6[i,]) & ("Bob" %in%
> > prob_matrix_6[i,]) & ("Charlie" %in% prob_matrix_6[i,]) & ("Don" %in%
> > prob_matrix_6[i,]) & ("Ellen" %in% prob_matrix_6[i,]) & ("Fred" %in%
> prob_matrix_6[i,])){
> >        prob_matrix_6[i,13] = 1
> >      }else{
> >        prob_matrix_6[i,13] = 0
> >      }
> >    }
> >
> >    #relabeling column v13 so that its new name is success
> >    colnames(prob_matrix_6)[13] <- "success"
> >
> >
> >    #calculating probability of success
> >
> >    p6 = sum(prob_matrix_6$success)/nrow(prob_matrix_6)
> >    prob_frame_6 <- cbind(prob_frame_6, p6)
> >
> >    k = k + 1
> >
> > }
> >
> > prob_frame_6 <- prob_frame_6[11:20]
> > colnames(prob_frame_6) <-
> > c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10")
> > average_prob_frame_6 <- rowMeans(prob_frame_6) trial_1000000_10_frame
> > <- cbind(prob_frame_6, average_prob_frame_6)
> > final_frame_6 <- trial_1000000_10_frame
> > colnames(final_frame_6) <-
> > c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10",
> > "avg_prob_frame_5")
> >
> > write.csv(final_frame_6, "OneMillion_Trials_Ten_Times_Results.csv")
> > print(final_frame_6)
> > print(paste("The average probability of success when doing 1,000,000
> > trials
> > 10 times is:", average_prob_frame_6))
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mail
> > man_listinfo_r-2Dhelp&d=DwIDaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs
> > Rzsn7AkP-g&m=wM6IILK-62Cb0r02bfEW05eVb0lndtvSWfuz7gHTlutarVXWwMYm2ucHQ
> > 8zpRTow&s=il9X1N5C_yJqnTaXdQFBZFkG2En_jhiejElnzL__WdA&e=
> > PLEASE do read the posting guide
> > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or
> > g_posting-2Dguide.html&d=DwIDaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA
> > sRzsn7AkP-g&m=wM6IILK-62Cb0r02bfEW05eVb0lndtvSWfuz7gHTlutarVXWwMYm2ucH
> > Q8zpRTow&s=027mAUgUIZGXPwemk-9LCBL8uREpR8MMkM5-4Hf4y5w&e=
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIDaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=wM6IILK-62Cb0r02bfEW05eVb0lndtvSWfuz7gHTlutarVXWwMYm2ucHQ8zpRTow&s=il9X1N5C_yJqnTaXdQFBZFkG2En_jhiejElnzL__WdA&e=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIDaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=wM6IILK-62Cb0r02bfEW05eVb0lndtvSWfuz7gHTlutarVXWwMYm2ucHQ8zpRTow&s=027mAUgUIZGXPwemk-9LCBL8uREpR8MMkM5-4Hf4y5w&e=
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list