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

The Archive Base Latest Questions

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

I’ve created the following code that nests a for loop inside of a for

  • 0

I’ve created the following code that nests a for loop inside of a for loop in R. It is a simulation to calculate Power. I’ve read that R isn’t great for doing for loops but I was wondering if there are any efficiencies I could apply to make this run a bit faster. I’m fairly new to R as well as programming of any sort. Right now the run times I’m seeing are:

m=10 I get .17 sec

m=100 I get 3.95 sec

m=1000 I get 246.26 sec

m=2000 I get 1003.55 sec

I was hoping to set the number of times to sample, m, upwards of 100K but I’m afraid to even set this at 10K

Here is the code:

m = 1000                        # number of times we are going to  take samples
popmean=120                     # set population mean at 120
popvar=225                      # set known/established population 
variance at 225
newvar=144                      # variance of new methodology 
alpha=.01                       # set alpha
teststatvect = matrix(nrow=m,ncol=1)    # empty vector to populate with test statistics
power = matrix(nrow=200,ncol=1)     # empty vector to populate with power

system.time(                    # not needed - using to gauge how long this takes
    for (n in 1:length(power))          # begin for loop for different sample sizes
      for(i in 1:m){                # begin for loop to take "m" samples
      y=rnorm(n,popmean,sqrt(newvar))   # sample of size n with mean 120 and var=144
      ts=sum((y-popmean)^2/popvar)      # calculate test statistic for each sample
      teststatvect[i]=ts            # loop and populate the vector to hold test statistics
      vecpvals=pchisq(teststatvect,n)   # calculate the pval of each statistic
      power[n]=length(which(vecpvals<=alpha))/length(vecpvals) # loop to populate      power vector. Power is the proportion lessthan ot equal to alpha
        }
   }
 )
  • 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-13T08:32:19+00:00Added an answer on June 13, 2026 at 8:32 am

    I reorganized your code a bit and got rid of the inner loop.

    • Sampling one long vector of random numbers (and then collapsing it into a matrix) is much faster than repeatedly sampling short vectors (replicate, as suggested in another answer, is nice for readability, but in this case you can do better by sampling random numbers in a block)
    • colSums is faster than summing inside a for loop or using apply.
    • it’s just sugar (i.e. it isn’t actually any more efficient), but you can use mean(pvals<=alpha) in place of sum(pvals<=alpha)/length(alpha)
    • I defined a function to return the power for a specified set of parameters (including sample size), then used sapply to range over the vector of sizes (not faster than a for loop, but cleaner and maybe easier to generalize).

    Code:

    powfun <- function(ssize=100,
                       m=1000,      ## samples per trial
                       popmean=120, ## pop mean
                       popvar=225,  ## known/established pop variance
                       newvar=144,  ## variance of new methodology
                       alpha=0.01,
                       sampchisq=FALSE)  ## sample directly from chi-squared distrib?
    {
        if (!sampchisq) {
          ymat <- matrix(rnorm(ssize*m,popmean,sd=sqrt(newvar)),ncol=m)
          ts <- colSums((ymat-popmean)^2/popvar)          ## test statistic
        } else {
          ts <- rchisq(m,df=ssize)*newvar/popvar
        }
        pvals <- pchisq(ts,df=ssize)                    ## pval
        mean(pvals<=alpha)                              ## power
    }
    

    Do you really need the power for every integer value of sample size, or would a more widely spaced sample be OK (if you need exact values, interpolation would probably be pretty accurate)

    ssizevec <- seq(10,250,by=5)
    set.seed(101)
    system.time(powvec <- sapply(ssizevec,powfun,m=5000))  ## 13 secs elapsed
    

    This is reasonably fast and might get you up to m=1e5 if you needed, but I’m not quite sure why you need results that are that precise — the power curve is reasonably smooth with m=5000 …

    If you’re impatiently waiting for long simulations, you can also get a progress bar to print by replacing sapply(ssizevec,powfun,m=5000) with library(plyr); aaply(ssizevec,.margins=1,powfun,.progress="text",m=5000)

    Finally, I think you can speed the whole up a lot by sampling chi-squared values directly, or by doing an analytical power calculation (!). I think that rchisq(m,df=ssize)*newvar/popvar is equivalent to the first two lines of the loop, and you might even be able to do a numerical computation on the chi-squared densities directly …

    system.time(powvec2 <- sapply(ssizevec,powfun,m=5000,sampchisq=TRUE))
    ## 0.24 seconds elapsed
    

    (I just tried this out, sampling m=1e5 at every value of sample size from 1 to 200 … it takes 24 seconds … but I still think it might be unnecessary.)

    A picture:

    par(bty="l",las=1)
    plot(ssizevec,powvec,type="l",xlab="sample size",ylab="power",
         xlim=c(0,250),ylim=c(0,1))
    lines(ssizevec,powvec2,col="red")
    

    enter image description here

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.