1 Initialize R

options(digits=5,width=70) # R console display parameters
options(contrasts=c("contr.sum","contr.poly") )
# install.packages("BSDA")
library(BSDA) # use this for z.test()

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

2 z & t tests

  1. Use the following code to create a sample (\(n=20\)) by randomly selecting values from a normal distribution with a mean of 5 and a standard deviation of 10.
set.seed(210980) # set random number seed
dscores <- rnorm(n=20,5,10) # get the values
  1. Calculate the mean, variance, and standard deviation of dscores. Use a boxplot to display the distribution of values.
c(mean(dscores),var(dscores),sd(dscores))
## [1]   4.5103 103.7138  10.1840
boxplot(dscores,ylab="dscore")

  1. Use a quantile-quantile plot to investigate whether the dscores is distributed normally. Do the data look approximately normal? Use shapiro.test to address this question with a quantitative test.
# the data fall along a straight line and therefore look normal
qqnorm(dscores)
qqline(dscores)

shapiro.test(dscores) # we do not reject the null hypothesis of normality
## 
##  Shapiro-Wilk normality test
## 
## data:  dscores
## W = 0.95, p-value = 0.37
  1. Imagine that we conducted an experiment that evaluated the effect of a drug on memory. Twenty participants completed a standardized memory test before and after taking the drug. The difference scores on the two memory tests (post-drug minus pre-drug) are stored in dscores. Use a \(t\) to evaluate the hypothesis that memory was affected (positively or negatively) by the drug. State the null and alternative hypotheses evaluated by your test, and whether or not you reject the null hypothesis.
# H0: mu = 0  H1: mu ≠ 0
t.test(dscores,mu=0,alternative="two.sided")
## 
##  One Sample t-test
## 
## data:  dscores
## t = 1.98, df = 19, p-value = 0.062
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.25597  9.27655
## sample estimates:
## mean of x 
##    4.5103

Answer: The null hypothesis is that the population mean of difference scores is zero and the alternative is that the mean is not equal to zero. This test is two-sided because we are examining if the drug affected memory positively OR negatively. The test was not significant (\(t=1.98\), \(p=0.06\)) and therefore I do not reject the null hypothesis.

  1. Use z.test to repeat the analysis performed in question 3 with a \(z\) test. To use z.test, you need to set sigma.x to a value that equals the population standard deviation (\(\sigma\)) of dscores. Although we do not know \(\sigma\), we can estimate it with the sample standard deviation, sigma.x = sd(dscores). Perform the \(z\) test this way. How do the results differ from the \(t\) test in question 4? Why do they differ? Which test do you think is more valid?
# H0: mu = 0  H1: mu ≠ 0
z.test(x=dscores,alternative="two.sided",mu=0,sigma.x=sd(dscores),conf.level=0.95)
## 
##  One-sample z-Test
## 
## data:  dscores
## z = 1.98, p-value = 0.048
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.047038 8.973539
## sample estimates:
## mean of x 
##    4.5103
# Unlike the t test, the z test is significant p<.05 and therefore we reject H0 in favor of H1
# The z and t test differ because the ESTIMATE of pop SD is biased (too small) and therefore
# inflates z and results in more Type I errors. I trust the t test more.
  1. We believe that the drug will improve memory. Use a one-sided \(t\) test to evaluate this idea. Clearly state the null and alternative hypotheses, the \(t\) and \(p\) values, and your conclusion regarding the null hypothesis.
# H0: mu ≤ 0  H1: mu > 0
# I reject the null hypothesis, t(19)=1.98, p=0.031

t.test(dscores,mu=0,alternative="greater")
## 
##  One Sample t-test
## 
## data:  dscores
## t = 1.98, df = 19, p-value = 0.031
## alternative hypothesis: true mean is greater than 0
## 95 percent confidence interval:
##  0.57269     Inf
## sample estimates:
## mean of x 
##    4.5103