# How to calculate sample size for linear regression when you know the power, effect size, no. predictors and significance level (in R)?

I'm trying to calculate sample sizes of a study using the linear regression method. The model has only one predictor and I know that I require 80% power + Significance level 0.05 to reject the null hypothesis. I've read in other articles that for linear regression, they suggest calculating effect size using R^2. However, I am trying to calculate the sample sizes using Cohen's and Sawilowsky's effect sizes: Small (0.2), Medium (0.5), Large (0.8), Very Large (1.2) and Huge (2). Is there a way to do this in R?

It's perhaps also important to mention that I am trying to detect a difference, not an Odds Ratio.

Thanks.

Try this:

samplesize<- function(alpha, power, meandiff, sd, p,
type = c('one.sample', 'two.sample', 'paired'),
tail=c("one.tailed", "two.tailed")) {
if((type=="one.sample")|(type=="paired")){
if(tail=="one.tailed") {
n=((qnorm(1-alpha)+qnorm(power))*sd/meandiff)^2
names(n)= "n (one.tailed)"
n
}
else {
n=((qnorm(1-alpha/2)+qnorm(power))*sd/meandiff)^2
names(n)= "n (two.tailed)"
n
}
}
else {
if(tail=="one.tailed") {
n=((qnorm(1-alpha)+qnorm(power))*sd/meandiff)^2
names(n)= "n (one.tailed)"
n/(p*(1-p))
}
else {
n=((qnorm(1-alpha/2)+qnorm(power))*sd/meandiff)^2
names(n)= "n (two.tailed)"
n/(p*(1-p))
}
}
}


Inputs:

alpha = alpha in decimal

power = power in decimal

meandiff = μ-μ0 or μ12

sd = standard deviation

p = proportion of size of first group to size of second group (for two sample test only)

type = one.sample or two.sample or paired

tail = one.tailed or two tailed

Examples

> samplesize(0.05, .8, 10, 20, type = 'one.sample', tail = "two.tailed")
n (two.tailed)
31.39552
> samplesize(0.05, .8, 10, 20, type = 'paired', tail = "two.tailed")
n (two.tailed)
31.39552
> samplesize(0.05, .8, 10, 20, 0.4, type = 'two.sample', tail = "two.tailed")
n (two.tailed)
130.8147
> samplesize(alpha = 0.05, power = 0.80, meandiff = 10, sd =30,
type = "paired", tail = "two.tailed")
n (two.tailed)
70.63992