knitr::opts_chunk$set(echo = TRUE)
B. How many subjects are needed to detect the main effect of A with power = .8?
#load libraries #load libraries library(tibble) library(ggplot2) library(patchwork) library(tidyverse) library(ggplot2) df <- data.frame(A = c("A1", "A1", "A2","A2"), B = c("B1", "B2", "B1", "B2"), dv = c(0,0,0.5,0.25) ) ggplot(df, aes(y=dv, x= B, fill = A)) + geom_bar(stat = "identity", position = "dodge") ggplot(df, aes(y=dv, x=B, color = A)) + geom_point()+ geom_line()
N <- 60 A_pvalue <- c() B_pvalue <- c() AB_pvalue <- c() for(i in 1:1000){ IVA <- rep(rep(c("1","2"), each=2),N) IVB <- rep(rep(c("1","2"), 2),N) DV <- c(replicate(N,c(rnorm(1,0,1), # means A1B1 rnorm(1,0,1), # means A1B2 rnorm(1,.5,1), # means A2B1 rnorm(1,.25,1) # means A2B2 ))) sim_df <- data.frame(IVA,IVB,DV) aov_results <- summary(aov(DV~IVA*IVB, sim_df)) A_pvalue[i]<-aov_results[[1]]$`Pr(>F)`[1] B_pvalue[i]<-aov_results[[1]]$`Pr(>F)`[2] AB_pvalue[i]<-aov_results[[1]]$`Pr(>F)`[3] } length(A_pvalue[A_pvalue<0.05])/1000 length(B_pvalue[B_pvalue<0.05])/1000 length(AB_pvalue[AB_pvalue<0.05])/1000
C. How many subjects are needed to detect the interaction effect with power = .8?
N <- 525 A_pvalue <- c() B_pvalue <- c() AB_pvalue <- c() for(i in 1:1000){ IVA <- rep(rep(c("1","2"), each=2),N) IVB <- rep(rep(c("1","2"), 2),N) DV <- c(replicate(N,c(rnorm(1,0,1), # means A1B1 rnorm(1,0,1), # means A1B2 rnorm(1,.5,1), # means A2B1 rnorm(1,.25,1) # means A2B2 ))) sim_df <- data.frame(IVA,IVB,DV) aov_results <- summary(aov(DV~IVA*IVB, sim_df)) A_pvalue[i]<-aov_results[[1]]$`Pr(>F)`[1] B_pvalue[i]<-aov_results[[1]]$`Pr(>F)`[2] AB_pvalue[i]<-aov_results[[1]]$`Pr(>F)`[3] } length(A_pvalue[A_pvalue<0.05])/1000 length(B_pvalue[B_pvalue<0.05])/1000 length(AB_pvalue[AB_pvalue<0.05])/1000
B1. Create a power curve showing how power for the interaction effect in this example is influenced by number of subjects. Choose a range of N from 25 to 800 (per cell) and run a simulation-based power analysis for increments of 25 subjects. Then plot the results using ggplot2 (2 points).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.