Skip to content

Instantly share code, notes, and snippets.

@dsquintana
Created July 20, 2018 19:30
Show Gist options
  • Save dsquintana/2c35348250cad752d37d26f40c5ac595 to your computer and use it in GitHub Desktop.
Save dsquintana/2c35348250cad752d37d26f40c5ac595 to your computer and use it in GitHub Desktop.
t-statistic
# Write function
t.test2 <- function(m1,m2,s1,s2,n1,n2,m0=0,equal.variance=FALSE)
{
if( equal.variance==FALSE )
{
se <- sqrt( (s1^2/n1) + (s2^2/n2) )
# welch-satterthwaite df
df <- ( (s1^2/n1 + s2^2/n2)^2 )/( (s1^2/n1)^2/(n1-1) + (s2^2/n2)^2/(n2-1) )
} else
{
# pooled standard deviation, scaled by the sample sizes
se <- sqrt( (1/n1 + 1/n2) * ((n1-1)*s1^2 + (n2-1)*s2^2)/(n1+n2-2) )
df <- n1+n2-2
}
t <- (m1-m2-m0)/se
dat <- c(m1-m2, se, t, 2*pt(-abs(t),df))
names(dat) <- c("Difference of means", "Std Error", "t", "p-value")
return(dat)
}
# Calculate t-statistic and p-value
t.test2(m1=37.33, m2=39.65, s1=13.40, s2=15.91, n1=23,n2=32,
equal.variance = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment