concept sample size in category R

appears as: sample sizes, sample size, sample sizes, The sample sizes, Sample size, samples sizes
R in Action, Third Edition

This is an excerpt from Manning's book R in Action, Third Edition.

The wmc() function first provides the sample sizes, medians, and median absolute deviations for each group #2. The West has the lowest illiteracy rate, and the South has the highest. The function then generates six statistical comparisons (West versus North Central, West versus Northeast, West versus South, North Central versus Northeast, North Central versus South, and Northeast versus South) #3. You can see from the two-sided p-values (p) that the South differs significantly from the other three regions and that the other three regions don’t differ from each other at a p < .05 level.

Although the sample size and significance level are under the direct control of the researcher, power and effect size are affected more indirectly. For example, as you relax the significance level (in other words, make it easier to reject the null hypothesis), power increases. Similarly, increasing the sample size increases power.

Before leaving the pwr package, let’s look at a more involved graphing example. Suppose you’d like to see the sample size necessary to declare a correlation coefficient statistically significant for a range of effect sizes and power levels. You can use the pwr.r.test() function and for loops to accomplish this task, as shown in the following listing.

Listing 10.2 Sample size curves for detecting correlations of various sizes
library(pwr)
r <- seq(.1,.5,.01)                                          #1  
p <- seq(.4,.9,.1)                                       
 
df <- expand.grid(r, p)
colnames(df) <- c("r", "p")
 
for (i in 1:nrow(df)){                                       #2
    result <- pwr.r.test(r = df$r[i],
                         sig.level = .05, power = df$p[i],
                         alternative = "two.sided")
    df$n[i] <- ceiling(result$n)
}
 
library(ggplot2)                                             #3
ggplot(data=df,
       aes(x=r, y=n, color=factor(p))) +
  geom_line(size=1) +
  theme_bw() +
  labs(title="Sample Size Estimation for Correlation Studies",
       subtitle="Sig=0.05 (Two-tailed)",
       x="Correlation Coefficient (r)",
       y="Samsple Size (n)",
       color="Power")
1 Sets the range of correlations and power values
2 Obtains sample sizes
3 Plot power curves

Listing 10.2 uses the seq() function to generate a range of effect sizes r (correlation coefficients under H1) and power levels p #1. The expand.grid() function is used to create a data frame with every combination of these two variables.  A for loop then cycles through rows of the data frame, calculating the sample size (n) for that row's correlation and power level, and saving the result #2. The ggplot2 package is then used to plot a sample size vs. correlation curve for each power level #3. The resulting graph is displayed in figure 10.3.

Figure 10.3 Sample size curves for detecting a significant correlation at various power levels

As you can see from the graph, you’d need a sample size of approximately 75 to detect a correlation of 0.20 with 40% confidence. You’d need approximately 185 additional observations (n = 260) to detect the same correlation with 90% confidence. With simple modifications, the same approach can be used to create sample size and power curve graphs for a wide range of statistical tests.

R in Action, Second Edition: Data analysis and graphics with R

This is an excerpt from Manning's book R in Action, Second Edition: Data analysis and graphics with R.

Listing 6.5. Pie charts
par(mfrow=c(2, 2)) #1 slices <- c(10, 12,4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pie(slices, labels = lbls, main="Simple Pie Chart") pct <- round(slices/sum(slices)*100) #2 lbls2 <- paste(lbls, " ", pct, "%", sep="") #2 pie(slices, labels=lbls2, col=rainbow(length(lbls2)), #2 main="Pie Chart with Percentages") #2 library(plotrix) pie3D(slices, labels=lbls,explode=0.1, main="3D Pie Chart ") mytable <- table(state.region) #3 lbls3 <- paste(names(mytable), "\n", mytable, sep="") pie(mytable, labels = lbls3, main="Pie Chart from a Table\n (with sample sizes)") #1: 1 Combines four graphs into one #2: 2 Adds percentages to the pie chart #3: 3 Creates a chart from the table

Although the sample size and significance level are under the direct control of the researcher, power and effect size are affected more indirectly. For example, as you relax the significance level (in other words, make it easier to reject the null hypothesis), power increases. Similarly, increasing the sample size increases power.

It’s important to keep in mind that Cohen’s benchmarks are just general suggestions derived from a range of social research studies and may not apply to your particular field of research. An alternative is to vary the study parameters and note the impact on such things as sample size and power. For example, again assume that you want to compare five groups using a one-way ANOVA and a 0.05 significance level. The following listing computes the sample sizes needed to detect a range of effect sizes and plots the results in figure 10.2.

Figure 10.2. Sample size needed to detect various effect sizes in a one-way ANOVA with five groups (assuming a power of 0.90 and significance level of 0.05)
Listing 10.1. Sample sizes for detecting significant effects in a one-way ANOVA
library(pwr) es <- seq(.1, .5, .01) nes <- length(es) samsize <- NULL for (i in 1:nes){ result <- pwr.anova.test(k=5, f=es[i], sig.level=.05, power=.9) samsize[i] <- ceiling(result$n) } plot(samsize,es, type="l", lwd=2, col="red", ylab="Effect Size", xlab="Sample Size (per cell)", main="One Way ANOVA with Power=.90 and Alpha=.05")

Graphs such as these can help you estimate the impact of various conditions on your experimental design. For example, there appears to be little bang for the buck in increasing the sample size above 200 observations per group. We’ll look at another plotting example in the next section.