+ Reply to Thread
Results 1 to 7 of 7

Thread: Calculate power

  1. #1
    Points: 130, Level: 2
    Level completed: 60%, Points required for next Level: 20

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculate power




    We were given an R code for our home work, but it doesn't seem to work.
    Assignment is: N = 500, best estimate of the effect size is correlation I calculated last week r = 0.107176 and I also want to conduct a two-tailed test with an alpha of 0.05 and I want a power of 0.80. Question is: is it worth doing the study if I can only randomly assign 500 people. (so sample size is fixed.)

    The code our prof. gave us for R studio doesn't work because I am obviously missing something here.

    simple.power.r<-function(n,r=NA){
    #simple function to provide approximate power estimates
    # (two-tailed, alpha = 0.05)
    #given correlation values
    #
    r<-abs(r)
    tvalues<-r*sqrt((n-2)/(1-(r^2)))
    power <-1-pnorm(1.96(1.96-tvalue)
    }

    I replaced n with 500
    and r with my correlation in this function before I ran it.

    Any ideas what I did wrong?

  2. #2
    Devorador de queso
    Points: 97,410, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,981
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Calculate power

    You didn't actually tell us what is going wrong are you getting an error or is it not running or is it giving the wrong answer?

    Anyways count your parenthesis on the last line.
    I don't have emotions and sometimes that makes me very sad.

  3. #3
    Points: 130, Level: 2
    Level completed: 60%, Points required for next Level: 20

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculate power

    I noticed that error. This is the latest version I tried. It works until the last line. I get my results (power and tvalue) displayed in the global environment after my last code, but then it shows up with this error, so there is a problem with setting up this function. Not sure what it is.

    simple.power.r<-function(n , r= 0.107176){
    > #simple function to provide approximate power estimates
    > # (two-tailed, alpha = 0.05)
    > #given correlation values
    > #
    > r<-abs(0.107176)
    > tvalue<-r*sqrt((500-2)/(1-(r^2)))
    > power <-1-pnorm (1.96-tvalue)

    Now calculates power and displays it in global environment.

    Then last line:
    return (power)
    }
    Error: No function to return from, jumping to top level.

  4. #4
    Devorador de queso
    Points: 97,410, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,981
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Calculate power

    Can you paste your session in. The one that gives that - without providing any commentary. I think you excluded some of the important pieces but it's hard to know for sure with you not paying exactly what was entered and was printed.
    I don't have emotions and sometimes that makes me very sad.

  5. #5
    Human
    Points: 12,908, Level: 74
    Level completed: 15%, Points required for next Level: 342
    Awards:
    Master Tagger
    GretaGarbo's Avatar
    Posts
    1,394
    Thanks
    458
    Thanked 471 Times in 411 Posts

    Re: Calculate power

    Now you have changed this line:

    Code: 
     
    power <-1-pnorm (1.96-tvalue)
    Does work now? And where is the rest of the code (generating the random numbers for example) ?


    Make a reproducible example!

  6. #6
    Points: 2, Level: 1
    Level completed: 3%, Points required for next Level: 48

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculate power

    it seems the size doesnt matter
    > simple.power.r = function(n, r=0.1) {r=abs(r); tvalue=r*sqrt((500-2)/(1-(r^2))); power = 1-pnorm(1.96-tvalue); return(power)}
    > simple.power.r(10000)
    [1] 0.6113478
    > simple.power.r(1000)
    [1] 0.6113478
    > simple.power.r(100)
    [1] 0.6113478
    > simple.power.r(10)
    [1] 0.6113478
    > simple.power.r(5)
    [1] 0.6113478

  7. #7
    Points: 130, Level: 2
    Level completed: 60%, Points required for next Level: 20

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calculate power


    Quote Originally Posted by mvernengo View Post
    it seems the size doesnt matter
    > simple.power.r = function(n, r=0.1) {r=abs(r); tvalue=r*sqrt((500-2)/(1-(r^2))); power = 1-pnorm(1.96-tvalue); return(power)}
    > simple.power.r(10000)
    [1] 0.6113478
    > simple.power.r(1000)
    [1] 0.6113478
    > simple.power.r(100)
    [1] 0.6113478
    > simple.power.r(10)
    [1] 0.6113478
    > simple.power.r(5)
    [1] 0.6113478
    I keep getting different things, but actually your answer has already helped me a whole lot.
    I think I had a couple more mistakes in my code that I tried to fix. I believe that sample size HAS to matter for this function. Can you try this code and let me know and if it makes any sense at all? I think I am not supposed to add any of my values into the original code for the function just into the final simple.power.r code?
    Let's try this one. Can you let me know if you get the same result and if it makes sense?

    > simple.power.r = function(n, r=NA) {r=abs(r); tvalue=r*sqrt((n-2)/(1-(r^2))); power = 1-pnorm(1.96-tvalue); return(power)}
    > simple.power.r (n=500, r=.107176)
    [1] 0.672052

    Now one should get different power values when n changes.

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats