Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonystevendick committed Aug 21, 2024
1 parent b1007e5 commit c2bdec3
Show file tree
Hide file tree
Showing 18 changed files with 5,238 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified docs/.DS_Store
Binary file not shown.
1,197 changes: 1,197 additions & 0 deletions docs/combined_plot.eps

Large diffs are not rendered by default.

Binary file added docs/combined_plot.tiff
Binary file not shown.
686 changes: 686 additions & 0 deletions docs/power_curves.html

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions docs/power_curves.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
title: "Plots of Power Curves"
author: "Anthony Steven Dick"
format:
html:
code-fold: true
---

## Load required libraries
```{r setup, include=TRUE}
library(ggplot2)
library(pwr)
library(patchwork)
```

## Give range of correlations, range of Cohen's d, and power values

```{r}
# range of correlations
r <- seq(.02, .1, .001)
nr <- length(r)
# range of Cohen's d
d <- seq(.02, 0.2, length.out = nr) # Adjusted range for Cohen's d
nd <- length(d)
# power values
p <- c(.80, .99)
np <- length(p)
```

## Obtain Sample Sizes

```{r}
# obtain sample sizes for Pearson's correlation coefficient
samsize_r <- array(numeric(nr*np), dim=c(nr,np))
for (i in 1:np){
for (j in 1:nr){
result <- pwr.r.test(n = NULL, r = r[j],
sig.level = .05, power = p[i],
alternative = "two.sided")
samsize_r[j,i] <- ceiling(result$n)
}
}
# obtain sample sizes for Cohen's d
samsize_d <- array(numeric(nd*np), dim=c(nd,np))
for (i in 1:np){
for (j in 1:nd){
result <- pwr.t.test(n = NULL, d = d[j],
sig.level = .05, power = p[i],
alternative = "two.sided")
samsize_d[j,i] <- ceiling(result$n)
}
}
```

## Create dataframes for ggplot for Pearson's correlation and Cohen's d

```{r}
# create dataframe for ggplot for Pearson's correlation coefficient
df_r <- data.frame(r = rep(r, np), samsize = c(samsize_r[,1], samsize_r[,2]),
power = rep(p, each = nr), measure = "Pearson's r")
# create dataframe for ggplot for Cohen's d
df_d <- data.frame(d = rep(d, np), samsize = c(samsize_d[,1], samsize_d[,2]),
power = rep(p, each = nd), measure = "Cohen's d")
# Rename columns in df_d to match df_r
names(df_d) <- c("r", "samsize", "power", "measure")
# Combine data frames
df_combined <- rbind(df_r, df_d)
```

## Plotting

```{r}
# Define Hufflepuff colors
hufflepuff_colors <- c("#FFDD00", "#000000")
# Plot for Pearson's correlation coefficient
plot_r <- ggplot(df_r, aes(x = r, y = samsize, color = factor(power))) +
geom_line(linewidth = 1) +
scale_color_manual(values = hufflepuff_colors) +
labs(x = "Correlation Coefficient |r|",
y = "Sample Size (n)",
color = "Power",
title = "Power as a Function of Sample Size for r") +
theme_minimal() +
geom_hline(yintercept = 11865, linetype = "dashed", color = "black") +
scale_y_continuous(breaks = seq(0, 45000, by = 2500)) +
scale_x_continuous(breaks = seq(0, 0.1, by = 0.01)) + # Adjusted x-axis breaks
theme(legend.position = "none", text = element_text(size = 16)) # Adjust font size here
```

## More Plotting

```{r}
# Plot for Cohen's d
plot_d <- ggplot(df_d, aes(x = r, y = samsize, color = factor(power))) +
geom_line(linewidth = 1) +
scale_color_manual(values = hufflepuff_colors) +
labs(x = "Cohen's d",
y = "Sample Size (n)",
color = "Power",
title = "Power as a Function of Sample Size for Cohen's d") +
theme_minimal() +
geom_hline(yintercept = 11865, linetype = "dashed", color = "black") +
scale_y_continuous(breaks = seq(0, 100000, by = 5000)) +
scale_x_continuous(breaks = seq(0, .2, by = 0.05)) + # Adjusted x-axis breaks
theme(legend.position = "none", text = element_text(size = 16)) # Adjust font size here
```

## Combine Plots and Save if Needed

```{r, fig.width=7, fig.height=5}
# Combine plots using patchwork
combined_plot <- plot_r + plot_d + plot_layout(ncol = 2)
# Add single legend at the bottom
combined_plot <- combined_plot + theme(legend.position = "right", text = element_text(size = 16))
# Display the combined plot
print(combined_plot)
# Save the combined plot to a .tiff file with 600 dpi resolution
ggsave("combined_plot.tiff", plot = combined_plot, dpi = 600)
# Save the combined plot to a .eps file
ggsave("combined_plot.eps", plot = combined_plot, device = "eps")
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c2bdec3

Please sign in to comment.