options(digits=5,width=70) # R console display parameters
options(contrasts=c("contr.sum","contr.poly") )
#install.packages("DAAG")
library(DAAG)
data("mignonette") # load data file
?mignonette # read about the data file
Answer the questions in Section 2 and submit your answers as a script file on Avenue 2 Learn. Make sure to begin your script file with the following lines:
# PSYCH 710 Lab 2 Homework
# Date: 15-SEP-2022
# Your Name: <<Your name here>>
# Student ID: <<Your ID here>>
# Collaborators: <<Names of your collaborators here>>
Also, make sure that text that is not an R command is preceded by a comment symbol (#). For example, you can insert questions or comments among your commands like this:
# The following command doesn't work... not sure why...
# ttest(x=g1,y=g2) # was trying to do a t test
The mignonette data frame contains the results of an experiment that compared the heights of crossed plants with self-fertilized plants. Plants were paired within the pots in which they were grown, with one on one side and one on the other.
## 1. Use an independent samples t test to examine whether heights
## differed in the crossed- and self-fertilized conditions.
# answer 1 :
# the variances differ by a factor of approx 2... not too much...
# nevertheless I will use the results of the 2nd t test
# (the differences between t tests are trivial)
sapply(mignonette,var)
## cross self
## 22.2716 9.7955
with(mignonette,t.test(cross,self,var.equal=T) ) # assume equal variances
##
## Two Sample t-test
##
## data: cross and self
## t = 2.21, df = 46, p-value = 0.032
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.23056 4.88402
## sample estimates:
## mean of x mean of y
## 17.177 14.620
with(mignonette,t.test(cross,self,var.equal=F) ) # do NOT assume equal variances
##
## Welch Two Sample t-test
##
## data: cross and self
## t = 2.21, df = 40, p-value = 0.033
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.22102 4.89356
## sample estimates:
## mean of x mean of y
## 17.177 14.620
library(effectsize)
with(mignonette,cohens_d(x=cross,y=self,paired=FALSE,mu=0) )
## Cohen's d | 95% CI
## ------------------------
## 0.64 | [0.05, 1.22]
##
## - Estimated using pooled SD.
## Analyzing the mignonette data using a paired-sample t test.
# answer 2:
# I reject the null hypothesis of no difference between group means.
# Equivalently, I reject the hypothesis that the mean of the difference scores is zero.
with(mignonette,t.test(cross,self,paired=T) )
##
## Paired t-test
##
## data: cross and self
## t = 2.12, df = 23, p-value = 0.045
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 0.05823 5.05635
## sample estimates:
## mean difference
## 2.5573
with(mignonette,cohens_d(x=cross,y=self,paired=TRUE,mu=0) )
## Cohen's d | 95% CI
## ------------------------
## 0.43 | [0.01, 0.85]
# answer 3:
# The variance of a difference score A-B is
# VAR(A) + VAR(B) - 2*COV(A,B) = VAR(A) + VAR(B) - 2*r*SD(A)*SD(B)
# So the variance of the difference score is reduced when A & B are correlated.
# In this case the two sets of data are not strongly correlated,
# so pairing does not provide much of an advantage
# and the results of the two tests are similar
with(mignonette,cor.test(cross,self))
##
## Pearson's product-moment correlation
##
## data: cross and self
## t = -0.472, df = 22, p-value = 0.64
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.48400 0.31601
## sample estimates:
## cor
## -0.10016
# I will illustrate this point by creating a new mignonette data set
# where cross and self are correlated
library(MASS)
Sigma <- matrix(c(22.2716,12,12,9.7955),2,2)
mignon2 <- data.frame(mvrnorm(n=24,mu=c(17.177,14.62),Sigma,empirical=TRUE))
names(mignon2) <- c("cross","self")
# means and variances are the same as in mignonette but variables are correlated strongly
# original data
sapply(mignonette,mean)
## cross self
## 17.177 14.620
sapply(mignonette,var)
## cross self
## 22.2716 9.7955
# new data
sapply(mignon2,mean)
## cross self
## 17.177 14.620
sapply(mignon2,var)
## cross self
## 22.2716 9.7955
with(mignon2,cor.test(cross,self))
##
## Pearson's product-moment correlation
##
## data: cross and self
## t = 6.54, df = 22, p-value = 1.4e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.60846 0.91572
## sample estimates:
## cor
## 0.81244
# now I do independent & paired t tests on new data
with(mignon2,t.test(cross,self,paired=F) ) # same as before
##
## Welch Two Sample t-test
##
## data: cross and self
## t = 2.21, df = 40, p-value = 0.033
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.22073 4.89327
## sample estimates:
## mean of x mean of y
## 17.177 14.620
with(mignon2,t.test(cross,self,paired=T) ) # p value is much lower!
##
## Paired t-test
##
## data: cross and self
## t = 4.41, df = 23, p-value = 2e-04
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 1.3577 3.7563
## sample estimates:
## mean difference
## 2.557
# compare mean & variance of DIFFERENCE scores:
d1 <- mignonette$cross - mignonette$self
d2 <- mignon2$cross - mignon2$self
c(mean(d1),var(d1))
## [1] 2.5573 35.0258
c(mean(d2),var(d2)) # variance is lower
## [1] 2.5570 8.0671
cohens_d(d1,mu=0)
## Cohen's d | 95% CI
## ------------------------
## 0.43 | [0.01, 0.85]
cohens_d(d2,mu=0) # cohen's d is much greater
## Cohen's d | 95% CI
## ------------------------
## 0.90 | [0.42, 1.37]
UPPER.BOUND <- 4
LOWER.BOUND <- -4
# test upper bound:
with(mignonette, t.test(cross,self,mu=UPPER.BOUND,alternative="less") )
##
## Welch Two Sample t-test
##
## data: cross and self
## t = -1.25, df = 40, p-value = 0.11
## alternative hypothesis: true difference in means is less than 4
## 95 percent confidence interval:
## -Inf 4.5037
## sample estimates:
## mean of x mean of y
## 17.177 14.620
# Do not reject null hypothesis that MU_d >= 4
# test lower bound:
with(mignonette, t.test(cross,self,mu=LOWER.BOUND,alternative="greater") )
##
## Welch Two Sample t-test
##
## data: cross and self
## t = 5.67, df = 40, p-value = 6.8e-07
## alternative hypothesis: true difference in means is greater than -4
## 95 percent confidence interval:
## 0.61086 Inf
## sample estimates:
## mean of x mean of y
## 17.177 14.620
# Reject null hypothesis that MU_d <= -4 in favor of alternative MU_d > -4
# CONCLUSION: I do not reject the null equivalence hypothesis that the true difference is ≥ 4 OR ≤ -4.
## Using the TOST procedure we do not reject the hypothesis that the two means differ, t(40) = -1.25, p=0.11.
## Next we evaluate the equivalence null hypothesis with a 2-tailed, 90% CI.
## The CI does NOT lie between the bounds [-4, +4].
## So we fail to reject the null hypothesis that the MU_d is ≤ -4 OR ≥ 4.
# calculate 90% CI:
with(mignonette, t.test(cross,self,mu=0,conf.level=0.90) )
##
## Welch Two Sample t-test
##
## data: cross and self
## t = 2.21, df = 40, p-value = 0.033
## alternative hypothesis: true difference in means is not equal to 0
## 90 percent confidence interval:
## 0.61086 4.50373
## sample estimates:
## mean of x mean of y
## 17.177 14.620
## Here is an answer using the TOSTER library.
library(TOSTER)
# help(package="TOSTER") # inspect the various commands
# ?t_TOST # this looks promising
UPPER.BOUND <- 4
LOWER.BOUND <- -4
t_TOST(x=mignonette$cross,
y=mignonette$self,
eqb=c(LOWER.BOUND,UPPER.BOUND))
##
## Welch Two Sample t-test
##
## The equivalence test was non-significant, t(39.95) = -1.2, p = 0.11
## The null hypothesis test was significant, t(39.95) = 2.212p = 0.03
## NHST: reject null significance hypothesis that the effect is equal to zero
## TOST: don't reject null equivalence hypothesis
##
## TOST Results
## t df p.value
## t-test 2.212 39.95 0.033
## TOST Lower 5.673 39.95 < 0.001
## TOST Upper -1.248 39.95 0.11
##
## Effect Sizes
## Estimate SE C.I. Conf. Level
## Raw 2.5573 1.1559 [0.6109, 4.5037] 0.9
## Hedges's g(av) 0.6266 0.3509 [0.143, 1.1026] 0.9
## Note: SMD confidence intervals are an approximation. See vignette("SMD_calcs").