# changing the directory
setwd('/Users/stp48131/Library/CloudStorage/Dropbox/WKU/Teaching/ECON_307/Class_Materials/Honors/Comparing_Two_Samples')
# loading packages
library(readxl) # for loading the Excel file
library(tidyverse) # for the lag function to generate returns
# loading the excel file and assigning it to the data frame named data
data <- read_excel("comparing_two_samples_R.xlsx")
# attaching so I do not have to reference the data frame each time
attach(data)
# generating the return variables
# we do not have to use data$ on the right side of the <-
# since we attached the data dataframe. If we wanted to put this
# variable in the data dataframe we would have to add data$ before BTC_return,
# ETH_return, and LTC_return
BTC_return <- BTC / lag(BTC) -1
ETH_return <- ETH / lag(ETH) -1
LTC_return <- LTC / lag(LTC) -1
We can conduct the t-test using the t.test()
function.
This will display a test statistic and p-value that you can use to
determine the result of the test. The alternative option could be
“two.sided”, “greater”, or “less” depending on the test you are
using.
# T-test
# BTC vs ETH
t.test(BTC_return,ETH_return, # variables you want to test
alternative = 'two.sided', # this is for a two-tailed test
paired=FALSE, # TRUE if it is a paired-sample
var.equal = FALSE, # always use FALSE here
conf.level = .95) # confidence level
##
## Welch Two Sample t-test
##
## data: BTC_return and ETH_return
## t = -0.73325, df = 94.358, p-value = 0.4652
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.07188807 0.03311043
## sample estimates:
## mean of x mean of y
## 0.03230173 0.05169055
Fail to reject null hypothesis because p-value>\(\alpha\).
#BTC vs LTC
t.test(BTC_return,LTC_return,
alternative = 'two.sided',
paired=FALSE,
var.equal = FALSE,
conf.level = .95)
##
## Welch Two Sample t-test
##
## data: BTC_return and LTC_return
## t = -0.25478, df = 85.152, p-value = 0.7995
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.06732693 0.05203191
## sample estimates:
## mean of x mean of y
## 0.03230173 0.03994924
Fail to reject null hypothesis because p-value>\(\alpha\).
#ETH vs LTC
t.test(ETH_return,LTC_return,
alternative = 'two.sided',
paired=FALSE,
var.equal = FALSE,
conf.level = .95)
##
## Welch Two Sample t-test
##
## data: ETH_return and LTC_return
## t = 0.35402, df = 98.679, p-value = 0.7241
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.05406961 0.07755223
## sample estimates:
## mean of x mean of y
## 0.05169055 0.03994924
Fail to reject null hypothesis because p-value>\(\alpha\).
We can also conduct a test for the variances. This uses the
var.test()
function. The options are similar to the one
used in the t.test()
.
# F-test
# BTC vs ETH
var.test(BTC_return, ETH_return,
alternative = "two.sided",
conf.level = .95)
##
## F test to compare two variances
##
## data: BTC_return and ETH_return
## F = 0.55692, num df = 51, denom df = 51, p-value = 0.03889
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.3196876 0.9702061
## sample estimates:
## ratio of variances
## 0.5569227
Reject null hypothesis because p-value<\(\alpha\).
# BTC vs LTC
var.test(BTC_return, LTC_return,
alternative = "two.sided",
conf.level = .95)
##
## F test to compare two variances
##
## data: BTC_return and LTC_return
## F = 0.38426, num df = 51, denom df = 51, p-value = 0.0008464
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.2205732 0.6694082
## sample estimates:
## ratio of variances
## 0.3842571
Reject null hypothesis because p-value<\(\alpha\).
# ETH vs LTC
var.test(ETH_return, LTC_return,
alternative = "two.sided",
conf.level = .95)
##
## F test to compare two variances
##
## data: ETH_return and LTC_return
## F = 0.68996, num df = 51, denom df = 51, p-value = 0.1885
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.3960572 1.2019770
## sample estimates:
## ratio of variances
## 0.689965
Fail to reject null hypothesis because p-value>\(\alpha\).