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)
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