1. ## 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. ## 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.

3. ## 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. ## 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.

5. ## 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. ## 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. ## Re: Calculate power

Originally Posted by mvernengo
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 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.

 Tweet

#### Posting Permissions

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