# ----------------------------------------------------------
# call ttest with a formula
# ----------------------------------------------------------
# create simulated data, no population mean difference
# X has two values only, Y is numeric
# put into a data frame, required for formula version
n <- 12
X <- sample(c("Group1","Group2"), size=n, replace=TRUE)
Y <- rnorm(n=n, mean=50, sd=10)
mydata <- data.frame(X,Y)
# analyze data with formula version
# variable names and levels of X are automatically obtained from data
# although data frame is not attached, can reference the variable
# names directly
ttest(Y ~ X)
# brief version of results
ttest.brief(Y ~ X)
# Compare to standard R function t.test
t.test(Y ~ X, var.equal=TRUE)
# consider the practical importance of the difference
ttest(Y ~ X, msmd=.5)
# variable of interest is in a data frame which is not the default mydata
# access the data frame in the R provided warpbreaks data set
# although data not attached, access the variables directly by their name
data(warpbreaks)
ttest(breaks ~ wool, dframe=warpbreaks)
# -------------------------------------------------------
# data stored in two vectors, call ttest accordingly
# -------------------------------------------------------
# create two separate vectors of response variable Y
# the vectors exist are not in a data frame
# their lengths need not be equal
n <- 10
Y1 <- rnorm(n=n/2, mean=50, sd=10)
Y2 <- rnorm(n=n/2, mean=60, sd=10)
# analyze the two vectors directly
# usually explicitly specify variable names and levels of X
# to enhance the readability of the output
ttest(Y1, Y2, Ynm="MyY", Xnm="MyX", X1nm="Group1", X2nm="Group2")
Run the code above in your browser using DataLab