library(meta)
## Warning: package 'meta' was built under R version 4.2.3
## Loading 'meta' package (version 6.2-1).
## Type 'help(meta)' for a brief overview.
## Readers of 'Meta-Analysis with R (Use R!)' should install
## older version of 'meta' package: https://tinyurl.com/dt4y5drs
library(metafor)
## Warning: package 'metafor' was built under R version 4.2.3
## Loading required package: Matrix
## Loading required package: metadat
## Warning: package 'metadat' was built under R version 4.2.3
## Loading required package: numDeriv
## 
## Loading the 'metafor' package (version 4.0-0). For an
## introduction to the package please type: help(metafor)
df <- read.csv("Seminar.csv")
View(df)

Perform Q Statics (Research Question 1)

meta <- rma(yi = hedges_g, sei = Standard_Error, data = df)
summary(meta)
## 
## Random-Effects Model (k = 10; tau^2 estimator: REML)
## 
##   logLik  deviance       AIC       BIC      AICc   
## -22.3656   44.7311   48.7311   49.1256   50.7311   
## 
## tau^2 (estimated amount of total heterogeneity): 8.3367 (SE = 4.1687)
## tau (square root of estimated tau^2 value):      2.8873
## I^2 (total heterogeneity / total variability):   99.91%
## H^2 (total variability / sampling variability):  1101.66
## 
## Test for Heterogeneity:
## Q(df = 9) = 48634.9872, p-val < .0001
## 
## Model Results:
## 
## estimate      se    zval    pval    ci.lb   ci.ub    
##   1.6028  0.9411  1.7031  0.0886  -0.2418  3.4473  . 
## 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Perform a descriptive analysis of effect sizes

# Calculate effect sizes
smd <- df$hedges_g
se <- df$Standard_Error
res <- rma(yi = hedges_g, sei = Standard_Error, data = df)
n <- res$k
min_g <- min(res$yi)
max_g <- max(res$yi)
mean_g <- res$b
se_mean <- res$se
sd_g <- sqrt(res$tau2)
# Create a data frame with the descriptive statistics
descriptive_stats <- data.frame(
  Statistics = c("Number of effect sizes", "Minimum Hedges' g", "Maximum Hedges' g", "Mean Hedges' g", "Standard error of the mean", "Standard deviation"),
  Value = c(n, min_g, max_g, mean_g, se_mean, sd_g)
)

# Print the data frame
descriptive_stats
##                   Statistics     Value
## 1     Number of effect sizes 10.000000
## 2          Minimum Hedges' g  0.190000
## 3          Maximum Hedges' g  9.450000
## 4             Mean Hedges' g  1.602761
## 5 Standard error of the mean  0.941101
## 6         Standard deviation  2.887326

Research Question 1

# Create a data frame with your effect sizes and standard errors
data <- data.frame(hedges_g = df$hedges_g,se = df$Standard_Error)
# Calculate the average effect size
avg_effect_size <- mean(data$hedges_g)

# Calculate the variance of the effect sizes
var_effect_sizes <- sum((data$hedges_g - avg_effect_size)^2) / (nrow(data) - 1)

# Calculate the standard error of the effect sizes
se_effect_sizes <- sqrt(var_effect_sizes / nrow(data))

# Calculate the z-values for each effect size
z_values <- (data$hedges_g - 0) / data$se

# Calculate the p-values for each z-value
p_values <- 2 * (1 - pnorm(abs(z_values)))

# Calculate the 95% confidence interval for the effect sizes
lower_bound <- data$hedges_g - (1.96 * data$se)
upper_bound <- data$hedges_g + (1.96 * data$se)

# Print the results
cat("Average effect size:", avg_effect_size, "
")
## Average effect size: 1.547
cat("Variance of effect sizes:", var_effect_sizes, "
")
## Variance of effect sizes: 7.977401
cat("Standard error of effect sizes:", se_effect_sizes, "
")
## Standard error of effect sizes: 0.893163
cat("Average z-value of effect sizes:", mean(z_values), "
")
## Average z-value of effect sizes: 96.40065
cat("Avergae p-values of effect sizes:", mean(p_values), "
")
## Avergae p-values of effect sizes: 0.2893444
cat("Average 95% confidence interval lower bound:", mean (lower_bound), "
")
## Average 95% confidence interval lower bound: 0.38864
cat("Average 95% confidence interval upper bound:", mean(upper_bound), "
")
## Average 95% confidence interval upper bound: 2.70536

Research Question 2

# Calculate the number of effect sizes for each institution type
n <- tapply(df$hedges_g, df$Course_Subject, length)
# Calculate the average effect size for each institution type
mean <- tapply(df$hedges_g, df$Course_Subject, mean)
# Calculate the variance of effect sizes for each institution type
variance <- tapply(df$hedges_g, df$Course_Subject, var)
# Calculate the standard error of effect sizes for each institution type
se <- sqrt(variance / n)

# Calculate the z-values for each institution type
z <- mean / se

# Calculate the p-values for each institution type (two-tailed test)
p <- 2 * (1 - pnorm(abs(z)))

# Calculate the 95% confidence interval lower bound and upper bound for each institution type
ci_lower <- mean - qt(0.975, n-1) * se
ci_upper <- mean + qt(0.975, n-1) * se

# Combine the results into a data frame
results <- data.frame(institution = unique(df$Course_Subject), n = n, mean = mean, variance = variance, se = se, z = z, p = p, ci_lower = ci_lower, ci_upper = ci_upper)

# Print the results
results
##                   institution n     mean variance        se        z
## Health Science Health Science 5 0.870000  0.43865 0.2961925 2.937279
## Other          Social Science 3 3.356667 27.84813 3.0467542 1.101719
## Social Science          Other 2 0.525000  0.14045 0.2650000 1.981132
##                          p    ci_lower  ci_upper
## Health Science 0.003311062  0.04763777  1.692362
## Other          0.270583899 -9.75245858 16.465792
## Social Science 0.047576464 -2.84214426  3.892144

Research Question 2

# Calculate the number of effect sizes for each institution type
n <- tapply(df$hedges_g, df$Institution_Type, length)
# Calculate the average effect size for each institution type
mean <- tapply(df$hedges_g, df$Institution_Type, mean)
# Calculate the variance of effect sizes for each institution type
variance <- tapply(df$hedges_g, df$Institution_Type, var)
# Calculate the standard error of effect sizes for each institution type
se <- sqrt(variance / n)

# Calculate the z-values for each institution type
z <- mean / se

# Calculate the p-values for each institution type (two-tailed test)
p <- 2 * (1 - pnorm(abs(z)))

# Calculate the 95% confidence interval lower bound and upper bound for each institution type
ci_lower <- mean - qt(0.975, n-1) * se
ci_upper <- mean + qt(0.975, n-1) * se

# Combine the results into a data frame
results <- data.frame(institution = unique(df$Institution_Type), n = n, mean = mean, variance = variance, se = se, z = z, p = p, ci_lower = ci_lower, ci_upper = ci_upper)

# Print the results
results
##                 institution n  mean    variance        se        z            p
## Graduate      Undergraduate 4 0.530  0.08686667 0.1473658 3.596494 0.0003225356
## Undergraduate      Graduate 6 2.225 12.92815000 1.4678868 1.515784 0.1295738636
##                  ci_lower  ci_upper
## Graduate       0.06101639 0.9989836
## Undergraduate -1.54832315 5.9983232

Test for heterogeneity based on institution type

# fit a random-effects model with Institution_Type as the moderator
library(metafor)
model_inst <- rma(yi = hedges_g, sei = Standard_Error, mods = ~ Institution_Type, data = df, method = "REML")
Q_inst <- summary(model_inst)$QE[1]
Q_inst
## [1] 47780.19

Test for heterogeneity based on Course Subject

# fit a random-effects model with Course_Subject_Type as the moderator
model_course <- rma(yi = hedges_g, sei = Standard_Error, mods = ~ Course_Subject, data = df, method = "REML")
Q_inst <- summary(model_course)$QE[1]
Q_inst
## [1] 47458.91