Skip to content

Instantly share code, notes, and snippets.

@oliviergimenez
Created September 1, 2023 14:34
Show Gist options
  • Save oliviergimenez/856eaf92e87b9705e9aa34697fccd719 to your computer and use it in GitHub Desktop.
Save oliviergimenez/856eaf92e87b9705e9aa34697fccd719 to your computer and use it in GitHub Desktop.
z-test
# we seek to test whether two parameters, say theta1 and theta2, are diff from each other, using a z-test
# we assume we have an estimate of the thetaj's (mle1 and mle2) and associated standard errors (se1 and se2)
# we write a function that calculate the z-statistic and return the p-value (both-sided test)
# function
ztest <- function(mle1, mle2, se1, se2){
stat <- (mle1 - mle2)/sqrt(se1^2+se2^2)
pvalue <- ifelse(stat > 0, 2 * pnorm(stat, lower.tail = FALSE), 2 * pnorm(stat, lower.tail = TRUE))
return(data.frame("z-score" = stat, "p-value" = pvalue))
}
# example
m1 <- 0.8
m2 <- 0.6
ste1 <- 0.06
ste2 <- 0.05
ztest(m1, m2, ste1, ste2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment