---
title: "t-tests and power"
author: "Tera Letzring"
date: "October 2023"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)
library(car)          #to compute Levene's test
library(effectsize)   #to compute effect sizes for one and matched samples
library(compute.es)   #to compute effect sizes for independent samples
library(pwr)          #to compute power
library(presize)      #for precision analysis
```

```{r read in data}

#set the working directory (wd) to the folder that contains the script file, and read in the data
x <- getwd()
setwd(x)

#this data looked at variables related to volunteering for psychological research
mydata = read.table("volunteer.csv", header=T, sep=",")  
attach(mydata)
head(mydata)
```

```{r one-sample t-test}

###1) One-sample, two-tailed, all default options
t.test(extraversion)
sd(extraversion)           #calculate SD to report in results

###One-sample, one-tailed, compare to value of 5, 99% confidence interval
t.test(mydata$extraversion, alternative='greater', mu=5, conf.level=.99)

###2) Effect sizes

#Cohen's d for null = 0
cohens_d(extraversion)

#Cohen's d for null = 5
cohens_d(extraversion, mu=5)

#Hedge's g for null = 0. Correct for small N. Use with N < 20.
hedges_g(extraversion)

#Hedge's g for null = 5
hedges_g(extraversion, mu=5)

```

```{r matched/paired/related-samples t-test}

###Matched t-test using data set with different variables that are being compared

#3)	Compare two variables for the same set of participants, all default options
t.test(extraversion, neuroticism, paired=TRUE)

#create variable for the difference between the variables being compared and calculate standard deviation of those difference scores
mydata$diffEN <- extraversion - neuroticism
sd(mydata$diffEN)

#with options 
t.test(extraversion, neuroticism, paired=TRUE, alternative='greater', conf.level=.99)

#Effect sizes 
cohens_d(Pair(extraversion, neuroticism), data = mydata)
hedges_g(Pair(extraversion, neuroticism), data = mydata)


###4) with data set up so that there is a grouping variable

#read in R dataset called sleep, with increases in hours of sleep (extra) for 2 drugs (group).
data(sleep)

#Paired sample t-test
t.test(extra ~ group, paired = TRUE, data = sleep)

#with options
t.test(extra ~ group, paired = TRUE, alternative='greater', conf.level=.99, data = sleep)

#Effect sizes
cohens_d(sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2], paired = TRUE)

hedges_g(sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2], paired = TRUE)

```

```{r independent samples t-test}

###compare neuroticism levels for people who did and did not volunteer

#5)Determine equality of variances
#output variance for each group
tapply(neuroticism, volunteer, var, na.rm=TRUE)

#compute Levene's test for equality of variances
leveneTest(neuroticism, volunteer)

#6)t-test

#all default options 
t.test(neuroticism~volunteer)
       
#equal variances assumed
t.test(neuroticism~volunteer, var.equal=TRUE)

#one-tailed, equal variances assumed, 99% CI
t.test(neuroticism~volunteer, alternative='greater', conf.level=.99, var.equal=TRUE)

###Effect sizes with confidence intervals

#From effectsize package
#assumes equal variances
cohens_d(neuroticism~volunteer, data = mydata)
#does not assume equal variances
cohens_d(neuroticism~volunteer, data = mydata, var.equal= FALSE)

#From compute.es package
#determine sample size of groups
table(volunteer)

tes(t=-.4755, n.1=824, n.2=597)     #this outputs several effect sizes
tes(-.4755, 824, 597)


```

```{r power and precision analysis}

#two-tailed tests are the default. 

#take out one variable (n, d, sig.level, or power) to calculate that value

###to get needed sample size
#default option for type is independent samples t-test 
pwr.t.test(n=NULL, d = .30, sig.level = .05, power = .90)
#one-sample t-test
pwr.t.test(d = .30, sig.level = .05, power = .90, type = "one.sample")
#matched-samples t-test
pwr.t.test(d = .50, sig.level = .05, power = .80, type = "paired")

#sensitivity analysis for independent samples t-test
pwr.t.test(n=200, d = NULL, sig.level = .05, power = .90)

#precision analysis for confidence intervals for one-sample and paired-samples t-tests
prec_mean(mean=12, sd=4, n=NULL, conf.width=1, conf.level=.95)

#precision analysis for confidence intervals for independent-samples t-tests. delta = difference in means between groups, r = relative size of groups. Default is equal variances assumed.
prec_meandiff(delta=2, sd1=3, n1=NULL, r=1, conf.width=1, conf.level=.95)

#equal variances not assumed
prec_meandiff(delta=2, sd1=3, sd2=4, n1=NULL, r=1, conf.width=.5, conf.level=.95, variance="unequal")
```
