Skip to content
Snippets Groups Projects
Lab-8-T-test-Skeleton.R 4.5 KiB
Newer Older
Matthew K Defenderfer's avatar
Matthew K Defenderfer committed
# Load Packages
library(pastecs)
library(car)
library(tidyverse)

theme_set(theme_bw())


# Load Data
load()

## The file has two dataframes in it, sniff.ind and sniff.dep. Both dataframes
## have the following variables:

## group (factor, 2 levels): which group the mouse is a part of. Either control
##       or anesthetic treatment (Sevo) for sniff.ind, or familiar or novel for sniff.dep
## time (numeric): time spent sniffing another mouse


# Independent T-test
# We will be using the sniff.ind dataset for all of the Independent T-test section

## Assumptions

### Normality
### We need to test the assumption of normality within each group using the
### different graphs and statistical methods we have talked about before

#### Histograms 
#### We need to create multiple histograms, one for each group. Let's
#### make the fill color change by group, make them with 7 bins, and a black outline
ggplot(sniff.ind, aes(x = )) +
  geom_histogram(bins = , color = "black") +
  facet_wrap(, scales = "free")


#### Q-Q Plots
#### Do the same thing for Q-Q plots, create 1 for each group
ggplot(sniff.ind, aes()) +
  geom_qq() +
  geom_qq_line() +
  facet_wrap(, scales = "free")


#### Boxplots
#### For our boxplots, we can put group on the x-axis and the time values on the y-axis
ggplot(sniff.ind, aes()) +
  geom_boxplot()


#### Statistical Tests
#### Since we need to calculate normality across groups, we can use the tapply
#### function to apply a stat.desc function to data within each group in `time`

#### `tapply` has the following form:
  
#### `tapply(data, group_var,func, ...)`

#### `data`: vector of data to apply the function to
#### `group_var`: a factor variable that describes which group each element in `data` belongs to
#### `func`: name of the function to apply to each group
#### `...`: options to pass to `func`

#### Use tapply to calculate the normality stats for each group in `time` using stat.desc
tapply()


### Homogeneity of Variance
### The other test we need R for for independent t-test is homoscedasticity. We
### will just use Levene's Test like before
leveneTest()

## Performing an Independent T-test

## t.test is the function we will be using and has the following form:
## `t.test(formula, data, paired = FALSE, var.equal = FALSE)`

## `formula`: formula describing the independent variable being tested as a function of a grouping variable
## `data`: the dataset containing the variables
## `paired`: a binary value saying whether it is a paired t-test (FALSE by default)
## `var.equal`: says whether the variances of the group should be treated as equal (FALSE by default). 

## Let's calculate the t-test of time as a function of group
t.test()


# Dependent T-test
# We will now be switching to the sniff.dep dataframe.

## Assumptions
### Normality

### Normality tests for dependent t-test is done on the distribution of
### differences between your paired variable. In our case, it will be novel -
### familiar. We can spread our times into two columns to more easily make a
### difference variable

### We first need to add an ID variable to each mouse so `spread` knows which
### data to pair together. We can add an ID just by creating a vector of numbers
### from 1 to 30, twice
sniff.ind$idnum <- factor(c(seq(1,30),seq(1,30)))

### Now we can spread using the group variable as our key and the time variable as our value
sniff.dep.s <- spread(sniff.dep,key = , value = )

#### Histograms
#### Let's make histograms in the same way as before, just switching to a different dataframe
ggplot(sniff.dep.s, aes(x = )) +
  geom_histogram(bins = , color = "black")


#### Q-Q Plots
#### Do the same thing for Q-Q plots, create 1 for each group
ggplot(sniff.dep.s, aes()) +
  geom_qq() +
  geom_qq_line()


#### Boxplots
#### For our boxplots, we can put group on the x-axis and the time values on the y-axis
ggplot(sniff.dep, aes()) +
  geom_boxplot()

#### Statistical Tests
#### We just need to use stat.desc like normal on the time.diff column
stat.desc

## Perform a Dependent T-test
## We have a within-subject variable of `group` and our dependent variable
## `time`. Use t.test just like before except changing the paired option.
t.test()

## Another way to use t.test
## There is another way to use the t.test function. You can pass in two vectors
## of data as opposed to a formula if your data is set up that way.

## Now we can pass in the novel and familiar columns as inputs to t.test:
t.test(x = , y = ,)

## We can also just pass in the time.diff column to the x option, leave the y
## option blank and get the same thing
t.test(x = )