Skip to content

Instantly share code, notes, and snippets.

@AbeKh
Created November 27, 2023 19:32
Show Gist options
  • Save AbeKh/eac2d25586641ecb513d70f43e515db3 to your computer and use it in GitHub Desktop.
Save AbeKh/eac2d25586641ecb513d70f43e515db3 to your computer and use it in GitHub Desktop.
A two sample t test fucntion to output p-value from inputs of means and standard error
t.test_SEM <- function(mean1, mean2, SE1, SE2, n1, n2) {
# Calculate t-statistic
t_statistic <- (mean1 - mean2) / sqrt(SE1^2 + SE2^2)
# Calculate degrees of freedom
df <- ((SE1^2 + SE2^2)^2) / (((SE1^2)^2 / (n1 - 1)) + ((SE2^2)^2 / (n2 - 1)))
# Calculate p-value
p_value <- 2 * pt(abs(t_statistic), df = df, lower.tail = FALSE)
return(p_value)
}
# Example usage:
mean1 <- 5 #means
mean2 <- 7
SE1 <- 0.1 # standard errors
SE2 <- 0.15
n1 <- 10 # number of samples
n2 <- 12
p_value <- t.test_SEM(mean1, mean2, SE1, SE2, n1, n2)
print(p_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment