x<-c(4,2,3, 5, 6)
x
mean(x)
sd(x)
var(x)
X = 1,2,3 Y = 4,2,0
X = c(1,2,3)
Y = c(4,2,0)
paste0(" Mean of X = ", mean(X))
paste0(" Mean of Y = ", mean(Y))
paste0(" Mean of X+Y = ", mean(X+Y))
paste0(" Var of X = ", var(X))
paste0(" Var of Y = ", var(Y))
paste0(" Var of X+Y = ", var(X+Y))
paste0(" Cov(X,y) of X+Y = ", cov(X,Y))
w<- var(X)+var(Y) +2*cov(X,Y)
paste0(" var(X)+ var(Y)+2cov(X,Y) = ",w )
var(X+Y)
Żródło: https://www.geeksforgeeks.org/how-to-find-the-sample-size-for-t-test-in-r/
Seeking sample size for a t-test using R.
To conduct a reliable statistical test, it is essential to correctly determine the number of participants. In the case of a t-test, the sample size depends on several factors, such as the expected effect, significance level and power of the test. We will show how to calculate the optimal sample size using the R programming language.
Definitions of factors
__Effect size (d):__The size of the difference between groups or conditions. A larger effect size means that a smaller sample is required to detect this effect.
See:https://pl.wikipedia.org/wiki/Wielko%C5%9B%C4%87_efektu
Significance level (α): The probability of rejecting the null hypothesis when it is true (type I error). Usually α is set at 0.05.
Power (1 - β) : Probability of correctly rejecting the null hypothesis when it is false (type II error). Typically, the power is set at 0.80, which means an 80% chance of detecting an effect.
Test type : One-sample t-test, two-sample t-test (independent) or paired t-test.
Typical libraries:
pwr : A popular package that offers functions for calculating sample sizes for various tests, including t tests.
statistics :_ A basic R package can be used to perform basic t tests, although it does not directly provide sample size calculations.
power.t.test : A built-in function in R that calculates sample size for one-sample, two-sample and paired-sample t tests.
Translated with DeepL.com (free version)
The power.t.test()function in R is the most direct method for calculating sample size for t tests. This function supports t tests for one sample, two samples and paired t tests. The required parameters are effect size ( d), significance level ( alpha) and desired power ( power). Let’s calculate the required sample size for a two-sample t-test with a significance level of 0.05, a power of 0.80 and an expected effect size of 0.5.
result <- power.t.test( delta = .5 , sd = 1 , sig.level = 0.05 , power = 0.80 , type = "two.sample" )
result $ n
[1] 63.76576
This means that each group needs about 63 participants to reach a power of 80%.
Method 2: Using pwr.t.test()from the pwr package The pwr package is another option for calculating sample size. The function is versatile and allows you to calculate power, effect size or sample size depending on the given parameters .pwr.t.test()
install.packages( "pwr" )
Instalowanie pakietu w ‘C:/Users/pstasz/AppData/Local/R/win-library/4.2’
(ponieważ ‘lib’ nie jest określony)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/pwr_1.3-0.zip'
Content type 'application/zip' length 160429 bytes (156 KB)
downloaded 156 KB
pakiet ‘pwr’ został pomyślnie rozpakowany oraz sumy MD5 zostały sprawdzone
Pobrane pakiety binarne są w
C:\Users\pstasz\AppData\Local\Temp\RtmpEt7a4K\downloaded_packages
library( pwr )
# Oblicz rozmiar próby za pomocą testu pwr.t. dla dwupróbkowego testu t
pwr_result <- pwr.t.test ( d = 0.5 , sig.level = 0.05 , power = 0.80 , type = "two.sample" )
pwr_result $ n
[1] 63.76561
As with the previous method, this result indicates that 63 participants are needed in each group.
Visualize sample size calculations You can also visualize how different parameters (e.g., effect size or power) affect the required sample size.
# Utwórz sekwencję rozmiarów efektów/Sequnce of effects sizes
rozmiary_efektów <- seq ( 0.1 , 1.0 , by = .1 )
# Oblicz rozmiary próbek dla każdego rozmiaru efektu/ Calcuale sample size for each effect size
rozmiary_próbek <- sapply ( rozmiary_efektów , function(d) {
power.t.test ( delta = d , sd = 1 , sig.level = 0.05 , power = 0.80 , type = "two.sample" ) $ n
})
# Wykreśl wyniki/ Plot resuts
plot( rozmiary_efektów , rozmiary_próbek , typ = "b" , col = "blue" ,
xlab = "Effect size" , ylab = "Sample size" ,
main = "Effect size versus sample size (two-sample test T)" )
NA
NA
Visualize sample size calculations This will generate a graph showing how the required sample size decreases as the effect size increases. Larger effects require fewer participants to detect the difference.
Factors affecting the sample size calculation
Effect size : Larger effect sizes require smaller sample sizes. If the expected effect is small, you will need a larger sample to detect it.
Significance level : A lower significance level (0.01 instead of 0.05) will require a larger sample to maintain power.
Power : Higher desired power (0.90 instead of 0.80) means you will need a larger number of participants to confidently detect the effect.