Front
Back
How do you run a two-sample t-test in R?
Use t.test(x, y, var.equal=) for means difference.
Code:
t.test(mpg ~ am, data = mtcars, var.equal = FALSE)
Notes:
Check normality & variance assumptions; use wilcox.test for nonparametric alternative.
How do you run a chi-squared test of independence?
Use chisq.test() on a contingency table.
Code:
tbl <- table(mtcars$cyl, mtcars$am)
chisq.test(tbl)
Notes:
Expected counts should be sufficiently large; else consider Fisher’s exact test (fisher.test).
How do you test correlation with cor.test()?
Use cor.test(x, y, method=’pearson’|’spearman’).
Code:
cor.test(mtcars$mpg, mtcars$hp, method=’spearman’)
How do you check normality?
Use shapiro.test() for small samples; also inspect QQ plots.
Code:
shapiro.test(mtcars$mpg)