25 sep 2018

Paired 2 samples

## Paired-samples t-test

In the Paired samples t-test the deviation ($$D$$) for each pair is calculated and the mean of these deviations ($$\bar{D}$$) is tested against the null hypothesis where $$\mu = 0$$.

$t_{n-1} = \frac{\bar{D} - \mu}{ {SE}_D }$ Where $$n$$ (the number of cases) minus $$1$$, are the degrees of freedom $$df = n - 1$$ and $$SE_D$$ is the standard error of $$D$$, defined as $$s_D/\sqrt{n}$$.

## Hypothesis

\LARGE{ \begin{aligned} H_0 &: \bar{D} = \mu_D \\ H_A &: \bar{D} \neq \mu_D \\ H_A &: \bar{D} > \mu_D \\ H_A &: \bar{D} < \mu_D \\ \end{aligned}}

## Data structure

index k1 k2
1 d d
2 d d
3 d d
4 d d

Where $$k$$ is the level of the categorical predictor variabla and $$d$$ is the value of the outcome/dependent variable.

## Data example

We are going to use the IQ estimates we collected last week. You had to gues the IQ of the one sitting next to you and your own IQ.

Let's take a look at the data.

## Calculate $$D$$

D = IQ.next.to.you - IQ.you

## Calculate $$\bar{D}$$

D      = na.omit(D) # get rid of all missing values
D.mean = mean(D)
D.mean
## [1] 1.959184

And we also need n.

n = length(D)
n
## [1] 49

## Calculate t-value

$t_{n-1} = \frac{\bar{D} - \mu}{ {SE}_D }$

mu = 0                # Define mu

D.sd = sd(D)          # Calculate standard deviation
D.se = D.sd / sqrt(n) # Calculate standard error

df   = n - 1          # Calculate degrees of freedom

# Calculate t
t = ( D.mean - mu ) / D.se
t
## [1] 1.915726

## Test for significance

Two tailed

if(!"visualize" %in% installed.packages()) { install.packages("visualize") }
library("visualize")

visualize.t(c(-t,t), df, section="tails")